combine.fm/lib/error-handler.js

41 lines
924 B
JavaScript
Raw Normal View History

2016-10-23 23:04:32 +01:00
import debuglog from 'debug';
2017-10-23 17:58:23 +01:00
const debug = debuglog('combine.fm');
2016-10-23 23:04:32 +01:00
2017-05-26 19:47:17 +01:00
export default function (raven) {
2016-10-23 23:04:32 +01:00
return function* error(next) {
this.set('Server', 'Nintendo 64');
try {
yield next;
} catch (err) {
if (err.status === 404) {
this.body = '404 Page Not Found';
} else if (err.status >= 400 && err.status < 500) {
this.status = err.status;
this.body = err.error;
} else {
debug('Error: %o', err);
2017-05-26 19:47:17 +01:00
raven.captureException(err);
2016-10-23 23:04:32 +01:00
throw err;
}
}
if (this.status !== 404) return;
switch (this.accepts('html', 'json')) {
case 'html':
this.type = 'html';
this.body = '404 Page Not Found';
break;
case 'json':
this.body = {
message: 'Page Not Found',
};
break;
default:
this.type = 'text';
this.body = 'Page Not Found';
}
};
}