Repository URL to install this package:
|
Version:
0.6.14 ▾
|
// (c) Copyright 2023 Supertenant Ltd. - all rights reserved.
// See LICENSE file in project root for license terms.
"use strict";const serializeError=require("serialize-error");let logger,downstreamConnection=(logger=require("../logger").getLogger("util/uncaughtExceptionHandler",newLogger=>{logger=newLogger}),null),processIdentityProvider=null;const unhandledRejectionEventName="unhandledRejection";let unhandledRejectionDeprecationWarningHasBeenEmitted=!1,config,unhandledRejectionsMode="warn/default";for(let i=0;i<process.execArgv.length;i++)"--unhandled-rejections=none"===process.execArgv[i]?unhandledRejectionsMode="none":"--unhandled-rejections=strict"===process.execArgv[i]&&(unhandledRejectionsMode="strict");function activateUnhandledPromiseRejectionHandling(){config.reportUnhandledPromiseRejections?"strict"===unhandledRejectionsMode?logger.warn("Node.js has been started with --unhandled-rejections=strict, therefore reporting unhandled promise rejections will not be enabled."):(process.on(unhandledRejectionEventName,onUnhandledRejection),logger.info("Reporting unhandled promise rejections is enabled.")):logger.info("Reporting unhandled promise rejections is disabled.")}function onUnhandledRejection(reason){"none"!==unhandledRejectionsMode&&(console.warn("UnhandledPromiseRejectionWarning:",reason),console.warn("UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()."),unhandledRejectionDeprecationWarningHasBeenEmitted||(console.warn("[DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code."),unhandledRejectionDeprecationWarningHasBeenEmitted=!0)),downstreamConnection.sendEvent(createEventForUnhandledRejection(reason),error=>{error&&logger.error("Error received while trying to send event to agent: %s",error.message)})}function createEventForUnhandledRejection(reason){return createEvent(reason,"An unhandled promise rejection occured in a Node.js process.",5,!0)}function createEvent(error,title,severity,isPromiseRejection){let eventText;return{title:title,text:eventText=null!=error?errorToMarkdown(error):isPromiseRejection?'No "reason" parameter has been provided when the promise was rejected.':"A null/undefined value has been thrown.",plugin:"com.instana.forge.infrastructure.runtime.nodejs.NodeJsRuntimePlatform",id:processIdentityProvider&&"function"==typeof processIdentityProvider.getEntityId?processIdentityProvider.getEntityId():void 0,timestamp:Date.now(),duration:1,severity:severity}}function errorToMarkdown(error){error=serializeError(error);return error.name&&error.message&&"string"==typeof error.stack?`### ${error.name}
#### Message:
${error.message}
#### Stack:
`+stackTraceToMarkdown(error.stack):JSON.stringify(error)}function stackTraceToMarkdown(stackTrace){let formatted="";return stackTrace.split("\n").forEach(callSite=>{formatted=`${formatted}* \`${callSite.trim()}\`\n`}),formatted}exports.init=function(_config,_downstreamConnection,_processIdentityProvider){config=_config,downstreamConnection=_downstreamConnection,processIdentityProvider=_processIdentityProvider},exports.activate=function(){activateUnhandledPromiseRejectionHandling()},exports.deactivate=function(){process.removeListener(unhandledRejectionEventName,onUnhandledRejection)};