Collects browser errors, server request errors, and uncaught exceptions, and provides an API for consuming them.
yarn add fusion-plugin-error-handling
// src/monitoring.js
import {createPlugin} from 'fusion-core';
export default __NODE__ && createPlugin({
provides() {
return (e, captureType) => {
if (captureType === 'browser') {
const {message, source, line, col, error} = e;
console.log({message, source, line, col, error});
} else if (captureType === 'server') {
console.log('UNCAUGHT EXCEPTION', e);
} else if (captureType === 'request') {
console.log('REQUEST ERROR');
}
}
}
}
});
Normally, instead of using console
, you would consume errors via something like Kafka or at least use a production logger (such as fusion-plugin-universal-logger
) in conjunction with a logging service such as LogEntries or Papertrail.
// src/main.js
import React from 'react';
import App from 'fusion-react';
import ErrorHandling, {ErrorHandlerToken} from 'fusion-plugin-error-handling';
import log from './monitoring';
export default () => {
const app = new App(<div />);
if (__NODE__) {
app.register(ErrorHandlerToken, log);
app.register(ErrorHandling);
}
return app;
};
ErrorHandling
import ErrorHandling from 'fusion-plugin-error-handling';
The plugin. Typically doesn't need to be associated with a token.
ErrorHandlerToken
import {ErrorHandlerToken} from 'fusion-plugin-error-handling';
A function to be called when an error is reported.
type ErrorHandler = (e: Error, captureType: string) => Promise
If the error is a global uncaught exception or unhandled rejection, the process exits when the returned Promise resolves/rejects.
e: Error
- The error that was reportedcaptureType: string
- Either 'browser', 'uncaught' or 'request'.Promise