| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4339299 commit cfaa1e2
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ const { | |||
| 5 | 5 | ArrayPrototypeShift, | |
| 6 | 6 | Error, | |
| 7 | 7 | ObjectDefineProperty, | |
| 8 | + ObjectPrototypeHasOwnProperty, | ||
| 8 | 9 | SafeWeakMap, | |
| 9 | 10 | } = primordials; | |
| 10 | 11 | ||
@@ -79,6 +80,12 @@ function hasRejectionToWarn() { | |||
| 79 | 80 | return tickInfo[kHasRejectionToWarn] === 1; | |
| 80 | 81 | } | |
| 81 | 82 | ||
| 83 | + function isErrorLike(o) { | ||
| 84 | + return typeof o === 'object' && | ||
| 85 | + o !== null && | ||
| 86 | + ObjectPrototypeHasOwnProperty(o, 'stack'); | ||
| 87 | + } | ||
| 88 | + | ||
| 82 | 89 | function getUnhandledRejectionsMode() { | |
| 83 | 90 | const { getOptionValue } = require('internal/options'); | |
| 84 | 91 | switch (getOptionValue('--unhandled-rejections')) { | |
@@ -179,14 +186,21 @@ function emitUnhandledRejectionWarning(uid, reason) { | |||
| 179 | 186 | `(rejection id: ${uid})` | |
| 180 | 187 | ); | |
| 181 | 188 | try { | |
| 182 | - if (reason instanceof Error) { | ||
| 189 | + if (isErrorLike(reason)) { | ||
| 183 | 190 | warning.stack = reason.stack; | |
| 184 | 191 | process.emitWarning(reason.stack, unhandledRejectionErrName); | |
| 185 | 192 | } else { | |
| 186 | 193 | process.emitWarning( | |
| 187 | 194 | noSideEffectsToString(reason), unhandledRejectionErrName); | |
| 188 | 195 | } | |
| 189 | - } catch {} | ||
| 196 | + } catch { | ||
| 197 | + try { | ||
| 198 | + process.emitWarning( | ||
| 199 | + noSideEffectsToString(reason), unhandledRejectionErrName); | ||
| 200 | + } catch { | ||
| 201 | + // Ignore. | ||
| 202 | + } | ||
| 203 | + } | ||
| 190 | 204 | ||
| 191 | 205 | process.emitWarning(warning); | |
| 192 | 206 | } | |
@@ -232,7 +246,7 @@ function processPromiseRejections() { | |||
| 232 | 246 | try { | |
| 233 | 247 | switch (unhandledRejectionsMode) { | |
| 234 | 248 | case kStrictUnhandledRejections: { | |
| 235 | - const err = reason instanceof Error ? | ||
| 249 | + const err = isErrorLike(reason) ? | ||
| 236 | 250 | reason : generateUnhandledRejectionError(reason); | |
| 237 | 251 | // This destroys the async stack, don't clear it after | |
| 238 | 252 | triggerUncaughtException(err, true /* fromPromise */); | |
@@ -259,7 +273,7 @@ function processPromiseRejections() { | |||
| 259 | 273 | case kThrowUnhandledRejections: { | |
| 260 | 274 | const handled = emit(reason, promise, promiseInfo); | |
| 261 | 275 | if (!handled) { | |
| 262 | - const err = reason instanceof Error ? | ||
| 276 | + const err = isErrorLike(reason) ? | ||
| 263 | 277 | reason : generateUnhandledRejectionError(reason); | |
| 264 | 278 | // This destroys the async stack, don't clear it after | |
| 265 | 279 | triggerUncaughtException(err, true /* fromPromise */); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | 'use strict'; | |
| 3 | 3 | ||
| 4 | 4 | const common = require('../common'); | |
| 5 | + const assert = require('assert'); | ||
| 5 | 6 | ||
| 6 | 7 | // Verify that ignoring unhandled rejection works fine and that no warning is | |
| 7 | 8 | // logged. | |
@@ -12,11 +13,25 @@ new Promise(() => { | |||
| 12 | 13 | ||
| 13 | 14 | Promise.reject('test'); | |
| 14 | 15 | ||
| 16 | + function lookForMeInStackTrace() { | ||
| 17 | + Promise.reject(new class ErrorLike { | ||
| 18 | + constructor() { | ||
| 19 | + Error.captureStackTrace(this); | ||
| 20 | + this.message = 'ErrorLike'; | ||
| 21 | + } | ||
| 22 | + }()); | ||
| 23 | + } | ||
| 24 | + lookForMeInStackTrace(); | ||
| 25 | + | ||
| 15 | 26 | // Unhandled rejections trigger two warning per rejection. One is the rejection | |
| 16 | 27 | // reason and the other is a note where this warning is coming from. | |
| 17 | - process.on('warning', common.mustCall(4)); | ||
| 28 | + process.on('warning', common.mustCall((reason) => { | ||
| 29 | + if (reason.message.includes('ErrorLike')) { | ||
| 30 | + assert.match(reason.stack, /lookForMeInStackTrace/); | ||
| 31 | + } | ||
| 32 | + }, 6)); | ||
| 18 | 33 | process.on('uncaughtException', common.mustNotCall('uncaughtException')); | |
| 19 | - process.on('rejectionHandled', common.mustCall(2)); | ||
| 34 | + process.on('rejectionHandled', common.mustCall(3)); | ||
| 20 | 35 | ||
| 21 | 36 | process.on('unhandledRejection', (reason, promise) => { | |
| 22 | 37 | // Handle promises but still warn! | |
| Back | FazBrowse Home | New Git URL |
0 commit comments