in AutoCollection/Exceptions.ts [37:84]
public enable(isEnabled: boolean) {
if (isEnabled) {
this._isInitialized = true;
var self = this;
if (!this._exceptionListenerHandle) {
// For scenarios like Promise.reject(), an error won't be passed to the handle. Create a placeholder
// error for these scenarios.
var handle = (reThrow: boolean, name: string, error: Error = new Error(AutoCollectExceptions._FALLBACK_ERROR_MESSAGE)) => {
let exceptionTelemetry: Contracts.ExceptionTelemetry = { exception: error };
// Add full error in context so it could used in telemetryProcessors
exceptionTelemetry.contextObjects = {};
exceptionTelemetry.contextObjects["Error"] = error;
this._client.trackException(exceptionTelemetry);
this._client.flush({ isAppCrashing: true });
// only rethrow when we are the only listener
if (reThrow && name && (<any>process).listeners(name).length === 1) {
console.error(error);
process.exit(1);
}
};
if (AutoCollectExceptions._canUseUncaughtExceptionMonitor) {
// Node.js >= 13.7.0, use uncaughtExceptionMonitor. It handles both promises and exceptions
this._exceptionListenerHandle = handle.bind(this, false, undefined); // never rethrows
(<any>process).on(AutoCollectExceptions.UNCAUGHT_EXCEPTION_MONITOR_HANDLER_NAME, this._exceptionListenerHandle);
} else {
this._exceptionListenerHandle = handle.bind(this, true, AutoCollectExceptions.UNCAUGHT_EXCEPTION_HANDLER_NAME);
this._rejectionListenerHandle = handle.bind(this, false, undefined); // never rethrows
(<any>process).on(AutoCollectExceptions.UNCAUGHT_EXCEPTION_HANDLER_NAME, this._exceptionListenerHandle);
(<any>process).on(AutoCollectExceptions.UNHANDLED_REJECTION_HANDLER_NAME, this._rejectionListenerHandle);
}
}
} else {
if (this._exceptionListenerHandle) {
if (AutoCollectExceptions._canUseUncaughtExceptionMonitor) {
process.removeListener(AutoCollectExceptions.UNCAUGHT_EXCEPTION_MONITOR_HANDLER_NAME, this._exceptionListenerHandle);
} else {
process.removeListener(AutoCollectExceptions.UNCAUGHT_EXCEPTION_HANDLER_NAME, this._exceptionListenerHandle);
process.removeListener(AutoCollectExceptions.UNHANDLED_REJECTION_HANDLER_NAME, this._rejectionListenerHandle);
}
this._exceptionListenerHandle = undefined;
this._rejectionListenerHandle = undefined;
delete this._exceptionListenerHandle;
delete this._rejectionListenerHandle;
}
}
}