sentry.service.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { Inject, Injectable } from '@nestjs/common';
  2. import { CaptureContext, StartSpanOptions } from '@sentry/core';
  3. import * as Sentry from '@sentry/node';
  4. import { SENTRY_PLUGIN_OPTIONS } from './constants';
  5. import { SentryPluginOptions } from './types';
  6. /**
  7. * @description
  8. * Service for capturing errors and messages to Sentry.
  9. * @docsCategory core plugins/SentryPlugin
  10. */
  11. @Injectable()
  12. export class SentryService {
  13. constructor(@Inject(SENTRY_PLUGIN_OPTIONS) private options: SentryPluginOptions) {}
  14. captureException(exception: Error) {
  15. Sentry.captureException(exception);
  16. }
  17. /**
  18. * @description
  19. * Captures a message
  20. * @param message - The message to capture
  21. * @param captureContext - The capture context
  22. */
  23. captureMessage(message: string, captureContext?: CaptureContext) {
  24. Sentry.captureMessage(message, captureContext);
  25. }
  26. /**
  27. * @description
  28. * Starts new span
  29. */
  30. startSpan(context: StartSpanOptions) {
  31. return Sentry.startSpanManual(context, span => span);
  32. }
  33. }