Express error tracking

Express error tracking in one line

Add one error handler after your routes and every server error in your Express app is reported with the route, the request and the trace — while your own error handlers keep working.

Set up

npm install @errorlenshq/node
server.js
import * as ErrorLens from "@errorlenshq/node";
ErrorLens.init({ dsn: process.env.ERRORLENS_DSN, tracesSampleRate: 0.2 });

import express from "express";
const app = express();

app.get("/orders/:id", getOrder);

// after all routes, before your own error handlers
ErrorLens.setupExpressErrorHandler(app);

app.listen(3000);

What you get

  • Only server errors are reported — a 404 or validation error passed to next(err) with a 4xx status is skipped
  • Transactions named after the full route, including routers: GET /api/orders/:id
  • Request method, URL, headers and query string; cookies and bodies only with sendDefaultPii
  • Each request isolated: ErrorLens.setUser() in one request never shows up on another

Fastify, Koa and Hapi

ErrorLens.setupFastifyErrorHandler(fastify);
ErrorLens.setupKoaErrorHandler(app);
await ErrorLens.setupHapiErrorHandler(server);

Frequently asked questions

Do I still need my own error middleware?

Keep it. The ErrorLens handler reports the error and passes it on with next(err), so your handler still decides the response.

Express 4 or 5?

Both are supported.

Start tracking errors in minutes

Free hosted plan with 5,000 errors a month, or self-host with no limits. No credit card.