| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5cbe0f2 commit 9a9aa88
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,11 +60,25 @@ function setupPromises(scheduleMicrotasks) { | |||
| 60 | 60 | } | |
| 61 | 61 | ||
| 62 | 62 | function emitWarning(uid, reason) { | |
| 63 | + try { | ||
| 64 | + if (reason instanceof Error) { | ||
| 65 | + process.emitWarning(reason.stack, 'UnhandledPromiseRejectionWarning'); | ||
| 66 | + } else { | ||
| 67 | + process.emitWarning( | ||
| 68 | + safeToString(reason), 'UnhandledPromiseRejectionWarning' | ||
| 69 | + ); | ||
| 70 | + } | ||
| 71 | + } catch (e) { | ||
| 72 | + // ignored | ||
| 73 | + } | ||
| 74 | + | ||
| 63 | 75 | const warning = new Error( | |
| 64 | - `Unhandled promise rejection (rejection id: ${uid}): ` + | ||
| 65 | - safeToString(reason)); | ||
| 76 | + 'Unhandled promise rejection. This error originated either by ' + | ||
| 77 | + 'throwing inside of an async function without a catch block, ' + | ||
| 78 | + 'or by rejecting a promise which was not handled with .catch(). ' + | ||
| 79 | + `(rejection id: ${uid})` | ||
| 80 | + ); | ||
| 66 | 81 | warning.name = 'UnhandledPromiseRejectionWarning'; | |
| 67 | - warning.id = uid; | ||
| 68 | 82 | try { | |
| 69 | 83 | if (reason instanceof Error) { | |
| 70 | 84 | warning.stack = reason.stack; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,21 @@ | |||
| 1 | + (node:*) UnhandledPromiseRejectionWarning: Error: This was rejected | ||
| 2 | + at * (*test*message*unhandled_promise_trace_warnings.js:*) | ||
| 3 | + at * | ||
| 4 | + at * | ||
| 5 | + at * | ||
| 6 | + at * | ||
| 7 | + at * | ||
| 8 | + at * | ||
| 9 | + at * | ||
| 10 | + at * | ||
| 11 | + at * | ||
| 12 | + at * | ||
| 13 | + at * | ||
| 14 | + at * | ||
| 15 | + at * | ||
| 16 | + at * | ||
| 17 | + at * | ||
| 18 | + at * | ||
| 1 | 19 | (node:*) Error: This was rejected | |
| 2 | 20 | at * (*test*message*unhandled_promise_trace_warnings.js:*) | |
| 3 | 21 | at * | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,8 +6,11 @@ const expectedDeprecationWarning = 'Unhandled promise rejections are ' + | |||
| 6 | 6 | 'rejections that are not handled will ' + | |
| 7 | 7 | 'terminate the Node.js process with a ' + | |
| 8 | 8 | 'non-zero exit code.'; | |
| 9 | - const expectedPromiseWarning = 'Unhandled promise rejection (rejection id: ' + | ||
| 10 | - '1): [object Object]'; | ||
| 9 | + const expectedPromiseWarning = 'Unhandled promise rejection. ' + | ||
| 10 | + 'This error originated either by throwing ' + | ||
| 11 | + 'inside of an async function without a catch ' + | ||
| 12 | + 'block, or by rejecting a promise which was ' + | ||
| 13 | + 'not handled with .catch(). (rejection id: 1)'; | ||
| 11 | 14 | ||
| 12 | 15 | function throwErr() { | |
| 13 | 16 | throw new Error('Error from proxy'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,17 +1,24 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | const common = require('../common'); | |
| 3 | 3 | ||
| 4 | + const expectedValueWarning = 'Symbol()'; | ||
| 4 | 5 | const expectedDeprecationWarning = 'Unhandled promise rejections are ' + | |
| 5 | 6 | 'deprecated. In the future, promise ' + | |
| 6 | 7 | 'rejections that are not handled will ' + | |
| 7 | 8 | 'terminate the Node.js process with a ' + | |
| 8 | 9 | 'non-zero exit code.'; | |
| 9 | - const expectedPromiseWarning = 'Unhandled promise rejection (rejection id: ' + | ||
| 10 | - '1): Symbol()'; | ||
| 10 | + const expectedPromiseWarning = 'Unhandled promise rejection. ' + | ||
| 11 | + 'This error originated either by throwing ' + | ||
| 12 | + 'inside of an async function without a catch ' + | ||
| 13 | + 'block, or by rejecting a promise which was ' + | ||
| 14 | + 'not handled with .catch(). (rejection id: 1)'; | ||
| 11 | 15 | ||
| 12 | 16 | common.expectWarning({ | |
| 13 | 17 | DeprecationWarning: expectedDeprecationWarning, | |
| 14 | - UnhandledPromiseRejectionWarning: expectedPromiseWarning, | ||
| 18 | + UnhandledPromiseRejectionWarning: [ | ||
| 19 | + expectedPromiseWarning, | ||
| 20 | + expectedValueWarning | ||
| 21 | + ], | ||
| 15 | 22 | }); | |
| 16 | 23 | ||
| 17 | 24 | // ensure this doesn't crash | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,18 +12,43 @@ let b = 0; | |||
| 12 | 12 | process.on('warning', common.mustCall((warning) => { | |
| 13 | 13 | switch (b++) { | |
| 14 | 14 | case 0: | |
| 15 | - assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning'); | ||
| 16 | - assert(/Unhandled promise rejection/.test(warning.message)); | ||
| 15 | + // String rejection error displayed | ||
| 16 | + assert.strictEqual(warning.message, 'This was rejected'); | ||
| 17 | 17 | break; | |
| 18 | 18 | case 1: | |
| 19 | - assert.strictEqual(warning.name, 'DeprecationWarning'); | ||
| 19 | + // Warning about rejection not being handled (will be next tick) | ||
| 20 | + assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning'); | ||
| 21 | + assert( | ||
| 22 | + /Unhandled promise rejection/.test(warning.message), | ||
| 23 | + 'Expected warning message to contain "Unhandled promise rejection" ' + | ||
| 24 | + 'but did not. Had "' + warning.message + '" instead.' | ||
| 25 | + ); | ||
| 20 | 26 | break; | |
| 21 | 27 | case 2: | |
| 28 | + // One time deprecation warning, first unhandled rejection | ||
| 29 | + assert.strictEqual(warning.name, 'DeprecationWarning'); | ||
| 30 | + break; | ||
| 31 | + case 3: | ||
| 32 | + // Number rejection error displayed. Note it's been stringified | ||
| 33 | + assert.strictEqual(warning.message, '42'); | ||
| 34 | + break; | ||
| 35 | + case 4: | ||
| 36 | + // Unhandled rejection warning (won't be handled next tick) | ||
| 37 | + assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning'); | ||
| 38 | + assert( | ||
| 39 | + /Unhandled promise rejection/.test(warning.message), | ||
| 40 | + 'Expected warning message to contain "Unhandled promise rejection" ' + | ||
| 41 | + 'but did not. Had "' + warning.message + '" instead.' | ||
| 42 | + ); | ||
| 43 | + break; | ||
| 44 | + case 5: | ||
| 45 | + // Rejection handled asynchronously. | ||
| 22 | 46 | assert.strictEqual(warning.name, 'PromiseRejectionHandledWarning'); | |
| 23 | 47 | assert(/Promise rejection was handled asynchronously/ | |
| 24 | 48 | .test(warning.message)); | |
| 25 | 49 | } | |
| 26 | - }, 3)); | ||
| 50 | + }, 6)); | ||
| 27 | 51 | ||
| 28 | - const p = Promise.reject('This was rejected'); | ||
| 29 | - setImmediate(common.mustCall(() => p.catch(() => {}))); | ||
| 52 | + const p = Promise.reject('This was rejected'); // Reject with a string | ||
| 53 | + setImmediate(common.mustCall(() => p.catch(() => { }))); | ||
| 54 | + Promise.reject(42); // Reject with a number | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments