UNPKG

@itwin/core-bentley

Version:

Bentley JavaScript core components

67 lines 3.23 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Errors */ Object.defineProperty(exports, "__esModule", { value: true }); exports.UnexpectedErrors = void 0; const Logger_1 = require("./Logger"); /** * Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for * unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected * errors should not cause program termination, and should instead be logged and swallowed. However, for * development/debugging, it can be helpful to re-throw exceptions so they are not missed. * @public */ class UnexpectedErrors { /** handler for re-throwing exceptions directly */ static reThrowImmediate = (e) => { throw e; }; /** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */ static reThrowDeferred = (e) => setTimeout(() => { throw e; }, 0); /** handler for logging exception to console */ static consoleLog = (e) => console.error(e); // eslint-disable-line no-console /** handler for logging exception with [[Logger]] */ static errorLog = (e) => Logger_1.Logger.logException("unhandled", e); static _telemetry = []; static _handler = this.errorLog; // default to error logging constructor() { } // this is a singleton /** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler. * @returns a method to remove the tracker */ static addTelemetry(tracker) { this._telemetry.push(tracker); return () => this._telemetry.splice(this._telemetry.indexOf(tracker), 1); } /** call this method when an unexpected error happens so the global handler can process it. * @param error the unexpected error * @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example. */ static handle(error, notifyTelemetry = true) { this._handler(error); if (notifyTelemetry) { this._telemetry.forEach((telemetry) => { try { telemetry(error); } catch { // ignore errors from telemetry trackers } }); } } /** establish a new global *unexpected error* handler. * @param handler the new global handler. You may provide your own function or use one of the static members of this class. * The default is [[errorLog]]. * @returns the previous handler. Useful to temporarily change the handler. */ static setHandler(handler) { const oldHandler = this._handler; this._handler = handler; return oldHandler; } } exports.UnexpectedErrors = UnexpectedErrors; //# sourceMappingURL=UnexpectedErrors.js.map