1.0.0 • Published 22 days ago

@okode/ngx-sentry v1.0.0

Weekly downloads
-
License
-
Repository
-
Last release
22 days ago

Sentry for Angular

Angular library to setup and configure Sentry projects easily on your applications.

Install

npm i @okode/ngx-sentry

Usage

Configure the Sentry Provider

All-in-one configuration:

import { provideSentry } from '@okode/ngx-sentry';

export const appConfig: ApplicationConfig = {
  providers: [
    ...
    provideSentry({
      dsn: 'YOUR_APP_DSN',
      enabled: true,
      release: '0.0.0',
      env: 'YOUR_APP_ENV', // <- dev, pre, pro
    }),
  ],
  ...
};

Fine-grained configuration by features:

  • provideSentryWithFeatures: allow to configure Sentry with modular features and your own custom features. ( API REFERENCE -> SentryFeatures)
import { provideSentryWithFeatures, withConfig, withDefaultErrorHandler } from '@okode/ngx-sentry';

export const appConfig: ApplicationConfig = {
  providers: [
    ...
    provideSentryWithFeatures(
        withConfig({
        dsn: 'YOUR_APP_DSN',
        enabled: true,
        release: '0.0.0',
        env: 'YOUR_APP_ENV', // <- dev, pre, pro
      }),
      withDefaultErrorHandler(),
      // Add any other custom feature here
    ),
  ],
  ...
};

Manual initialization

The default provideSentry() function automatically initializes Sentry on bootstrap. If you want to init Sentry on some other point, you'll have to use provideSentryWithFeatures() function and avoid using withConfig().

import { provideSentryWithFeatures, withDefaultHttpErrorInterceptor, withDefaultErrorHandler } from '@okode/ngx-sentry';

export const appConfig: ApplicationConfig = {
  providers: [
    ...
    provideSentryWithFeatures(
      withDefaultHttpErrorInterceptor(),
      withDefaultErrorHandler()
    ),
    ...
  ],
...
};

Now, in some other point on your app, you can init() the SentryErrorReporterService.

// some other place

import { SentryErrorReporterService } from '@okode/ngx-sentry';
import { version } from './package.json';

export class OtherPlaceService {

constructor(private sentryService: SentryErrorReporterService) {
      this.sentryService.init({
      dsn: 'YOUR_APP_DSN',
      enabled: true,
      release: '0.0.0',
      env: 'YOUR_APP_ENV', // <- dev, pre, pro
      release: version,
    });
  }
...
}

Send a custom error

Send a custom error whenever you need.

import { SentryErrorReporterService } from '@okode/ngx-sentry';

export class OtherPlaceService {
  constructor(private sentryService: SentryErrorReporterService) {}

  sendCustomErrorWithError(): void {
    this.sentryService.sendCustomError('MY_ERROR', new Error('PoC Error'));
  }
}

Integration with microfrontends - Monitoring microfrontend errors

Supported since version 0.18.0+.

If a Javascript error happens in a microfrontend feature, this is registered as an application error because this error cannot be identified as external error by default. In addition, Sentry doesn't show any kind of useful information about this error because it doesn't belong to the application project, the trace is minified because there are no source maps, etc.

This library is able to identify and redirect errors that reference its own Sentry project without an extra effort. So, what we have to do is to customize the microfrontend build and to add some metadata that allows application/library to identify this errors as microfrontend errors and redirect them to its own Sentry project.

Remember uploading microfrontend source maps to its Sentry project after it is built, otherwise you will still see the trace minified.

Official docs

API

Sentry Provider Functions

provideSentry(...)

provideSentry(config: SentryConfigResolver) => EnvironmentProviders

Sets a Sentry default setup provider.

ParamType
configSentryConfigResolver

provideSentryWithFeatures(...)

 provideSentryWithFeatures(...features: SentryFeature<SentryFeatureKind>[]) => EnvironmentProviders

Sets a Sentry setup provider with additional features.

ParamType
featuresSentryFeature[]

Sentry Features

withConfig(...)

withConfig(config: SentryConfigResolver) => SentryFeature<SentryFeatureKind.CONFIG>

Sets a custom SentryConfigRevolver and initializes Sentry.

ParamType
configSentryConfigResolver

withDefaultHttpErrorInterceptor()

withDefaultHttpErrorInterceptor() => SentryFeature<SentryFeatureKind.DEFAULT_HTTP_ERROR_INTERCEPTOR>

Provides the default HTTPErrorInterceptor service.


withDefaultErrorHandler()

withDefaultErrorHandler() => SentryFeature<SentryFeatureKind.DEFAULT_ERROR_HANDLER>

Provides the default ErrorHandler service.

makeSentryFeature(...)

makeSentryFeature<KindT extends SentryFeatureKind>(  kind: KindT,  providers: Provider[]) =>SentryFeature<KindT>

Creates a custom SentryFeature.

ParamType
kindKindT
providersProvider[]

SentryErrorReporterService

Sentry service that communication with Sentry.

init(...)

init(config: SentryConfig) => void

Initializes Sentry Service.

ParamType
configSentryConfig

sendError(...)

sendError(error: unknown) => void

Sends Error.

ParamType
errorunknown

sendServerError(...)

sendServerError(error: HttpErrorResponse, req: HttpRequest<unknown>) => void

Sends Server Error.

ParamType
errorHttpErrorResponse
reqHttpRequest

sendCustomError(...)

sendCustomError(errorCode: string, error?: unknown, level: 'debug' | 'warning' = 'debug') => void

Sends Custom Error.

ParamType
errorCodestring
errorunknown
level'debug' or 'warning'

setUserScope(...)

setUserScope(sentryUserScope: User) => void

Sets a User scope.

ParamType
sentryUserScopeUser

SentryReportErrorHttpInterceptor

intercept(...)

intercept(request: HttpRequest<unknown>, next: HttpHandler) => Observable<HttpEvent<unknown>>

Intercepts HTTP Errors.

ParamType
requestHttpRequest
nextHttpHandler

SentryErrorHandler

handleError(...)

handleError(error: unknow) => void

Handle thrown errors.

ParamType
errorunknown

Models

Sentry Config

ParamType
dsnstring
enabledboolean
envstring
releasestring
diststring
denyUrls(RegExp | string)[]
debugboolean
logErrorsboolean
tracesSampleRatenumber
ignoreErrors(RegExp | string)[]
beforeSend(event:ErrorEvent, hint:EventHint) => PromiseLike<Event | null> | Event | null
integrationsConfig{ browserTracing: { tracePropagationTargets?:string[];} }

Default denyUrls

  /extensions\//i,
  /^chrome:\/\//i,
  /safari-extension:/i,
  /ruxitagentjs/i,
  /googletagmanager/i,
  /pagead\/js/i,
  /\/(gtm|ga|analytics)\.js/i,
  /facebook\.com/i,
  /connect\.facebook\.net/i,
  /ubembed\.com/i,
  /cdn\.cookielaw\.org/i,
  /onetrust\.org/i,
  /googleapis\.com/i,
  /chatasistencia\/auraFW\//i,
  /embeddedService\/liveAgentStateChat\//i,
  /force\.com/i,
  /salesforceliveagent\.com/i,

User

ParamType
key: stringany
idstring | number
ip_addressstring
emailstring
usernamestring
segmentstring

SentryFeature

ParamType
ekindKindT
eprovidersProvider[]

SentryFeatureKind

ENUM
CONFIG
DEFAULT_HTTP_ERROR_INTERCEPTOR
DEFAULT_ERROR_HANDLER

FAQ

How to customize any error tracked in Sentry?

In the Sentry library, most use cases for error monitoring are covered, but it's always possible that additional aspects need to be implemented for specific project needs. For this reason, the Sentry library is extensible and can be customized.

To achieve this, you need to create a new service in your application, for example, sentry-reporter-service-ext.service.ts. This service should extend the class you want to modify.

In the providers of your application, you need to indicate that the new service created will replace the Sentry service from the library as follows:

{ provide: SentryErrorReporterService, useExisting: SentryErrorReporterServiceExt }

To customize error monitoring according to your specific requirements or to add new features, you'll need to use the @override annotation to replace or extend the functionality of the existing functions.

export class SentryErrorReporterServiceExt extends SentryErrorReporterService {

  constructor(
    @Inject(INJECTOR) injector: Injector,
    @Inject(PLATFORM_ID) platformId: string,
    @Inject(DOCUMENT) document: Document,
  ) {
    super(injector, platformId, document);
  }

  override sendError(error: unknown) {
    // [Your custom implementation]
  }
}
1.0.0

22 days ago

0.18.0

4 months ago

0.18.0-beta.0

4 months ago

0.17.11

5 months ago

0.17.10

5 months ago

0.17.2

7 months ago

0.17.3

7 months ago

0.17.4

7 months ago

0.17.6

7 months ago

0.17.7

7 months ago

0.17.8

7 months ago

0.17.9

7 months ago

0.11.0

9 months ago

0.12.0

9 months ago

0.13.0

8 months ago

0.13.1

8 months ago

0.15.0

8 months ago

0.17.1

8 months ago

0.15.3

8 months ago

0.10.0

7 months ago

0.9.0

10 months ago

0.7.2

10 months ago

0.8.0

10 months ago

0.7.1

10 months ago

0.5.0

10 months ago

0.7.0

10 months ago

0.6.0

10 months ago

0.4.0

11 months ago

0.3.0

11 months ago

0.2.0

12 months ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago