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_KEYandLESCOPR_API_KEY
Installation
Install via your package manager:
npm install lescoprConfiguration
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) orproduction
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 },
],
});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:
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 keyLESCOPR_API_KEY— API keyLESCOPR_ENVIRONMENT—developmentorproductionLESCOPR_DEBUG=true— Verbose output to.lescopr.log
npx lescopr init --sdk-key YOUR_SDK_KEY