pax_global_header00006660000000000000000000000064127363057320014522gustar00rootroot0000000000000052 comment=174a9b37f0de7ed255526b506f37961f9f74bd4d loud-rejection-1.6.0/000077500000000000000000000000001273630573200144515ustar00rootroot00000000000000loud-rejection-1.6.0/.editorconfig000066400000000000000000000002761273630573200171330ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 loud-rejection-1.6.0/.gitattributes000066400000000000000000000000141273630573200173370ustar00rootroot00000000000000* text=auto loud-rejection-1.6.0/.gitignore000066400000000000000000000000311273630573200164330ustar00rootroot00000000000000node_modules .nyc_output loud-rejection-1.6.0/.travis.yml000066400000000000000000000001141273630573200165560ustar00rootroot00000000000000language: node_js node_js: - '6' - '4' after_success: npm run coveralls loud-rejection-1.6.0/api.js000066400000000000000000000005461273630573200155650ustar00rootroot00000000000000'use strict'; var util = require('util'); var currentlyUnhandled = require('currently-unhandled'); // WARNING: This undocumented API is subject to change. module.exports = util.deprecate(function (process) { return { currentlyUnhandled: currentlyUnhandled(process) }; }, 'loudRejection/api is deprecated. Use the currently-unhandled module instead.'); loud-rejection-1.6.0/fixture-custom-log.js000066400000000000000000000002311273630573200205600ustar00rootroot00000000000000'use strict'; var loudRejection = require('./'); loudRejection(function (str) { console.log('custom-log', str); }); Promise.reject(new Error('foo')); loud-rejection-1.6.0/fixture.js000066400000000000000000000017651273630573200165060ustar00rootroot00000000000000'use strict'; var Promise = require('bluebird'); var loudRejection = require('./'); loudRejection(); var promises = {}; console.log('started'); function reject(key, reason) { // IMPORTANT: key is always logged to stdout // Make sure to remember that when grepping output (keep key and message different). console.log('Rejecting:', key); promises[key] = new Promise(function (resolve, reject) { reject(reason); }); } function handle(key) { promises[key].catch(function () {}); } process.on('message', function (message) { switch (message.action) { case 'reject-error': return reject(message.key, new Error(message.message)); case 'reject-value': return reject(message.key, message.value); case 'reject-nothing': return reject(message.key); case 'reinstall': return loudRejection(); case 'handle': return handle(message.key); default: console.error('Unknown message received:', message); process.exit(1); } }); process.send({status: 'ready'}); setTimeout(function () {}, 30000); loud-rejection-1.6.0/index.js000066400000000000000000000012521273630573200161160ustar00rootroot00000000000000'use strict'; var util = require('util'); var onExit = require('signal-exit'); var currentlyUnhandled = require('currently-unhandled'); var installed = false; module.exports = function (log) { if (installed) { return; } installed = true; log = log || console.error; var listUnhandled = currentlyUnhandled(); onExit(function () { var unhandledRejections = listUnhandled(); if (unhandledRejections.length > 0) { unhandledRejections.forEach(function (x) { var err = x.reason; if (!(err instanceof Error)) { err = new Error('Promise rejected with value: ' + util.inspect(err)); } log(err.stack); }); process.exitCode = 1; } }); }; loud-rejection-1.6.0/license000066400000000000000000000021371273630573200160210ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. loud-rejection-1.6.0/package.json000066400000000000000000000021611273630573200167370ustar00rootroot00000000000000{ "name": "loud-rejection", "version": "1.6.0", "description": "Make unhandled promise rejections fail loudly instead of the default silent fail", "license": "MIT", "repository": "sindresorhus/loud-rejection", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=0.10.0" }, "scripts": { "test": "xo && nyc ava", "coveralls": "nyc report --reporter=text-lcov | coveralls" }, "files": [ "index.js", "register.js", "api.js" ], "keywords": [ "promise", "promises", "unhandled", "uncaught", "rejection", "loud", "fail", "catch", "throw", "handler", "exit", "debug", "debugging", "verbose" ], "dependencies": { "currently-unhandled": "^0.4.1", "signal-exit": "^3.0.0" }, "devDependencies": { "ava": "*", "bluebird": "^3.0.5", "coveralls": "^2.11.4", "delay": "^1.0.0", "execa": "^0.4.0", "get-stream": "^2.0.0", "nyc": "^6.2.1", "xo": "*" }, "nyc": { "exclude": [ "fixture.js" ] } } loud-rejection-1.6.0/readme.md000066400000000000000000000032671273630573200162400ustar00rootroot00000000000000# loud-rejection [![Build Status](https://travis-ci.org/sindresorhus/loud-rejection.svg?branch=master)](https://travis-ci.org/sindresorhus/loud-rejection) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/loud-rejection/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/loud-rejection?branch=master) > Make unhandled promise rejections fail loudly instead of the default [silent fail](https://gist.github.com/benjamingr/0237932cee84712951a2) By default, promises fail silently if you don't attach a `.catch()` handler to them. Use this in top-level things like tests, CLI tools, apps, etc, **but not in reusable modules.**
Not needed in the browser as unhandled promises are shown in the console. ## Install ``` $ npm install --save loud-rejection ``` ## Usage ```js const loudRejection = require('loud-rejection'); const promiseFn = require('promise-fn'); // Install the unhandledRejection listeners loudRejection(); promiseFn(); ``` Without this module it's more verbose and you might even miss some that will fail silently: ```js const promiseFn = require('promise-fn'); function error(err) { console.error(err.stack); process.exit(1); } promiseFn().catch(error); ``` ### Register script Alternatively to the above, you may simply require `loud-rejection/register` and the unhandledRejection listener will be automagically installed for you. This is handy for ES2015 imports: ```js import 'loud-rejection/register'; ``` ## API ### loudRejection([log]) #### log Type: `Function`
Default: `console.error` Custom logging function to print the rejected promise. Receives the error stack. ## License MIT © [Sindre Sorhus](https://sindresorhus.com) loud-rejection-1.6.0/register.js000066400000000000000000000000371273630573200166330ustar00rootroot00000000000000'use strict'; require('./')(); loud-rejection-1.6.0/test.js000066400000000000000000000100161273630573200157640ustar00rootroot00000000000000import {fork} from 'child_process'; import test from 'ava'; import getStream from 'get-stream'; import delay from 'delay'; import execa from 'execa'; function tick(time) { // slow things down for reliable tests on Travis CI return delay(process.env.CI ? time * 10 : time); } test.cb.beforeEach(t => { const child = fork('fixture.js', {silent: true}); const exit = new Promise((resolve, reject) => child.on('exit', code => (code > 0 ? reject : resolve)(code) ) ); t.context = { // tell the child to create a promise, and reject it rejectWithError: (key, message) => child.send({ action: 'reject-error', key, message }), rejectWithValue: (key, value) => child.send({ action: 'reject-value', key, value }), rejectWithNothing: key => child.send({ action: 'reject-nothing', key }), // tell the child to handle the promise previously rejected handle: key => child.send({ action: 'handle', key }), // tell the child to reinstall loudRejection reinstall: () => child.send({action: 'reinstall'}), // kill the child (returns a promise for when the child is done) kill: () => { child.kill(); return exit; }, // the stdout of the child. Useful for debug stdout: getStream(child.stdout), // the stderr of the child. This is where unhandledRejections will be logged stderr: getStream(child.stderr), // promise for when the child has exited exit }; child.on('message', message => { if (message.status !== 'ready') { t.fail(`I got a message I don't understand: ${JSON.stringify(message)}`); } t.end(); }); }); test('no rejections', async t => { const child = t.context; await tick(20); await child.kill(); t.is(await child.stderr, ''); }); test('one unhandled rejection', async t => { const child = t.context; child.rejectWithError('a', 'foo123'); await tick(20); await child.kill(); t.regex(await child.stderr, /foo123/); }); test('two unhandled rejections', async t => { const child = t.context; child.rejectWithError('a', 'foo456'); child.rejectWithError('b', 'bar789'); await tick(20); await child.kill(); t.regex(await child.stderr, /foo456/); t.regex(await child.stderr, /bar789/); }); test('one rejection that is handled before exit', async t => { const child = t.context; child.rejectWithError('a', 'foo123'); await tick(20); child.handle('a'); await tick(20); await child.kill(); t.is(await child.stderr, ''); }); test('two rejections, first one handled', async t => { const child = t.context; child.rejectWithError('a', 'foo987'); child.rejectWithError('b', 'bar654'); await tick(20); child.handle('a'); await tick(20); await child.kill(); t.false(/foo987/.test(await child.stderr)); t.regex(await child.stderr, /bar654/); }); test('two rejections, last one handled', async t => { const child = t.context; child.rejectWithError('a', 'foo987'); child.rejectWithError('b', 'bar654'); await tick(20); child.handle('b'); await tick(20); await child.kill(); t.regex(await child.stderr, /foo987/); t.false(/bar654/.test(await child.stderr)); }); test('rejection with a string value', async t => { const child = t.context; child.rejectWithValue('a', 'foo123'); await tick(20); await child.kill(); t.regex(await child.stderr, /Promise rejected with value: 'foo123'/); }); test('rejection with a falsy value', async t => { const child = t.context; child.rejectWithValue('a', false); child.rejectWithValue('a', 0); await tick(20); await child.kill(); t.regex(await child.stderr, /Promise rejected with value: false/); t.regex(await child.stderr, /Promise rejected with value: 0/); }); test('rejection with no value', async t => { const child = t.context; child.rejectWithNothing(); await tick(20); await child.kill(); t.regex(await child.stderr, /Promise rejected with value: undefined/); }); test('custom log function', async t => { // TODO: use execa `reject: false` option const stdout = await execa('node', ['fixture-custom-log.js']).catch(err => err.stdout); t.is(stdout.split('\n')[0], 'custom-log Error: foo'); });