mirror of
https://github.com/ascribe/onion.git
synced 2024-12-23 01:39:36 +01:00
Implement boilerplate logic for ReactError
This commit is contained in:
parent
5a6c827f0b
commit
68f41f7550
14
js/app.js
14
js/app.js
@ -5,6 +5,8 @@ require('babel/polyfill');
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Router, Redirect } from 'react-router';
|
import { Router, Redirect } from 'react-router';
|
||||||
import history from './history';
|
import history from './history';
|
||||||
|
import { ReactError, ResourceNotFoundError } from './react_error.js';
|
||||||
|
import { ResourceNotFoundErrorHandler } from './react_error_handlers';
|
||||||
|
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
import fetch from 'isomorphic-fetch';
|
import fetch from 'isomorphic-fetch';
|
||||||
@ -91,12 +93,20 @@ class AppGateway {
|
|||||||
// us in that case.
|
// us in that case.
|
||||||
history.listen(EventActions.routeDidChange);
|
history.listen(EventActions.routeDidChange);
|
||||||
|
|
||||||
React.render((
|
ReactError({
|
||||||
|
render: React.render,
|
||||||
|
params: [(
|
||||||
<Router history={history}>
|
<Router history={history}>
|
||||||
{redirectRoute}
|
{redirectRoute}
|
||||||
{getRoutes(type, subdomain)}
|
{getRoutes(type, subdomain)}
|
||||||
</Router>
|
</Router>
|
||||||
), document.getElementById('main'));
|
),
|
||||||
|
document.getElementById('main')],
|
||||||
|
errors: [{
|
||||||
|
error: ResourceNotFoundError,
|
||||||
|
handler: ResourceNotFoundErrorHandler
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
|
||||||
// Send the applicationDidBoot event to the third-party stores
|
// Send the applicationDidBoot event to the third-party stores
|
||||||
EventActions.applicationDidBoot(settings);
|
EventActions.applicationDidBoot(settings);
|
||||||
|
42
js/react_error.js
Normal file
42
js/react_error.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
function _transformErrorsListToHashMap(errors) {
|
||||||
|
return errors.reduce((errObj, { error, handler }) => {
|
||||||
|
const errorName = error.name;
|
||||||
|
if(!errObj[errorName]) {
|
||||||
|
errObj[errorName] = {
|
||||||
|
error: error,
|
||||||
|
handler: handler
|
||||||
|
};
|
||||||
|
return errObj;
|
||||||
|
} else {
|
||||||
|
throw new Error('You\'re trying to override the error handler for ' + errorName + ' by specifying it twice.');
|
||||||
|
}
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ReactError({ render, params, errors }) {
|
||||||
|
errors = _transformErrorsListToHashMap(errors);
|
||||||
|
try {
|
||||||
|
// use react's render function + parameters to render
|
||||||
|
// the application
|
||||||
|
render(...params);
|
||||||
|
} catch(err) {
|
||||||
|
const potErrorRoutine = errors[err.name];
|
||||||
|
if(potErrorRoutine) {
|
||||||
|
potErrorRoutine.handler(err);
|
||||||
|
} else {
|
||||||
|
console.error(err.stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Followed: http://stackoverflow.com/a/32749533/1263876 for extending errors
|
||||||
|
export class ResourceNotFoundError extends Error {
|
||||||
|
constructor(message) {
|
||||||
|
super(message);
|
||||||
|
this.name = this.constructor.name;
|
||||||
|
this.message = message;
|
||||||
|
Error.captureStackTrace(this, this.constructor.name);
|
||||||
|
}
|
||||||
|
}
|
5
js/react_error_handlers.js
Normal file
5
js/react_error_handlers.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
export function ResourceNotFoundErrorHandler() {
|
||||||
|
console.log(arguments);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user