Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
loud-rejection
Advanced tools
Make unhandled promise rejections fail loudly instead of the default silent fail
The loud-rejection npm package is designed to make unhandled promise rejections fail loudly instead of the default silent fail. It helps in debugging by logging the errors to the console.
Default handler for unhandled rejections
This feature sets up a default handler for unhandled promise rejections, which logs the stack trace to stderr.
const loudRejection = require('loud-rejection');
loudRejection();
new Promise((resolve, reject) => {
reject(new Error('Unhandled rejection'));
});
Custom log function
This feature allows you to provide a custom log function to handle the unhandled rejections in a way that suits your needs.
const loudRejection = require('loud-rejection');
const customLogger = error => console.error('Custom log:', error);
loudRejection(customLogger);
new Promise((resolve, reject) => {
reject(new Error('Unhandled rejection with custom logger'));
});
Similar to loud-rejection, hard-rejection makes unhandled promise rejections fail hard and immediately exit the process with an error code, ensuring that unhandled rejections are not silently ignored.
While not directly related to unhandled rejections, why-is-node-running can be used to log out active handles that are keeping Node.js process hanging after an unhandled rejection or any other errors, which can be useful for debugging purposes.
Make unhandled promise rejections fail loudly instead of the default silent fail
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.
$ npm install --save loud-rejection
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:
const promiseFn = require('promise-fn');
function error(err) {
console.error(err.stack);
process.exit(1);
}
promiseFn().catch(error);
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:
import 'loud-rejection/register';
MIT © Sindre Sorhus
FAQs
Make unhandled promise rejections fail loudly instead of the default silent fail
The npm package loud-rejection receives a total of 2,779,454 weekly downloads. As such, loud-rejection popularity was classified as popular.
We found that loud-rejection demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.