|
2 | 2 |
|
3 | 3 | const { safeToString } = process.binding('util'); |
4 | 4 |
|
5 | | -const promiseRejectEvent = process._promiseRejectEvent; |
6 | | -const hasBeenNotifiedProperty = new WeakMap(); |
7 | | -const promiseToGuidProperty = new WeakMap(); |
| 5 | +const maybeUnhandledPromises = new WeakMap(); |
8 | 6 | const pendingUnhandledRejections = []; |
9 | | -let lastPromiseId = 1; |
| 7 | +const asyncHandledRejections = []; |
| 8 | +let lastPromiseId = 0; |
10 | 9 |
|
11 | | -exports.setup = setupPromises; |
| 10 | +module.exports = { |
| 11 | + emitPromiseRejectionWarnings |
| 12 | +}; |
12 | 13 |
|
13 | | -function getAsynchronousRejectionWarningObject(uid) { |
14 | | - return new Error('Promise rejection was handled ' + |
15 | | - `asynchronously (rejection id: ${uid})`); |
16 | | -} |
17 | | - |
18 | | -function setupPromises(scheduleMicrotasks) { |
19 | | - let deprecationWarned = false; |
| 14 | +process._setupPromises(unhandledRejection, handledRejection); |
20 | 15 |
|
21 | | - process._setupPromises(function(event, promise, reason) { |
22 | | - if (event === promiseRejectEvent.unhandled) |
23 | | - unhandledRejection(promise, reason); |
24 | | - else if (event === promiseRejectEvent.handled) |
25 | | - rejectionHandled(promise); |
26 | | - else |
27 | | - require('assert').fail(null, null, 'unexpected PromiseRejectEvent'); |
| 16 | +function unhandledRejection(promise, reason) { |
| 17 | + maybeUnhandledPromises.set(promise, { |
| 18 | + reason, |
| 19 | + uid: ++lastPromiseId, |
| 20 | + warned: false |
28 | 21 | }); |
| 22 | + pendingUnhandledRejections.push(promise); |
| 23 | + return true; |
| 24 | +} |
29 | 25 |
|
30 | | - function unhandledRejection(promise, reason) { |
31 | | - hasBeenNotifiedProperty.set(promise, false); |
32 | | - promiseToGuidProperty.set(promise, lastPromiseId++); |
33 | | - addPendingUnhandledRejection(promise, reason); |
| 26 | +function handledRejection(promise) { |
| 27 | + const promiseInfo = maybeUnhandledPromises.get(promise); |
| 28 | + if (promiseInfo !== undefined) { |
| 29 | + maybeUnhandledPromises.delete(promise); |
| 30 | + if (promiseInfo.warned) { |
| 31 | + const { uid } = promiseInfo; |
| 32 | + // Generate the warning object early to get a good stack trace. |
| 33 | + const warning = new Error('Promise rejection was handled ' + |
| 34 | + `asynchronously (rejection id: ${uid})`); |
| 35 | + warning.name = 'PromiseRejectionHandledWarning'; |
| 36 | + warning.id = uid; |
| 37 | + asyncHandledRejections.push({ promise, warning }); |
| 38 | + return true; |
| 39 | + } |
34 | 40 | } |
| 41 | + return false; |
| 42 | +} |
35 | 43 |
|
36 | | - function rejectionHandled(promise) { |
37 | | - const hasBeenNotified = hasBeenNotifiedProperty.get(promise); |
38 | | - if (hasBeenNotified !== undefined) { |
39 | | - hasBeenNotifiedProperty.delete(promise); |
40 | | - const uid = promiseToGuidProperty.get(promise); |
41 | | - promiseToGuidProperty.delete(promise); |
42 | | - if (hasBeenNotified === true) { |
43 | | - let warning = null; |
44 | | - if (!process.listenerCount('rejectionHandled')) { |
45 | | - // Generate the warning object early to get a good stack trace. |
46 | | - warning = getAsynchronousRejectionWarningObject(uid); |
47 | | - } |
48 | | - process.nextTick(function() { |
49 | | - if (!process.emit('rejectionHandled', promise)) { |
50 | | - if (warning === null) |
51 | | - warning = getAsynchronousRejectionWarningObject(uid); |
52 | | - warning.name = 'PromiseRejectionHandledWarning'; |
53 | | - warning.id = uid; |
54 | | - process.emitWarning(warning); |
55 | | - } |
56 | | - }); |
57 | | - } |
58 | | - |
| 44 | +const unhandledRejectionErrName = 'UnhandledPromiseRejectionWarning'; |
| 45 | +function emitWarning(uid, reason) { |
| 46 | + try { |
| 47 | + if (reason instanceof Error) { |
| 48 | + process.emitWarning(reason.stack, unhandledRejectionErrName); |
| 49 | + } else { |
| 50 | + process.emitWarning(safeToString(reason), unhandledRejectionErrName); |
59 | 51 | } |
| 52 | + } catch (e) { |
| 53 | + // ignored |
60 | 54 | } |
61 | 55 |
|
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 |
| 56 | + const warning = new Error( |
| 57 | + 'Unhandled promise rejection. This error originated either by ' + |
| 58 | + 'throwing inside of an async function without a catch block, ' + |
| 59 | + 'or by rejecting a promise which was not handled with .catch(). ' + |
| 60 | + `(rejection id: ${uid})` |
| 61 | + ); |
| 62 | + warning.name = unhandledRejectionErrName; |
| 63 | + try { |
| 64 | + if (reason instanceof Error) { |
| 65 | + warning.stack = reason.stack; |
73 | 66 | } |
| 67 | + } catch (err) { |
| 68 | + // ignored |
| 69 | + } |
| 70 | + process.emitWarning(warning); |
| 71 | + emitDeprecationWarning(); |
| 72 | +} |
74 | 73 |
|
75 | | - const warning = new Error( |
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 | | - ); |
81 | | - warning.name = 'UnhandledPromiseRejectionWarning'; |
82 | | - try { |
83 | | - if (reason instanceof Error) { |
84 | | - warning.stack = reason.stack; |
85 | | - } |
86 | | - } catch (err) { |
87 | | - // ignored |
88 | | - } |
89 | | - process.emitWarning(warning); |
90 | | - if (!deprecationWarned) { |
91 | | - deprecationWarned = true; |
92 | | - process.emitWarning( |
93 | | - 'Unhandled promise rejections are deprecated. In the future, ' + |
94 | | - 'promise rejections that are not handled will terminate the ' + |
95 | | - 'Node.js process with a non-zero exit code.', |
96 | | - 'DeprecationWarning', 'DEP0018'); |
97 | | - } |
| 74 | +let deprecationWarned = false; |
| 75 | +function emitDeprecationWarning() { |
| 76 | + if (!deprecationWarned) { |
| 77 | + deprecationWarned = true; |
| 78 | + process.emitWarning( |
| 79 | + 'Unhandled promise rejections are deprecated. In the future, ' + |
| 80 | + 'promise rejections that are not handled will terminate the ' + |
| 81 | + 'Node.js process with a non-zero exit code.', |
| 82 | + 'DeprecationWarning', 'DEP0018'); |
98 | 83 | } |
| 84 | +} |
99 | 85 |
|
100 | | - function emitPendingUnhandledRejections() { |
101 | | - let hadListeners = false; |
102 | | - while (pendingUnhandledRejections.length > 0) { |
103 | | - const promise = pendingUnhandledRejections.shift(); |
104 | | - const reason = pendingUnhandledRejections.shift(); |
105 | | - if (hasBeenNotifiedProperty.get(promise) === false) { |
106 | | - hasBeenNotifiedProperty.set(promise, true); |
107 | | - const uid = promiseToGuidProperty.get(promise); |
108 | | - if (!process.emit('unhandledRejection', reason, promise)) { |
109 | | - emitWarning(uid, reason); |
110 | | - } else { |
111 | | - hadListeners = true; |
112 | | - } |
113 | | - } |
| 86 | +function emitPromiseRejectionWarnings() { |
| 87 | + while (asyncHandledRejections.length > 0) { |
| 88 | + const { promise, warning } = asyncHandledRejections.shift(); |
| 89 | + if (!process.emit('rejectionHandled', promise)) { |
| 90 | + process.emitWarning(warning); |
114 | 91 | } |
115 | | - return hadListeners; |
116 | 92 | } |
117 | 93 |
|
118 | | - function addPendingUnhandledRejection(promise, reason) { |
119 | | - pendingUnhandledRejections.push(promise, reason); |
120 | | - scheduleMicrotasks(); |
| 94 | + let hadListeners = false; |
| 95 | + let len = pendingUnhandledRejections.length; |
| 96 | + while (len--) { |
| 97 | + const promise = pendingUnhandledRejections.shift(); |
| 98 | + const promiseInfo = maybeUnhandledPromises.get(promise); |
| 99 | + if (promiseInfo !== undefined) { |
| 100 | + promiseInfo.warned = true; |
| 101 | + const { reason, uid } = promiseInfo; |
| 102 | + if (!process.emit('unhandledRejection', reason, promise)) { |
| 103 | + emitWarning(uid, reason); |
| 104 | + } else { |
| 105 | + hadListeners = true; |
| 106 | + } |
| 107 | + } |
121 | 108 | } |
122 | | - |
123 | | - return emitPendingUnhandledRejections; |
| 109 | + return hadListeners || pendingUnhandledRejections.length !== 0; |
124 | 110 | } |
0 commit comments