pax_global_header00006660000000000000000000000064135437015300014513gustar00rootroot0000000000000052 comment=63acebc25e18516741b7328f4919922b529f8f69 loud-rejection-2.2.0/000077500000000000000000000000001354370153000144375ustar00rootroot00000000000000loud-rejection-2.2.0/.editorconfig000066400000000000000000000002571354370153000171200ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 loud-rejection-2.2.0/.gitattributes000066400000000000000000000000231354370153000173250ustar00rootroot00000000000000* text=auto eol=lf loud-rejection-2.2.0/.github/000077500000000000000000000000001354370153000157775ustar00rootroot00000000000000loud-rejection-2.2.0/.github/funding.yml000066400000000000000000000001701354370153000201520ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/loud-rejection custom: https://sindresorhus.com/donate loud-rejection-2.2.0/.github/security.md000066400000000000000000000002631354370153000201710ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. loud-rejection-2.2.0/.gitignore000066400000000000000000000000431354370153000164240ustar00rootroot00000000000000node_modules yarn.lock .nyc_output loud-rejection-2.2.0/.npmrc000066400000000000000000000000231354370153000155520ustar00rootroot00000000000000package-lock=false loud-rejection-2.2.0/.travis.yml000066400000000000000000000002361354370153000165510ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' after_success: - './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' loud-rejection-2.2.0/fixture-custom-log.js000066400000000000000000000002301354370153000205450ustar00rootroot00000000000000'use strict'; const loudRejection = require('.'); loudRejection(string => { console.log('custom-log', string); }); Promise.reject(new Error('foo')); loud-rejection-2.2.0/fixture.js000066400000000000000000000017611354370153000164700ustar00rootroot00000000000000'use strict'; const Promise = require('bluebird'); const loudRejection = require('.'); loudRejection(); const 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(((resolve, reject) => { reject(reason); })); } function handle(key) { promises[key].catch(() => {}); } process.on('message', 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(() => {}, 30000); loud-rejection-2.2.0/index.d.ts000066400000000000000000000014351354370153000163430ustar00rootroot00000000000000declare const loudRejection: { /** Make unhandled promise rejections fail loudly instead of the default [silent fail](https://gist.github.com/benjamingr/0237932cee84712951a2). @param log - Custom logging function to print the rejected promise. Receives the error stack. Default: `console.error`. @example ``` import loudRejection = require('loud-rejection'); import promiseFunction = require('promise-fn'); // Install the `unhandledRejection` listeners loudRejection(); promiseFunction(); ``` */ (log?: (stack: string) => void): void; // TODO: remove this in the next major version, refactor the whole definition to: // declare function loudRejection(log?: (stack: string) => void): void; // export = loudRejection; default: typeof loudRejection; }; export = loudRejection; loud-rejection-2.2.0/index.js000066400000000000000000000014751354370153000161130ustar00rootroot00000000000000'use strict'; const util = require('util'); const onExit = require('signal-exit'); const currentlyUnhandled = require('currently-unhandled'); let installed = false; const loudRejection = (log = console.error) => { if (installed) { return; } installed = true; const listUnhandled = currentlyUnhandled(); onExit(() => { const unhandledRejections = listUnhandled(); if (unhandledRejections.length > 0) { for (const unhandledRejection of unhandledRejections) { let error = unhandledRejection.reason; if (!(error instanceof Error)) { error = new Error(`Promise rejected with value: ${util.inspect(error)}`); } log(error.stack); } process.exitCode = 1; } }); }; module.exports = loudRejection; // TODO: remove this in the next major version module.exports.default = loudRejection; loud-rejection-2.2.0/index.test-d.ts000066400000000000000000000002541354370153000173160ustar00rootroot00000000000000import {expectType} from 'tsd'; import loudRejection = require('.'); import './register'; expectType(loudRejection()); expectType(loudRejection(console.log)); loud-rejection-2.2.0/license000066400000000000000000000021251354370153000160040ustar00rootroot00000000000000MIT License 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-2.2.0/package.json000066400000000000000000000020141354370153000167220ustar00rootroot00000000000000{ "name": "loud-rejection", "version": "2.2.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": ">=8" }, "scripts": { "test": "xo && nyc ava && tsd" }, "files": [ "index.js", "index.d.ts", "register.js", "register.d.ts" ], "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.2" }, "devDependencies": { "ava": "^1.4.1", "bluebird": "^3.5.3", "coveralls": "^3.0.3", "delay": "^4.1.0", "execa": "^1.0.0", "get-stream": "^5.0.0", "nyc": "^13.3.0", "tsd": "^0.7.1", "xo": "^0.24.0" }, "nyc": { "exclude": [ "fixture.js" ] } } loud-rejection-2.2.0/readme.md000066400000000000000000000047211354370153000162220ustar00rootroot00000000000000# 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. This tool keeps track of unhandled rejections globally. If any remain unhandled at the end of your process, it logs them to STDERR and exits with code 1. Use this in top-level things like tests, CLI tools, apps, etc, **but not in reusable modules.**
Not needed in the browser as unhandled rejections are shown in the console. ## Install ``` $ npm install loud-rejection ``` ## Usage ```js const loudRejection = require('loud-rejection'); const promiseFunction = require('promise-fn'); // Install the `unhandledRejection` listeners loudRejection(); promiseFunction(); ``` Without this module it's more verbose and you might even miss some that will fail silently: ```js const promiseFunction = require('promise-fn'); function error(error) { console.error(error.stack); process.exit(1); } promiseFunction().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. ## Related - [hard-rejection](https://github.com/sindresorhus/hard-rejection) - Make unhandled promise rejections fail hard right away instead of the default silent fail - [More…](https://github.com/sindresorhus/promise-fun) ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
loud-rejection-2.2.0/register.d.ts000066400000000000000000000000001354370153000170430ustar00rootroot00000000000000loud-rejection-2.2.0/register.js000066400000000000000000000000361354370153000166200ustar00rootroot00000000000000'use strict'; require('.')(); loud-rejection-2.2.0/test.js000066400000000000000000000100221354370153000157470ustar00rootroot00000000000000import {fork} from 'child_process'; import test from 'ava'; import getStream from 'get-stream'; import delay from 'delay'; import execa from 'execa'; // Slow things down for reliable tests on Travis CI const tick = time => delay(process.env.CI ? time * 10 : time); test.beforeEach.cb(t => { const child = fork('fixture.js', {silent: true}); const exit = new Promise((resolve, reject) => child.on('exit', exitCode => (exitCode > 0 ? reject : resolve)(exitCode) ) ); 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(error => error.stdout); t.is(stdout.split('\n')[0], 'custom-log Error: foo'); });