Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
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.
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.
$ npm install loud-rejection
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:
const promiseFunction = require('promise-fn');
function error(error) {
console.error(error.stack);
process.exit(1);
}
promiseFunction().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';
Type: Function
Default: console.error
Custom logging function to print the rejected promise. Receives the error stack.
FAQs
Make unhandled promise rejections fail loudly instead of the default silent fail
The npm package loud-rejection receives a total of 2,252,785 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.