Skip to content

Commit 0d2f17b

Browse files
Added unhandledRejection event (#912)
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.
1 parent 5f76bee commit 0d2f17b

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

node/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-pipelines-task-lib",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"description": "Azure Pipelines Task SDK",
55
"main": "./task.js",
66
"typings": "./task.d.ts",

node/task.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ process.on('uncaughtException', (err: Error) => {
108108
error(String(err.stack));
109109
});
110110

111+
//
112+
// Catching unhandled rejections from promises and rethrowing them as exceptions
113+
// For example, a promise that is rejected but not handled by a .catch() handler in node 10
114+
// doesn't cause an uncaughtException but causes in Node 16.
115+
// For types definitions(Error | Any) see https://nodejs.org/docs/latest-v16.x/api/process.html#event-unhandledrejection
116+
//
117+
process.on('unhandledRejection', (reason: Error | any) => {
118+
if (reason instanceof Error) {
119+
throw reason;
120+
} else {
121+
throw new Error(reason);
122+
}
123+
});
124+
111125
//-----------------------------------------------------
112126
// Loc Helpers
113127
//-----------------------------------------------------

0 commit comments

Comments
 (0)