UNPKG

@metamask/error-reporting-service

Version:

Logs errors to an error reporting service such as Sentry

163 lines 5.38 kB
import type { Messenger } from "@metamask/messenger"; /** * The action which can be used to report an error. * * @deprecated This action is deprecated and will be removed in a future * release. Please use `Messenger.captureException` directly instead. */ export type ErrorReportingServiceCaptureExceptionAction = { type: 'ErrorReportingService:captureException'; handler: ErrorReportingService['captureException']; }; /** * All actions that {@link ErrorReportingService} registers so that other * modules can call them. */ export type ErrorReportingServiceActions = ErrorReportingServiceCaptureExceptionAction; /** * All events that {@link ErrorReportingService} publishes so that other modules * can subscribe to them. */ export type ErrorReportingServiceEvents = never; /** * All actions registered by other modules that {@link ErrorReportingService} * calls. */ type AllowedActions = never; /** * All events published by other modules that {@link ErrorReportingService} * subscribes to. */ type AllowedEvents = never; /** * The messenger restricted to actions and events that * {@link ErrorReportingService} needs to access. */ export type ErrorReportingServiceMessenger = Messenger<'ErrorReportingService', ErrorReportingServiceActions | AllowedActions, ErrorReportingServiceEvents | AllowedEvents>; /** * The options that {@link ErrorReportingService} takes. */ type ErrorReportingServiceOptions = { captureException: ErrorReportingService['captureException']; messenger: ErrorReportingServiceMessenger; }; /** * `ErrorReportingService` is designed to log an error to an error reporting app * such as Sentry, but in an agnostic fashion. * * @example * * In this example, we have a controller, and something bad happens, but we want * to report an error instead of throwing it. * * ``` ts * // === Controller file === * * import type { ErrorReportingServiceCaptureExceptionAction } from '@metamask/error-reporting-service'; * * // Define the messenger type for the controller. * type AllowedActions = ErrorReportingServiceCaptureExceptionAction; * type ExampleControllerMessenger = Messenger< * 'ExampleController', * AllowedActions, * never, * >; * * // Define the controller. * class ExampleController extends BaseController< * 'ExampleController', * ExampleControllerState, * ExampleControllerMessenger * > { * doSomething() { * // Imagine that we do something that produces an error and we want to * // report the error. * this.messenger.call( * 'ErrorReportingService:captureException', * new Error('Something went wrong'), * ); * } * } * * // === Initialization file === * * import { captureException } from '@sentry/browser'; * import { ErrorReportingService } from '@metamask/error-reporting-service'; * import { ExampleController } from './example-controller'; * * type RootMessenger = Messenger< * 'Root', * MessengerActions<ErrorReportingServiceMessenger>, * MessengerEvents<ErrorReportingServiceMessenger> * >; * * // Create a root messenger. * const rootMessenger = new Messenger(); * * // Register handler for the `ErrorReportingService:captureException` * // action in the root messenger. * const errorReportingServiceMessenger = new Messenger< * 'ErrorReportingService', * MessengerActions<ErrorReportingServiceMessenger>, * MessengerEvents<ErrorReportingServiceMessenger>, * RootMessenger * >({ * namespace: 'ErrorReportingService', * parent: rootMessenger, * }); * const errorReportingService = new ErrorReportingService({ * messenger: errorReportingServiceMessenger, * captureException, * }); * * const exampleControllerMessenger = new Messenger< * 'ExampleController', * MessengerActions<ExampleControllerMessenger>, * MessengerEvents<ExampleControllerMessenger>, * RootMessenger * >({ * namespace: 'ExampleController', * parent: rootMessenger, * }); * rootMessenger.delegate({ * messenger: exampleControllerMessenger, * actions: ['ErrorReportingService:captureException'], * }); * const exampleController = new ExampleController({ * messenger: exampleControllerMessenger, * }); * * // === Somewhere else === * * // Now this will report an error without throwing it. * exampleController.doSomething(); * ``` * * @deprecated This service is deprecated and will be removed in a future * release. Please use `Messenger.captureException` directly instead. */ export declare class ErrorReportingService { #private; name: 'ErrorReportingService'; state: null; /** * Constructs a new ErrorReportingService. * * @param options - The options. * @param options.messenger - The messenger suited to this * ErrorReportingService. * @param options.captureException - A function that stores the given error in * the error reporting service. */ constructor({ messenger, captureException }: ErrorReportingServiceOptions); /** * Reports the given error to an external location. * * @param error - The error to report. * @deprecated This function is deprecated and will be removed in a future * release. Please use `Messenger.captureException` directly instead. */ captureException(error: Error): void; } export {}; //# sourceMappingURL=error-reporting-service.d.mts.map