From 2065e07df881068135df6d902a9c7f056236f478 Mon Sep 17 00:00:00 2001 From: ForbesLindesay Date: Mon, 25 Jul 2016 19:42:56 +0100 Subject: [PATCH 1/4] Add standard rejection tracking --- es6.js | 5 ++++ package.json | 8 ++++-- src/rejection-handlers-client.js | 47 ++++++++++++++++++++++++++++++++ src/rejection-handlers-node.js | 47 ++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 es6.js create mode 100644 src/rejection-handlers-client.js create mode 100644 src/rejection-handlers-node.js diff --git a/es6.js b/es6.js new file mode 100644 index 0000000..a5de164 --- /dev/null +++ b/es6.js @@ -0,0 +1,5 @@ +'use strict'; + +var Promise = require('./lib/core.js'); +require('./lib/es6-extensions.js'); +require('./lib/rejection-handlers-node.js'); diff --git a/package.json b/package.json index acec38e..2afa4aa 100644 --- a/package.json +++ b/package.json @@ -31,5 +31,9 @@ }, "dependencies": { "asap": "~2.0.3" - } -} \ No newline at end of file + }, + "browser": { + "./lib/rejection-handlers-node": "./lib/rejection-handlers-client", + "./lib/rejection-handlers-node.js": "./lib/rejection-handlers-client.js", + }, +} diff --git a/src/rejection-handlers-client.js b/src/rejection-handlers-client.js new file mode 100644 index 0000000..5354bf5 --- /dev/null +++ b/src/rejection-handlers-client.js @@ -0,0 +1,47 @@ +'use strict'; + +let id = 1; +Promise._onHandle = function (promise) { + if ( + promise._state === 2 // IS REJECTED + ) { + if (promise._rejectionEmitted) { + if (global.onrejectionhandled) { + global.onrejectionhandled({promise: promise, reason: promise._value}); + } + if (promise._loggingID) { + console.warn( + 'Promise Rejection Handled (id: ' + promise._loggingID + '):' + ); + console.warn( + ' This means you can ignore any previous messages of the form ' + + '"Possible Unhandled Promise Rejection" with id ' + + promise._loggingID + '.' + ); + } + } else { + clearTimeout(promise._rejectionEmitTimer); + } + } +}; +Promise._onReject = function (promise, err) { + if (promise._deferredState === 0) { // not yet handled + promise._rejectionEmitTimer = setTimeout(function () { + promise._rejectionEmitted = true; + if (global.onunhandledrejection) { + global.onunhandledrejection({promise: promise, reason: err}); + } else { + promise._loggingID = id++; + console.warn( + 'Possible Unhandled Promise Rejection (id: ' + + promise._loggingID + + '):' + ); + var errStr = (error && (error.stack || error)) + ''; + errStr.split('\n').forEach(function (line) { + console.warn(' ' + line); + }); + } + }, 0); + } +}; diff --git a/src/rejection-handlers-node.js b/src/rejection-handlers-node.js new file mode 100644 index 0000000..95698c7 --- /dev/null +++ b/src/rejection-handlers-node.js @@ -0,0 +1,47 @@ +'use strict'; + +let id = 1; +Promise._onHandle = function (promise) { + if ( + promise._state === 2 // IS REJECTED + ) { + if (promise._rejectionEmitted) { + promise.emit('rejectionHandled', promise); + if (promise._loggingID) { + console.warn( + 'Promise Rejection Handled (id: ' + promise._loggingID + '):' + ); + console.warn( + ' This means you can ignore any previous messages of the form ' + + '"Possible Unhandled Promise Rejection" with id ' + + promise._loggingID + '.' + ); + } + } else { + clearTimeout(promise._rejectionEmitTimer); + } + } +}; +Promise._onReject = function (promise, err) { + if (promise._deferredState === 0) { // not yet handled + promise._rejectionEmitTimer = setTimeout(function () { + promise._rejectionEmitted = true; + process.emit('unhandledRejection', promise, err); + if ( + process.listenerCount && + process.listenerCount('unhandledRejection') === 0 + ) { + promise._loggingID = id++; + console.warn( + 'Possible Unhandled Promise Rejection (id: ' + + promise._loggingID + + '):' + ); + var errStr = (error && (error.stack || error)) + ''; + errStr.split('\n').forEach(function (line) { + console.warn(' ' + line); + }); + } + }, 0); + } +}; From a4e8274305afd4732a53d3a5f017224045adae78 Mon Sep 17 00:00:00 2001 From: ForbesLindesay Date: Mon, 25 Jul 2016 19:46:55 +0100 Subject: [PATCH 2/4] Remember to export promise --- es6.js | 1 + 1 file changed, 1 insertion(+) diff --git a/es6.js b/es6.js index a5de164..489e108 100644 --- a/es6.js +++ b/es6.js @@ -3,3 +3,4 @@ var Promise = require('./lib/core.js'); require('./lib/es6-extensions.js'); require('./lib/rejection-handlers-node.js'); +module.exports = Promise; From ab2fc010f49f345e37761b9b7cde1d4ef3776075 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Wed, 27 Jul 2016 10:34:23 +0100 Subject: [PATCH 3/4] Remove trailing comma from package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2afa4aa..d649e15 100644 --- a/package.json +++ b/package.json @@ -35,5 +35,5 @@ "browser": { "./lib/rejection-handlers-node": "./lib/rejection-handlers-client", "./lib/rejection-handlers-node.js": "./lib/rejection-handlers-client.js", - }, + } } From 42a6690f5018c7235013d6184a7793b2ce1603b3 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Wed, 27 Jul 2016 10:39:54 +0100 Subject: [PATCH 4/4] Remove the other trailing comma --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d649e15..e15f16b 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,6 @@ }, "browser": { "./lib/rejection-handlers-node": "./lib/rejection-handlers-client", - "./lib/rejection-handlers-node.js": "./lib/rejection-handlers-client.js", + "./lib/rejection-handlers-node.js": "./lib/rejection-handlers-client.js" } }