Files
klz-cables.com/.pnpm-store/v10/files/22/1b9ee6ec9e761c8215f2dd1e231cc63bc83852516f291c0f05a77038bc550682848a2a6891c7722b872ce63a98f5dfa85c2129df6166300218c2b92f28467b
Marc Mintel 5397309103
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
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

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