hapi-sequelize-crud/src/error.js

31 lines
747 B
JavaScript
Raw Normal View History

import { wrap } from 'boom';
export default (target, key, descriptor) => {
2016-07-09 05:11:23 +00:00
const fn = descriptor.value;
descriptor.value = async (request, reply) => {
try {
await fn(request, reply);
2016-07-09 05:11:23 +00:00
} catch (e) {
const { code, detail } = e.original;
// pg error codes https://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
if (code && (code.startsWith('22') || code.startsWith('23'))) {
const error = wrap(e, 406);
// detail tends to be more specific information. So, if we have it, use.
if (detail) {
error.message += `: ${detail}`;
error.reformat();
}
reply(error);
} else {
reply(wrap(e));
}
}
};
return descriptor;
};