payload
Version:
Node, React, Headless CMS and Application Framework built on Next.js
57 lines (56 loc) • 2 kB
JavaScript
// @ts-strict-ignore
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