Star us on GitHub
Star
Menu

Nest.js Error Monitoring

Learn how to set up highlight.io in Nest.js.
1
Add `tracingOrigins` to your client Highlight snippet.

This backend SDK requires one of the Highlight frontend SDKs to be installed, so please make sure you've followed the fullstack mapping guide first.

H.init("<YOUR_PROJECT_ID>", { tracingOrigins: ['localhost', 'example.myapp.com/backend'], networkRecording: { enabled: true, recordHeadersAndBody: true, }, });
Copy
2
Install the relevant Highlight SDK(s).

Install @highlight-run/nest with your package manager.

npm install --save @highlight-run/nest
Copy
3
Add the @highlight-run/nest app middleware.

Use the HighlightErrorFilter middleware to capture backend errors.

import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { HighlightInterceptor, H } from '@highlight-run/nest'; const env = { projectID: '<YOUR_PROJECT_ID>', serviceName: 'my-nestjs-app', serviceVersion: 'git-sha', environment: 'production', debug: false, }; async function bootstrap() { H.init(env); const app = await NestFactory.create(AppModule); app.useGlobalInterceptors(new HighlightInterceptor(env)); await app.listen(3000); } bootstrap();
Copy
4
Optionally, report manual errors in your app.

If you need to report exceptions outside of a handler, use the Highlight SDK.

const parsed = H.parseHeaders(request.headers) H.consumeError(error, parsed?.secureSessionId, parsed?.requestId)
Copy
5
Verify that your SDK is reporting errors.

You'll want to throw an exception in one of your Nest.js handlers. Access the API handler and make sure the error shows up in Highlight.

import { Injectable } from '@nestjs/common' @Injectable() export class AppService { getHello(): string { console.log('hello, world!') console.warn('whoa there! ', Math.random()) if (Math.random() < 0.2) { // error will be caught by the HighlightErrorFilter throw new Error(`a random error occurred! 0.23717540028976725`) } return 'Hello World!' } }
Copy
6
Set up logging.

Start sending logs to Highlight! Follow the logging setup guide to get started.