pax_global_header00006660000000000000000000000064134505463000014512gustar00rootroot0000000000000052 comment=71300d4b742f2cee0f926f4ad3459b66c88d610f hard-rejection-2.1.0/000077500000000000000000000000001345054630000144105ustar00rootroot00000000000000hard-rejection-2.1.0/.editorconfig000066400000000000000000000002571345054630000170710ustar00rootroot00000000000000root = 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 hard-rejection-2.1.0/.gitattributes000066400000000000000000000000231345054630000172760ustar00rootroot00000000000000* text=auto eol=lf hard-rejection-2.1.0/.gitignore000066400000000000000000000000271345054630000163770ustar00rootroot00000000000000node_modules yarn.lock hard-rejection-2.1.0/.npmrc000066400000000000000000000000231345054630000155230ustar00rootroot00000000000000package-lock=false hard-rejection-2.1.0/.travis.yml000066400000000000000000000000641345054630000165210ustar00rootroot00000000000000language: node_js node_js: - '10' - '8' - '6' hard-rejection-2.1.0/fixture-custom-log.js000066400000000000000000000002341345054630000205220ustar00rootroot00000000000000'use strict'; const hardRejection = require('.'); hardRejection(string => { console.log('custom-log', string); }); Promise.reject(new Error('Unicorn')); hard-rejection-2.1.0/fixture.js000066400000000000000000000001531345054630000164330ustar00rootroot00000000000000'use strict'; const hardRejection = require('.'); hardRejection(); Promise.reject(new Error('Unicorn')); hard-rejection-2.1.0/index.d.ts000066400000000000000000000011311345054630000163050ustar00rootroot00000000000000declare const hardRejection: { /** Make unhandled promise rejections fail hard right away 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`. */ (log?: (stack?: string) => void): void; // TODO: Remove this for the next major release, refactor the whole definition to: // declare function hardRejection(log?: (stack?: string) => void): void; // export = hardRejection; default: typeof hardRejection; }; export = hardRejection; hard-rejection-2.1.0/index.js000066400000000000000000000007641345054630000160640ustar00rootroot00000000000000'use strict'; const util = require('util'); let installed = false; const hardRejection = (log = console.error) => { if (installed) { return; } installed = true; process.on('unhandledRejection', error => { if (!(error instanceof Error)) { error = new Error(`Promise rejected with value: ${util.inspect(error)}`); } log(error.stack); process.exit(1); }); }; module.exports = hardRejection; // TODO: Remove this for the next major release module.exports.default = hardRejection; hard-rejection-2.1.0/index.test-d.ts000066400000000000000000000002541345054630000172670ustar00rootroot00000000000000import {expectType} from 'tsd'; import hardRejection = require('.'); import './register'; expectType(hardRejection()); expectType(hardRejection(console.log)); hard-rejection-2.1.0/license000066400000000000000000000021251345054630000157550ustar00rootroot00000000000000MIT 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. hard-rejection-2.1.0/package.json000066400000000000000000000014451345054630000167020ustar00rootroot00000000000000{ "name": "hard-rejection", "version": "2.1.0", "description": "Make unhandled promise rejections fail hard right away instead of the default silent fail", "license": "MIT", "repository": "sindresorhus/hard-rejection", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=6" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts", "register.js" ], "keywords": [ "promise", "promises", "unhandled", "uncaught", "rejection", "hard", "fail", "catch", "throw", "handler", "exit", "debug", "debugging", "verbose", "immediate", "immediately" ], "devDependencies": { "ava": "^1.4.1", "execa": "^1.0.0", "tsd": "^0.7.1", "xo": "^0.24.0" } } hard-rejection-2.1.0/readme.md000066400000000000000000000040611345054630000161700ustar00rootroot00000000000000# hard-rejection [![Build Status](https://travis-ci.org/sindresorhus/hard-rejection.svg?branch=master)](https://travis-ci.org/sindresorhus/hard-rejection) > Make unhandled promise rejections fail hard right away instead of the default [silent fail](https://gist.github.com/benjamingr/0237932cee84712951a2) Promises fail silently if you don't attach a `.catch()` handler. This module exits the process with an error message right away when an unhandled rejection is encountered.
**Note: That might not be desirable as unhandled rejections can be [handled at a future point in time](https://nodejs.org/api/process.html#process_event_unhandledrejection), although not common. You've been warned.** Intended for top-level long-running processes like servers, **but not in reusable modules.**
For command-line apps and tests, see [`loud-rejection`](https://github.com/sindresorhus/loud-rejection). ## Install ``` $ npm install hard-rejection ``` ## Usage ```js const hardRejection = require('hard-rejection'); const promiseFunction = require('some-promise-fn'); // Install the handler hardRejection(); promiseFunction(); ``` Without this module it's more verbose and you might even miss some that will fail silently: ```js const promiseFunction = require('some-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 `hard-rejection/register` and the handler will be automagically installed for you. This is handy for ES2015 imports: ```js import 'hard-rejection/register'; ``` ## API ### hardRejection([log]) #### log Type: `Function`
Default: `console.error` Custom logging function to print the rejected promise. Receives the error stack. ## Related - [loud-rejection](https://github.com/sindresorhus/loud-rejection) - Make unhandled promise rejections fail loudly instead of the default silent fail - [More…](https://github.com/sindresorhus/promise-fun) ## License MIT © [Sindre Sorhus](https://sindresorhus.com) hard-rejection-2.1.0/register.js000066400000000000000000000000361345054630000165710ustar00rootroot00000000000000'use strict'; require('.')(); hard-rejection-2.1.0/test.js000066400000000000000000000006031345054630000157240ustar00rootroot00000000000000import test from 'ava'; import execa from 'execa'; test('main', async t => { const error = await t.throwsAsync(execa('node', ['fixture.js']), {code: 1}); t.regex(error.stderr, /^Error: Unicorn/); }); test('custom logging', async t => { const error = await t.throwsAsync(execa('node', ['fixture-custom-log.js']), {code: 1}); t.regex(error.stdout, /^custom-log Error: Unicorn/); });