loud-rejection
Advanced tools
Comparing version
50
index.js
'use strict'; | ||
var onExit = require('signal-exit'); | ||
var installed = false; | ||
function outputRejectedMessage(err) { | ||
if (err instanceof Error) { | ||
console.error(err.stack); | ||
} else if (typeof err === 'undefined') { | ||
console.error('Promise rejected no value'); | ||
} else { | ||
console.error('Promise rejected with value:', err); | ||
} | ||
} | ||
module.exports = function () { | ||
process.on('unhandledRejection', function (err) { | ||
if (err instanceof Error) { | ||
console.error(err.stack); | ||
} else if (err) { | ||
console.error('Promise rejected with value:', err); | ||
} else { | ||
console.error('Promise rejected no value'); | ||
} | ||
var unhandledRejections = []; | ||
process.exit(1); | ||
if (installed) { | ||
console.trace('WARN: loud rejection called more than once'); | ||
return; | ||
} | ||
installed = true; | ||
process.on('unhandledRejection', function (reason, p) { | ||
unhandledRejections.push({reason: reason, p: p}); | ||
}); | ||
process.on('rejectionHandled', function (p) { | ||
var index = unhandledRejections.reduce(function (result, item, idx) { | ||
return (item.p === p ? idx : result); | ||
}, -1); | ||
unhandledRejections.splice(index, 1); | ||
}); | ||
onExit(function () { | ||
if (unhandledRejections.length > 0) { | ||
unhandledRejections.forEach(function (x) { | ||
outputRejectedMessage(x.reason); | ||
}); | ||
process.exitCode = 1; | ||
} | ||
}); | ||
}; |
{ | ||
"name": "loud-rejection", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Make unhandled promise rejections fail loudly instead of the default silent fail", | ||
@@ -16,3 +16,4 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "node test.js || exit 0" | ||
"test": "xo && nyc ava", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
@@ -24,3 +25,5 @@ "files": [ | ||
"promise", | ||
"promises", | ||
"unhandled", | ||
"uncaught", | ||
"rejection", | ||
@@ -32,8 +35,32 @@ "loud", | ||
"handler", | ||
"exit" | ||
"exit", | ||
"debug", | ||
"debugging", | ||
"verbose" | ||
], | ||
"dependencies": { | ||
"signal-exit": "^2.1.2" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"bluebird": "^3.0.5", | ||
"coveralls": "^2.11.4", | ||
"get-stream": "^1.0.0", | ||
"nyc": "^3.2.2", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"ignores": [ | ||
"test.js" | ||
] | ||
}, | ||
"config": { | ||
"nyc": { | ||
"exclude": [ | ||
"node_modules", | ||
"test.js", | ||
"fixture.js" | ||
] | ||
} | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# loud-rejection [](https://travis-ci.org/sindresorhus/loud-rejection) | ||
# loud-rejection [](https://travis-ci.org/sindresorhus/loud-rejection) [](https://coveralls.io/github/sindresorhus/loud-rejection?branch=master) | ||
@@ -7,7 +7,5 @@ > Make unhandled promise rejections fail loudly instead of the default [silent fail](https://gist.github.com/benjamingr/0237932cee84712951a2) | ||
This is useful for CLI tools so you don't have to manually `.catch()` all the promises. | ||
Use it in top-level things like tests, CLI tools, apps, etc, **but not in reusable modules.** | ||
**Should NOT be used in reusable modules. Only in top-level apps / CLI tools.** | ||
## Install | ||
@@ -14,0 +12,0 @@ |
4465
40.67%37
184.62%1
Infinity%6
200%45
-4.26%+ Added
+ Added