2016-08-03 21:42:20 +00:00
|
|
|
import { wrap } from 'boom';
|
|
|
|
|
2016-01-18 14:38:43 +00:00
|
|
|
export default (target, key, descriptor) => {
|
2016-07-09 05:11:23 +00:00
|
|
|
const fn = descriptor.value;
|
2016-01-18 14:38:43 +00:00
|
|
|
|
2016-01-19 06:33:29 +00:00
|
|
|
descriptor.value = async (request, reply) => {
|
2016-01-18 14:38:43 +00:00
|
|
|
try {
|
2016-01-19 06:33:29 +00:00
|
|
|
await fn(request, reply);
|
2016-07-09 05:11:23 +00:00
|
|
|
} catch (e) {
|
2016-08-03 21:42:20 +00:00
|
|
|
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));
|
|
|
|
}
|
2016-01-18 14:38:43 +00:00
|
|
|
}
|
2016-06-30 13:58:02 +00:00
|
|
|
};
|
2016-01-18 14:38:43 +00:00
|
|
|
|
|
|
|
return descriptor;
|
2016-06-30 13:58:02 +00:00
|
|
|
};
|