@sentry/node
Version:
Sentry Node SDK using OpenTelemetry for performance instrumentation
72 lines (71 loc) • 2.54 kB
JavaScript
import { DEBUG_BUILD } from "../debug-build.js";
import { logAndExitProcess } from "../utils/errorhandling.js";
import { captureException, debug, defineIntegration, getClient } from "@sentry/core";
import { isMainThread } from "worker_threads";
// src/integrations/onuncaughtexception.ts
const INTEGRATION_NAME = "OnUncaughtException";
/**
* Add a global exception handler.
*/
const onUncaughtExceptionIntegration = defineIntegration((options = {}) => {
const optionsWithDefaults = {
exitEvenIfOtherHandlersAreRegistered: false,
...options
};
return {
name: INTEGRATION_NAME,
setup(client) {
if (!isMainThread) return;
global.process.on("uncaughtException", makeErrorHandler(client, optionsWithDefaults));
}
};
});
/** Exported only for tests */
function makeErrorHandler(client, options) {
const timeout = 2e3;
let caughtFirstError = false;
let caughtSecondError = false;
let calledFatalError = false;
let firstError;
const clientOptions = client.getOptions();
return Object.assign((error) => {
let onFatalError = logAndExitProcess;
if (options.onFatalError) onFatalError = options.onFatalError;
else if (clientOptions.onFatalError) onFatalError = clientOptions.onFatalError;
const processWouldExit = global.process.listeners("uncaughtException").filter((listener) => {
return listener.name !== "domainUncaughtExceptionClear" && listener._errorHandler !== true;
}).length === 0;
const shouldApplyFatalHandlingLogic = options.exitEvenIfOtherHandlersAreRegistered || processWouldExit;
if (!caughtFirstError) {
firstError = error;
caughtFirstError = true;
if (getClient() === client) captureException(error, {
originalException: error,
captureContext: { level: "fatal" },
mechanism: {
handled: false,
type: "auto.node.onuncaughtexception"
}
});
if (!calledFatalError && shouldApplyFatalHandlingLogic) {
calledFatalError = true;
onFatalError(error);
}
} else if (shouldApplyFatalHandlingLogic) {
if (calledFatalError) {
DEBUG_BUILD && debug.warn("uncaught exception after calling fatal error shutdown callback - this is bad! forcing shutdown");
logAndExitProcess(error);
} else if (!caughtSecondError) {
caughtSecondError = true;
setTimeout(() => {
if (!calledFatalError) {
calledFatalError = true;
onFatalError(firstError, error);
}
}, timeout);
}
}
}, { _errorHandler: true });
}
export { makeErrorHandler, onUncaughtExceptionIntegration };
//# sourceMappingURL=onuncaughtexception.js.map