Refactor more, fix and design 404
This commit is contained in:
parent
7de374e00b
commit
8521baa6d9
11 changed files with 223 additions and 138 deletions
48
lib/error-handler.js
Normal file
48
lib/error-handler.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
import React from 'react';
|
||||
import createHandler from './react-handler';
|
||||
|
||||
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) {
|
||||
let Handler = yield createHandler(routes, this.request.url);
|
||||
|
||||
let App = React.createFactory(Handler);
|
||||
let content = React.renderToString(new App());
|
||||
|
||||
this.body = '<!doctype html>\n' + content;
|
||||
} else {
|
||||
debug('Error: %o', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
if (404 != this.status) return;
|
||||
|
||||
switch (this.accepts('html', 'json')) {
|
||||
case 'html':
|
||||
this.type = 'html';
|
||||
let Handler = yield createHandler(routes, this.request.url);
|
||||
|
||||
let App = React.createFactory(Handler);
|
||||
let content = React.renderToString(new App());
|
||||
|
||||
this.body = '<!doctype html>\n' + content;
|
||||
break;
|
||||
case 'json':
|
||||
this.body = {
|
||||
message: 'Page Not Found'
|
||||
};
|
||||
break;
|
||||
default:
|
||||
this.type = 'text';
|
||||
this.body = 'Page Not Found';
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue