errorcore
Middleware

Express

In Express, install request context before your routes and capture errors in the last error middleware.

express middleware
import express from "express";
import errorcore from "errorcore";

const app = express();

app.use((req, _res, next) => {
  errorcore.withRequestContext({
    method: req.method,
    route: req.path,
    requestId: req.headers["x-request-id"],
  }, next);
});

app.use((error, req, res, next) => {
  errorcore.captureError(error, { request: { method: req.method, route: req.path } });
  next(error);
});

Keep the error middleware last. Earlier capture usually loses the final route or status context.

On this page