UNPKG

@metamask/error-reporting-service

Version:

Logs errors to an error reporting service such as Sentry

114 lines 4.82 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _ErrorReportingService_captureException, _ErrorReportingService_messenger; /** * `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 = RestrictedMessenger< * 'ExampleController', * AllowedActions, * never, * AllowedActions['type'], * 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.messagingSystem.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'; * * // Create a global messenger. * const globalMessenger = new Messenger(); * * // Register handler for the `ErrorReportingService:captureException` * // action in the global messenger. * const errorReportingServiceMessenger = globalMessenger.getRestricted({ * allowedActions: [], * allowedEvents: [], * }); * const errorReportingService = new ErrorReportingService({ * messenger: errorReportingServiceMessenger, * captureException, * }); * * const exampleControllerMessenger = globalMessenger.getRestricted({ * allowedActions: ['ErrorReportingService:captureException'], * allowedEvents: [], * }); * const exampleController = new ExampleController({ * messenger: exampleControllerMessenger, * }); * * // === Somewhere else === * * // Now this will report an error without throwing it. * exampleController.doSomething(); * ``` */ export class ErrorReportingService { /** * 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 }) { _ErrorReportingService_captureException.set(this, void 0); _ErrorReportingService_messenger.set(this, void 0); __classPrivateFieldSet(this, _ErrorReportingService_messenger, messenger, "f"); __classPrivateFieldSet(this, _ErrorReportingService_captureException, captureException, "f"); __classPrivateFieldGet(this, _ErrorReportingService_messenger, "f").registerActionHandler('ErrorReportingService:captureException', __classPrivateFieldGet(this, _ErrorReportingService_captureException, "f").bind(this)); } /** * Reports the given error to an external location. * * @param error - The error to report. */ captureException(error) { __classPrivateFieldGet(this, _ErrorReportingService_captureException, "f").call(this, error); } } _ErrorReportingService_captureException = new WeakMap(), _ErrorReportingService_messenger = new WeakMap(); //# sourceMappingURL=error-reporting-service.mjs.map