@metamask/error-reporting-service
Version:
Logs errors to an error reporting service such as Sentry
143 lines • 5.87 kB
JavaScript
;
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;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorReportingService = void 0;
/**
* `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.
*/
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 }) {
this.name = 'ErrorReportingService';
this.state = null;
_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.
* @deprecated This function is deprecated and will be removed in a future
* release. Please use `Messenger.captureException` directly instead.
*/
captureException(error) {
__classPrivateFieldGet(this, _ErrorReportingService_captureException, "f").call(this, error);
}
}
exports.ErrorReportingService = ErrorReportingService;
_ErrorReportingService_captureException = new WeakMap(), _ErrorReportingService_messenger = new WeakMap();
//# sourceMappingURL=error-reporting-service.cjs.map