Add: Better errors #15

Merged
joeybaker merged 1 commits from better-errors into master 2016-08-04 06:07:29 +00:00

View File

@ -1,3 +1,5 @@
import { wrap } from 'boom';
export default (target, key, descriptor) => {
const fn = descriptor.value;
@ -5,8 +7,22 @@ export default (target, key, descriptor) => {
try {
await fn(request, reply);
} catch (e) {
console.error(e);
reply(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));
}
}
};