Skip to main content

Introduction

After you complete the NestJS installation, unhandled exceptions are reported automatically. Inject LerixService to report handled exceptions yourself with extra context.

Automatic capture

LerixModule registers two things:
  • A global exception filter. Every exception thrown from a controller, guard, pipe or interceptor passes through it. By default it reports exceptions that are not an HttpException, and HttpExceptions with a status of 500 or above. Expected client errors such as NotFoundException or a failed ValidationPipe are not reported. The filter then hands the exception to Nest’s default filter, so the HTTP response is exactly what it would have been without Lerix.
  • Process-level handlers. uncaughtException and unhandledRejection are reported, the report is given up to flushTimeoutMs to leave, and the process exits with code 1 as Node would. Set exitOnUnhandled: false to keep the process alive, or captureUnhandled: false to skip the handlers.
Every automatic report carries the request method, URL, route, status code, and the x-request-id (or x-correlation-id) header when present, under metadata.request.

Change what is reported

shouldCapture receives the exception and a context object with the request details:
app.module.ts

Use your own filter

If your app already registers a catch-all APP_FILTER, set captureHttpExceptions: false and report from that filter:
all-exceptions.filter.ts
You can also extend LerixExceptionFilter and override catch to customise the response while keeping the reporting.

Report an error manually

orders.service.ts
captureException never throws and resolves once the report has been accepted or given up on. Use captureMessage for something that was not thrown:

Parameters

BugType values

BugSeverity values

What is attached to a report

TypeScript file and line numbers

Stack traces are mapped back to your TypeScript source automatically. When it reports an error, the SDK reads the .js.map files that nest build writes next to the compiled code, so a frame like dist/src/orders/orders.service.js:58 is reported as src/orders/orders.service.ts:42. There is nothing to upload, no Node.js flag and nothing to configure. Nest enables "sourceMap": true in tsconfig.json by default. Inline source maps and nest build --webpack bundles work too. Paths are made relative to the app’s working directory (normally the repository root), so they read like repository paths. Frames from dependencies show as node_modules/....
If the .js.map files are not deployed, frames keep their compiled position. This happens, for example, with a Docker image that copies only .js files. Keep the .js.map files next to dist/**/*.js in the image.

Other LerixService methods

Disable in tests

Pass enabled: false to turn every call into a no-op without changing your code: