From e5f0fd7c7546886100b73fcb2e51c4f1f4ae8ae0 Mon Sep 17 00:00:00 2001 From: "Dmitrii Bobreshev (Akvelon INC)" Date: Thu, 5 Jan 2023 15:13:08 +0100 Subject: [PATCH] Added unhandledRejection event The event "unhandledRejection" was added on process.on because when Promise is rejected and there is no catch the error will not be thrown and process.on('uncaughtException') won't call for Node10 but it will emit "uncaughtException" for node 16. So we need to listen to "unhandledRejection" and throw the error to make sure the error is thrown and the result will be similar. --- node/package-lock.json | 2 +- node/package.json | 2 +- node/task.ts | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/node/package-lock.json b/node/package-lock.json index 7bf741552..247aa110b 100644 --- a/node/package-lock.json +++ b/node/package-lock.json @@ -1,6 +1,6 @@ { "name": "azure-pipelines-task-lib", - "version": "4.1.0", + "version": "4.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/node/package.json b/node/package.json index aa7d571bb..8f0cb19e8 100644 --- a/node/package.json +++ b/node/package.json @@ -1,6 +1,6 @@ { "name": "azure-pipelines-task-lib", - "version": "4.1.0", + "version": "4.2.0", "description": "Azure Pipelines Task SDK", "main": "./task.js", "typings": "./task.d.ts", diff --git a/node/task.ts b/node/task.ts index d948b4b73..74013b9e5 100644 --- a/node/task.ts +++ b/node/task.ts @@ -108,6 +108,20 @@ process.on('uncaughtException', (err: Error) => { error(String(err.stack)); }); +// +// Catching unhandled rejections from promises and rethrowing them as exceptions +// For example, a promise that is rejected but not handled by a .catch() handler in node 10 +// doesn't cause an uncaughtException but causes in Node 16. +// For types definitions(Error | Any) see https://nodejs.org/docs/latest-v16.x/api/process.html#event-unhandledrejection +// +process.on('unhandledRejection', (reason: Error | any) => { + if (reason instanceof Error) { + throw reason; + } else { + throw new Error(reason); + } +}); + //----------------------------------------------------- // Loc Helpers //-----------------------------------------------------