@map-colonies/error-express-handler

error-express-handler

This package exports a middleware for express that handles Error and HttpError(custom package type).

npm install --save @map-colonies/error-express-handler

Check the autogenerated documentation here.

Add the handler as the last middleware, so any error forwarded by the controllers will be handled. If the value of NODE_ENV is production the error message will be redacted and a generic one will be sent instead. In development, in addition to the error message, the stack trace of the error will be included in the response.

import express from 'express';
import { getErrorHandlerMiddleware } from '@map-colonies/error-express-handler';

process.env.NODE_ENV = 'development';

const app = express();

app.use('/meow', fn);

app.use(getErrorHandlerMiddleware());

app.listen(8080, function() {
console.log('server is up');
});
flowchart TD
    A@{ label: "NODE_ENV='production'" } -- true --> B["error implements HttpError"]
    A -- false --> C["error implements HttpError"]
    B -- true --> D["Status code: err.StatusCode
body: {fl°°34¶ßmessagefl°°34¶ß:fl°°34¶ßerr.messagefl°°34¶ß}"] B -- false --> E["Status code: 500
body: {fl°°34¶ßmessagefl°°34¶ß:fl°°34¶ßInternal Server Errorfl°°34¶ß}"] C -- true --> F["Status code: err.StatusCode
body: {fl°°34¶ßmessagefl°°34¶ß:fl°°34¶ßerr.messagefl°°34¶ß}"] C -- false --> G["Status code: 500
body: {fl°°34¶ßmessagefl°°34¶ß:fl°°34¶ßInternal Server Errorfl°°34¶ß, fl°°34stacktracefl°°34:err.stack }"]