Intercode SaaS Kit
  • Welcome to SaaS Starter Kit
  • Getting Started
    • Technology stack
    • Database Setup
    • Local Environment Setup
  • Basics
    • Dependencies
    • App architecture
    • Deployment
    • App roles
    • Endpoints List
      • Auth
      • Two Factor Auth
      • Businesses
      • Demo
      • Email
      • Export Document
      • Email Files
      • Files Demo
      • Leads
      • Orders
      • Payments
      • Subscriptions
      • Teams
      • Team Memberships
      • User Admin
  • Animation and Styles
    • Framer Motion
    • Ant Design and Styles
  • Pages
    • Auth
      • Working with PassportJS
      • Two-Factor Auth
      • OAuth Providers
    • Leads
    • Businesses
    • Team management
      • Ownership
    • Profile
    • User Settings
      • App Tour
    • App Settings
      • Lead Statuses
    • Dashboard
      • Lead volume widget
      • Doughnut chart widget
      • Recent leads table widget
      • Lead count over period widget
    • Demo
  • Features
    • Impersonation
    • Subscriptions (Stripe)
    • Search
    • Sentry
    • Captcha
    • Audit Logs
    • Internationalization
  • External integrations
    • Mailer
    • Google oAuth2
    • Facebook oAuth2
    • S3 compatible storage (AWS, MinIO)
Powered by GitBook
On this page
  • Create Sentry Project
  • API Setup
  1. Features

Sentry

This page explains how to set up Sentry debugging tools

PreviousSearchNextCaptcha

Last updated 6 months ago

Create Sentry Project

Step 1: Create a New Project on Sentry

  1. Go to and create new or log in to your account.

  2. In your dashboard, select Projects > Create Project.

  3. Choose NestJS as the platform.

  4. Name your project, assign it to a team, and click Create Project.

  5. After project creation, you’ll see a DSN (Data Source Name) key on the setup page. This DSN is essential for connecting your NestJS app to Sentry.

  6. Copy the DSN, as you’ll need it for environment variables.

API Setup

  1. Install dependencies

yarn add @sentry/nestjs @sentry/profiling-node
  1. Setup enviroment variables

.env
SENTRY_DNS=your_dsn
SENTRY_ENVIRONMENT=your_enviroment # 'development', 'production', etc.
constants.ts
SENTRY_DNS: process.env.SENTRY_DNS,
SENTRY_ENVIRONMENT: process.env.SENTRY_ENVIRONMENT,
  1. Initialize Sentry

In Sentry initialization file, you can set up Sentry with tracing and profiling, along with advanced configuration options to manage error handling more effectively.

instrument.ts
import * as Sentry from '@sentry/nestjs';
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import constants from './constants';
import { httpIntegration } from '@sentry/nestjs';

Sentry.init({
    dsn: constants.SENTRY_DNS,
    environment: constants.SENTRY_ENVIRONMENT,
    includeLocalVariables: true,
    integrations: [nodeProfilingIntegration(), httpIntegration()],
    tracesSampleRate: 0.1,
    profilesSampleRate: 0.1,
    beforeSend(event, hint) {
        const error = hint?.originalException;

        if (error && typeof error === 'object' && 'status' in error) {
            const statusCode = (error as any).status;

            if (statusCode >= 400 && statusCode < 500) {
                // Do not send 4xx errors to Sentry
                return null;
            }
        }

        return event;
    },
});
  1. Create Sentry filter

The Sentry filter allows you to add context to each error, making debugging more insightful by providing request details and user information without exposing sensitive data.

sentry.filter.ts
import { Catch, ArgumentsHost } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import * as Sentry from '@sentry/node';

@Catch()
export class SentryFilter extends BaseExceptionFilter {
    catch(exception: unknown, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const request = ctx.getRequest<Request>();

        Sentry.withScope(scope => {
            const headers = { ...request.headers };
            delete headers['authorization']; // Remove Authorization token

            scope.setExtras({
                method: request.method,
                url: request.url,
                headers: headers,
                user: request['user']
                    ? {
                          email: request['user']?.email,
                          roles: request['user']?.roles,
                          teamId: request['user']?.teamId,
                      }
                    : null,
            });

            Sentry.captureException(exception);
        });

        super.catch(exception, host);
    }
}
  1. Initialize filter

In your main application module, register the Sentry filter globally.

providers: [
    {
        provide: APP_FILTER,
        useClass: SentryGlobalFilter,
    },
],

After completing this steps you should be able to create test endopoint and catch your first error.

@Get("/debug-sentry")
getError() {
  throw new Error("My first Sentry error!");
}
Sentry