Add error handling to the frontend

This commit is contained in:
Jonathan Cremin 2016-10-23 23:04:32 +01:00
parent b5cb3e78ab
commit 0a9ea15b58
6 changed files with 77 additions and 18 deletions

39
lib/error-handler.js Normal file
View file

@ -0,0 +1,39 @@
import debuglog from 'debug';
const debug = debuglog('match.audio');
export default function () {
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);
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';
}
};
}