Angular — Lescopr SDK

Integrate the Lescopr SDK into your Angular application for automatic error capture and real-time monitoring, from installation to configuration.

Introduction

The Lescopr SDK integrates with Angular by providing a custom ErrorHandler that replaces the default Angular error handler.

Every uncaught Angular error, unhandled promise rejection and explicit log call is captured and streamed in real-time to your Lescopr dashboard.

The package is available on npm and is compatible with Angular 14+.

Requirements

  • Node.js >= 16.0.0
  • Angular >= 14.0
  • A valid LESCOPR_SDK_KEY and LESCOPR_API_KEY

Installation

Install via your package manager:

Terminal
bash
npm install lescopr

Configuration

Initialise the SDK in your main.ts before bootstrapping the Angular application, then register the Lescopr ErrorHandler provider:

Add your keys to environment.ts:

Available options:

  • sdkKey — Your Lescopr SDK key (required)
  • apiKey — Your Lescopr API key (required)
  • environment — development (default) or production
src/main.ts
typescript
import lescopr from 'lescopr';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { appConfig } from './app/app.config';
import { ErrorHandler } from '@angular/core';

lescopr.init({
  sdkKey:      'YOUR_SDK_KEY',
  apiKey:      'YOUR_API_KEY',
  environment: 'production',
});

class LescoprErrorHandler implements ErrorHandler {
  handleError(error: unknown) {
    lescopr.captureException(error as Error);
    console.error(error);
  }
}

bootstrapApplication(AppComponent, {
  ...appConfig,
  providers: [
    ...appConfig.providers,
    { provide: ErrorHandler, useClass: LescoprErrorHandler },
  ],
});
src/environments/environment.ts
typescript
export const environment = {
  production: false,
  lescoprSdkKey: 'lsk_xxxx',
  lescoprApiKey: 'lak_xxxx',
};

Usage

Once the LescoprErrorHandler is registered, all uncaught Angular errors are captured automatically.

You can also inject the SDK and log events manually from any service or component:

src/app/order.service.ts
typescript
import { Injectable } from '@angular/core';
import lescopr from 'lescopr';

@Injectable({ providedIn: 'root' })
export class OrderService {
  async placeOrder(order: Order) {
    try {
      const result = await this.http.post('/api/orders', order).toPromise();
      lescopr.log('info', 'Order placed', { orderId: result.id });
      return result;
    } catch (err) {
      lescopr.captureException(err as Error);
      throw err;
    }
  }
}

Advanced

CLI initialisation

Run the CLI at the project root:

Environment variables

  • LESCOPR_SDK_KEY — SDK key
  • LESCOPR_API_KEY — API key
  • LESCOPR_ENVIRONMENT — development or production
  • LESCOPR_DEBUG=true — Verbose output to .lescopr.log
Terminal
bash
npx lescopr init --sdk-key YOUR_SDK_KEY