UNPKG

@metamask/error-reporting-service

Version:

Logs errors to an error reporting service such as Sentry

1 lines 6.64 kB
{"version":3,"file":"error-reporting-service.mjs","sourceRoot":"","sources":["../src/error-reporting-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAwDA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8FG;AACH,MAAM,OAAO,qBAAqB;IAShC;;;;;;;;OAQG;IACH,YAAY,EAAE,SAAS,EAAE,gBAAgB,EAAgC;QAjBzE,SAAI,GAA4B,uBAAgC,CAAC;QAEjE,UAAK,GAAG,IAAI,CAAC;QAEJ,0DAAoE;QAEpE,mDAA2C;QAYlD,uBAAA,IAAI,oCAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,2CAAqB,gBAAgB,MAAA,CAAC;QAE1C,uBAAA,IAAI,wCAAW,CAAC,qBAAqB,CACnC,wCAAwC,EACxC,uBAAA,IAAI,+CAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,CAClC,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACH,gBAAgB,CAAC,KAAY;QAC3B,uBAAA,IAAI,+CAAkB,MAAtB,IAAI,EAAmB,KAAK,CAAC,CAAC;IAChC,CAAC;CACF","sourcesContent":["import type { Messenger } from '@metamask/messenger';\n\n/**\n * The action which can be used to report an error.\n *\n * @deprecated This action is deprecated and will be removed in a future\n * release. Please use `Messenger.captureException` directly instead.\n */\nexport type ErrorReportingServiceCaptureExceptionAction = {\n type: 'ErrorReportingService:captureException';\n handler: ErrorReportingService['captureException'];\n};\n\n/**\n * All actions that {@link ErrorReportingService} registers so that other\n * modules can call them.\n */\nexport type ErrorReportingServiceActions =\n ErrorReportingServiceCaptureExceptionAction;\n\n/**\n * All events that {@link ErrorReportingService} publishes so that other modules\n * can subscribe to them.\n */\nexport type ErrorReportingServiceEvents = never;\n\n/**\n * All actions registered by other modules that {@link ErrorReportingService}\n * calls.\n */\ntype AllowedActions = never;\n\n/**\n * All events published by other modules that {@link ErrorReportingService}\n * subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events that\n * {@link ErrorReportingService} needs to access.\n */\nexport type ErrorReportingServiceMessenger = Messenger<\n 'ErrorReportingService',\n ErrorReportingServiceActions | AllowedActions,\n ErrorReportingServiceEvents | AllowedEvents\n>;\n\n/**\n * The options that {@link ErrorReportingService} takes.\n */\ntype ErrorReportingServiceOptions = {\n captureException: ErrorReportingService['captureException'];\n messenger: ErrorReportingServiceMessenger;\n};\n\n/**\n * `ErrorReportingService` is designed to log an error to an error reporting app\n * such as Sentry, but in an agnostic fashion.\n *\n * @example\n *\n * In this example, we have a controller, and something bad happens, but we want\n * to report an error instead of throwing it.\n *\n * ``` ts\n * // === Controller file ===\n *\n * import type { ErrorReportingServiceCaptureExceptionAction } from '@metamask/error-reporting-service';\n *\n * // Define the messenger type for the controller.\n * type AllowedActions = ErrorReportingServiceCaptureExceptionAction;\n * type ExampleControllerMessenger = Messenger<\n * 'ExampleController',\n * AllowedActions,\n * never,\n * >;\n *\n * // Define the controller.\n * class ExampleController extends BaseController<\n * 'ExampleController',\n * ExampleControllerState,\n * ExampleControllerMessenger\n * > {\n * doSomething() {\n * // Imagine that we do something that produces an error and we want to\n * // report the error.\n * this.messenger.call(\n * 'ErrorReportingService:captureException',\n * new Error('Something went wrong'),\n * );\n * }\n * }\n *\n * // === Initialization file ===\n *\n * import { captureException } from '@sentry/browser';\n * import { ErrorReportingService } from '@metamask/error-reporting-service';\n * import { ExampleController } from './example-controller';\n *\n * type RootMessenger = Messenger<\n * 'Root',\n * MessengerActions<ErrorReportingServiceMessenger>,\n * MessengerEvents<ErrorReportingServiceMessenger>\n * >;\n *\n * // Create a root messenger.\n * const rootMessenger = new Messenger();\n *\n * // Register handler for the `ErrorReportingService:captureException`\n * // action in the root messenger.\n * const errorReportingServiceMessenger = new Messenger<\n * 'ErrorReportingService',\n * MessengerActions<ErrorReportingServiceMessenger>,\n * MessengerEvents<ErrorReportingServiceMessenger>,\n * RootMessenger\n * >({\n * namespace: 'ErrorReportingService',\n * parent: rootMessenger,\n * });\n * const errorReportingService = new ErrorReportingService({\n * messenger: errorReportingServiceMessenger,\n * captureException,\n * });\n *\n * const exampleControllerMessenger = new Messenger<\n * 'ExampleController',\n * MessengerActions<ExampleControllerMessenger>,\n * MessengerEvents<ExampleControllerMessenger>,\n * RootMessenger\n * >({\n * namespace: 'ExampleController',\n * parent: rootMessenger,\n * });\n * rootMessenger.delegate({\n * messenger: exampleControllerMessenger,\n * actions: ['ErrorReportingService:captureException'],\n * });\n * const exampleController = new ExampleController({\n * messenger: exampleControllerMessenger,\n * });\n *\n * // === Somewhere else ===\n *\n * // Now this will report an error without throwing it.\n * exampleController.doSomething();\n * ```\n *\n * @deprecated This service is deprecated and will be removed in a future\n * release. Please use `Messenger.captureException` directly instead.\n */\nexport class ErrorReportingService {\n name: 'ErrorReportingService' = 'ErrorReportingService' as const;\n\n state = null;\n\n readonly #captureException: ErrorReportingServiceOptions['captureException'];\n\n readonly #messenger: ErrorReportingServiceMessenger;\n\n /**\n * Constructs a new ErrorReportingService.\n *\n * @param options - The options.\n * @param options.messenger - The messenger suited to this\n * ErrorReportingService.\n * @param options.captureException - A function that stores the given error in\n * the error reporting service.\n */\n constructor({ messenger, captureException }: ErrorReportingServiceOptions) {\n this.#messenger = messenger;\n this.#captureException = captureException;\n\n this.#messenger.registerActionHandler(\n 'ErrorReportingService:captureException',\n this.#captureException.bind(this),\n );\n }\n\n /**\n * Reports the given error to an external location.\n *\n * @param error - The error to report.\n * @deprecated This function is deprecated and will be removed in a future\n * release. Please use `Messenger.captureException` directly instead.\n */\n captureException(error: Error): void {\n this.#captureException(error);\n }\n}\n"]}