All files / lib/services sentry.service.ts

84.62% Statements 33/39
62.5% Branches 5/8
66.67% Functions 6/9
91.18% Lines 31/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 863x   3x   3x     3x     3x 20x     20x     20x 20x   2x     18x                                       2x 2x 2x 2x         1x 1x 1x 1x         1x 1x 1x 1x         1x 1x 1x 1x         1x 1x 1x 1x              
import { Inject, Injectable, Logger, Optional } from '@nestjs/common';
import { Options, Client } from '@sentry/types';
import * as Sentry from '@sentry/node';
 
import { SENTRY_MODULE_OPTIONS } from '../common/sentry.constants';
import { SentryModuleOptions } from '../interfaces/sentry-options.interface';
 
export abstract class SentryBaseService extends Logger {}
 
@Injectable()
export class SentryService extends Logger {
    app: string = '@ntegral/nestjs-sentry: ';
    constructor(
        @Inject(SENTRY_MODULE_OPTIONS)
        private readonly options?: SentryModuleOptions,
        @Optional() prior?: SentryService
      ) {
        super();
        if (!(options && options.dsn)) {
          // console.log('options not found. Did you use SentryModule.forRoot?');
          return;
        }
 
        Sentry.init({
          dsn: options.dsn,
          debug: options.debug === true ? false : options.debug,
          environment: options.environment,
          release: options.release,
          logLevel: options.logLevel,
          integrations: [
            new Sentry.Integrations.OnUncaughtException({
              onFatalError: (async (err) => {
                // console.error('uncaughtException, not cool!')
                // console.error(err);
                if (err.name === 'SentryError') { console.log(err); } else { (Sentry.getCurrentHub().getClient<Client<Options>>() as Client<Options>).captureException(err); process.exit(1); }
              }),
            }),
            new Sentry.Integrations.OnUnhandledRejection({mode: 'warn'})
          ]
        });
      }
 
  log(message: string, context?: string) {
    message = `${this.app} ${message}`;
    try {
      Sentry.captureMessage(message, Sentry.Severity.Log);
      super.log(message, context);
    } catch (err) {  }
  }
 
  error(message: string, trace?: string, context?: string) {
    message = `${this.app} ${message}`;
    try {
      Sentry.captureMessage(message, Sentry.Severity.Error);
      super.error(message, trace, context);
    } catch (err) {  }
  }
 
  warn(message: string, context?: string) {
    message = `${this.app} ${message}`;
    try {
      Sentry.captureMessage(message, Sentry.Severity.Warning);
      super.warn(message, context);
    } catch (err) {  }
  }
 
  debug(message: string, context?: string) {
    message = `${this.app} ${message}`;
    try {
      Sentry.captureMessage(message, Sentry.Severity.Debug);
      super.debug(message, context);
    } catch (err) {  }
  }
 
  verbose(message: string, context?: string) {
    message = `${this.app} ${message}`;
    try {
      Sentry.captureMessage(message, Sentry.Severity.Info);
      super.verbose(message, context);
    } catch (err) {  }
  }
 
  instance() {
    return Sentry;
  }
}