aboutsummaryrefslogtreecommitdiff
path: root/node_modules/promise/setimmediate/rejection-tracking.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/promise/setimmediate/rejection-tracking.js')
-rw-r--r--node_modules/promise/setimmediate/rejection-tracking.js113
1 files changed, 113 insertions, 0 deletions
diff --git a/node_modules/promise/setimmediate/rejection-tracking.js b/node_modules/promise/setimmediate/rejection-tracking.js
new file mode 100644
index 000000000..088a0deaa
--- /dev/null
+++ b/node_modules/promise/setimmediate/rejection-tracking.js
@@ -0,0 +1,113 @@
+'use strict';
+
+var Promise = require('./core');
+
+var DEFAULT_WHITELIST = [
+ ReferenceError,
+ TypeError,
+ RangeError
+];
+
+var enabled = false;
+exports.disable = disable;
+function disable() {
+ enabled = false;
+ Promise._10 = null;
+ Promise._97 = null;
+}
+
+exports.enable = enable;
+function enable(options) {
+ options = options || {};
+ if (enabled) disable();
+ enabled = true;
+ var id = 0;
+ var displayId = 0;
+ var rejections = {};
+ Promise._10 = function (promise) {
+ if (
+ promise._81 === 2 && // IS REJECTED
+ rejections[promise._72]
+ ) {
+ if (rejections[promise._72].logged) {
+ onHandled(promise._72);
+ } else {
+ clearTimeout(rejections[promise._72].timeout);
+ }
+ delete rejections[promise._72];
+ }
+ };
+ Promise._97 = function (promise, err) {
+ if (promise._45 === 0) { // not yet handled
+ promise._72 = id++;
+ rejections[promise._72] = {
+ displayId: null,
+ error: err,
+ timeout: setTimeout(
+ onUnhandled.bind(null, promise._72),
+ // For reference errors and type errors, this almost always
+ // means the programmer made a mistake, so log them after just
+ // 100ms
+ // otherwise, wait 2 seconds to see if they get handled
+ matchWhitelist(err, DEFAULT_WHITELIST)
+ ? 100
+ : 2000
+ ),
+ logged: false
+ };
+ }
+ };
+ function onUnhandled(id) {
+ if (
+ options.allRejections ||
+ matchWhitelist(
+ rejections[id].error,
+ options.whitelist || DEFAULT_WHITELIST
+ )
+ ) {
+ rejections[id].displayId = displayId++;
+ if (options.onUnhandled) {
+ rejections[id].logged = true;
+ options.onUnhandled(
+ rejections[id].displayId,
+ rejections[id].error
+ );
+ } else {
+ rejections[id].logged = true;
+ logError(
+ rejections[id].displayId,
+ rejections[id].error
+ );
+ }
+ }
+ }
+ function onHandled(id) {
+ if (rejections[id].logged) {
+ if (options.onHandled) {
+ options.onHandled(rejections[id].displayId, rejections[id].error);
+ } else if (!rejections[id].onUnhandled) {
+ console.warn(
+ 'Promise Rejection Handled (id: ' + rejections[id].displayId + '):'
+ );
+ console.warn(
+ ' This means you can ignore any previous messages of the form "Possible Unhandled Promise Rejection" with id ' +
+ rejections[id].displayId + '.'
+ );
+ }
+ }
+ }
+}
+
+function logError(id, error) {
+ console.warn('Possible Unhandled Promise Rejection (id: ' + id + '):');
+ var errStr = (error && (error.stack || error)) + '';
+ errStr.split('\n').forEach(function (line) {
+ console.warn(' ' + line);
+ });
+}
+
+function matchWhitelist(error, list) {
+ return list.some(function (cls) {
+ return error instanceof cls;
+ });
+} \ No newline at end of file