Connect NestJS to Bugsink
When a NestJS request fails unexpectedly, Bugsink shows you the exception, the application frame that raised it, and the HTTP request that reached the controller.
This guide adds the NestJS SDK and its global exception filter, then sends one failed request through a controller and service. To follow along, create a Bugsink project and copy its DSN. The quickstart guide shows where to find it.
Install the NestJS SDK
Install the open-source Sentry SDK for NestJS. Bugsink accepts the Sentry event protocol, so the SDK can send exceptions directly to your Bugsink project:
npm install @sentry/nestjsnpm install @sentry/nestjs
Initialize the SDK first
Create src/instrument.ts and initialize the SDK with the DSN from the
environment:
import * as Sentry from "@sentry/nestjs"; Sentry.init({ dsn: process.env.SENTRY_DSN, });import * as Sentry from "@sentry/nestjs"; Sentry.init({ dsn: process.env.SENTRY_DSN, });
Import this file at the very top of src/main.ts, before NestJS or your root
module. The order matters because the SDK instruments NestJS while its modules
load:
import "./instrument"; import "reflect-metadata";import "./instrument"; import "reflect-metadata";
Set SENTRY_DSN in the environment where the application runs:
export SENTRY_DSN=https://key@bugsink.example/1export SENTRY_DSN=https://key@bugsink.example/1
Add the NestJS filter
Import SentryModule and SentryGlobalFilter in your root module. The module
adds the NestJS request integration, while the filter reports unexpected
exceptions from the framework’s normal error path:
import { APP_FILTER } from "@nestjs/core"; import { SentryGlobalFilter, SentryModule } from "@sentry/nestjs/setup";import { APP_FILTER } from "@nestjs/core"; import { SentryGlobalFilter, SentryModule } from "@sentry/nestjs/setup";
Add the module and register the filter as an APP_FILTER provider:
@Module({ imports: [SentryModule.forRoot()], controllers: [InventoryController], providers: [ { provide: APP_FILTER, useClass: SentryGlobalFilter, }, InventoryService, ], }) export class AppModule {}@Module({ imports: [SentryModule.forRoot()], controllers: [InventoryController], providers: [ { provide: APP_FILTER, useClass: SentryGlobalFilter, }, InventoryService, ], }) export class AppModule {}
If your application already has a catch-all exception filter, add the SDK’s
@WithSentry() decorator to that filter’s catch() method instead. An error
must reach one SDK-enabled filter before another filter consumes it.
Send a failed request
Your controllers and services do not need reporting calls. This inventory service throws an ordinary application error when the requested product does not exist:
reserve(sku: string): { reserved: string } { const product = this.products.get(sku); if (!product) { throw new ProductNotFound(sku); } return { reserved: product.name }; }reserve(sku: string): { reserved: string } { const product = this.products.get(sku); if (!product) { throw new ProductNotFound(sku); } return { reserved: product.name }; }
Build and start the application, then request a reservation for a missing product:
npm run build NODE_OPTIONS=--enable-source-maps npm start :::bash curl -X POST http://127.0.0.1:3000/inventory/nest-missing/reservationsnpm run build NODE_OPTIONS=--enable-source-maps npm start :::bash curl -X POST http://127.0.0.1:3000/inventory/nest-missing/reservations
NestJS still returns its normal HTTP 500 response:
{"statusCode":500,"message":"Internal server error"}{"statusCode":500,"message":"Internal server error"}
The NODE_OPTIONS setting makes the captured stack point back to the
TypeScript service instead of the generated JavaScript.
See the request in Bugsink
Open your Bugsink project and select the new ProductNotFound issue. Its event
shows the failed POST request, the parameterized NestJS route, and the line in
the service which raised the exception.
ProductNotFound
That’s it: unexpected exceptions reaching NestJS’s global error path will now be reported to Bugsink, without changing the response your API returns.
Limitations
NestJS’s own HttpException classes are treated as expected control-flow
errors by the SDK, regardless of their status code. A BadRequestException or
InternalServerErrorException is therefore not reported automatically; call
captureException() explicitly when one of those exceptions should create an
issue.
With NestJS 12.5.1, registering SentryGlobalFilter also produces a startup
warning that the filter has no decorator metadata for its optional constructor
argument. Despite the warning, the filter still captures the error and NestJS
returns its normal response; the warning is an upstream integration rough
edge.
For handled exceptions, messages, tags, structured context, breadcrumbs, and other SDK options, continue with the broader JavaScript guide.
