combine.fm/lib/error-handler.js

39 lines
883 B
JavaScript
Raw Normal View History

2015-08-21 18:33:50 +01:00
import React from 'react';
2016-01-09 15:10:28 +00:00
import { renderPage } from './react-handler';
2015-08-21 18:33:50 +01:00
import debuglog from 'debug';
const debug = debuglog('match.audio');
export default function (routes) {
return function* (next) {
this.set('Server', 'Nintendo 64');
try {
yield next;
} catch (err) {
if (err.status === 404) {
2016-01-09 15:10:28 +00:00
this.body = yield renderPage(routes, this.request.url, {});
2015-08-21 18:33:50 +01:00
} else {
debug('Error: %o', err);
throw err;
}
}
if (404 != this.status) return;
switch (this.accepts('html', 'json')) {
case 'html':
this.type = 'html';
2016-01-09 15:10:28 +00:00
this.body = yield renderPage(routes, this.request.url, {});
2015-08-21 18:33:50 +01:00
break;
case 'json':
this.body = {
message: 'Page Not Found'
};
break;
default:
this.type = 'text';
this.body = 'Page Not Found';
}
}
}