@dotcom-reliability-kit/middleware-render-error-info
v5.1.10
Published
Express middleware to render error information in a way that makes local debugging easier and production error rendering more consistent.
Downloads
1,341
Keywords
Readme
@dotcom-reliability-kit/middleware-render-error-info
Express middleware to render error information in a browser in a way that makes local debugging easier and production error rendering more consistent. This module is part of FT.com Reliability Kit.
Usage
Install @dotcom-reliability-kit/middleware-render-error-info
as a dependency:
npm install --save @dotcom-reliability-kit/middleware-render-error-info
Include in your code:
import renderErrorInfoPage from '@dotcom-reliability-kit/middleware-render-error-info';
// or
const renderErrorInfoPage = require('@dotcom-reliability-kit/middleware-render-error-info');
renderErrorInfoPage
The renderErrorInfoPage
function can be used to generate Express middleware which renders an error debugging page in local development and a sensible stripped-back error page in production.
When the NODE_ENV
environment variable is either empty or set to "development"
then a full debug page will be rendered. Otherwise only the error status code and message will be output, e.g. 500 Server Error
. This ensures that we don't leak important error information in production.
[!CAUTION] This middleware must be added to your Express app after all your application routes – you won't get rendered errors for any routes which are mounted after this middleware.
const app = express();
// App routes go here
app.use(renderErrorInfoPage());
[!CAUTION] If you're using @dotcom-reliability-kit/middleware-log-errors in your app, it's best to mount the error page middleware after the logging middleware. Otherwise the error will never be logged.
Once you've mounted the middleware, if you're working locally you should now see a detailed error page when you encounter an error in your app (assuming you're relying on the Express error handler to serve errors):
Debug headers
As well as rendering an error page, the middleware also sends an error-fingerprint
HTTP header in the response. This contains the error fingerprint and is available in development and production. Inspecting this header on a generic error page can help identify the root cause of an issue.
Migrating
Consult the Migration Guide if you're trying to migrate to a later major version of this package.
Contributing
See the central contributing guide for Reliability Kit.
License
Licensed under the MIT license. Copyright © 2022, The Financial Times Ltd.