Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
56 lines
1.9 KiB
Plaintext
56 lines
1.9 KiB
Plaintext
import { APIErrorName } from '../errors/APIError.js';
|
|
import { ValidationErrorName } from '../errors/ValidationError.js';
|
|
export const formatErrors = (incoming)=>{
|
|
if (incoming) {
|
|
// Cannot use `instanceof` to check error type: https://github.com/microsoft/TypeScript/issues/13965
|
|
// Instead, get the prototype of the incoming error and check its constructor name
|
|
const proto = Object.getPrototypeOf(incoming);
|
|
// Payload 'ValidationError' and 'APIError'
|
|
if ((proto.constructor.name === ValidationErrorName || proto.constructor.name === APIErrorName) && incoming.data) {
|
|
return {
|
|
errors: [
|
|
{
|
|
name: incoming.name,
|
|
data: incoming.data,
|
|
message: incoming.message
|
|
}
|
|
]
|
|
};
|
|
}
|
|
// Mongoose 'ValidationError': https://mongoosejs.com/docs/api/error.html#Error.ValidationError
|
|
if (proto.constructor.name === ValidationErrorName && 'errors' in incoming && incoming.errors) {
|
|
return {
|
|
errors: Object.keys(incoming.errors).reduce((acc, key)=>{
|
|
acc.push({
|
|
field: incoming.errors[key].path,
|
|
message: incoming.errors[key].message
|
|
});
|
|
return acc;
|
|
}, [])
|
|
};
|
|
}
|
|
if (Array.isArray(incoming.message)) {
|
|
return {
|
|
errors: incoming.message
|
|
};
|
|
}
|
|
if (incoming.name) {
|
|
return {
|
|
errors: [
|
|
{
|
|
message: incoming.message
|
|
}
|
|
]
|
|
};
|
|
}
|
|
}
|
|
return {
|
|
errors: [
|
|
{
|
|
message: 'An unknown error occurred.'
|
|
}
|
|
]
|
|
};
|
|
};
|
|
|
|
//# sourceMappingURL=formatErrors.js.map |