What is silent-error?
The silent-error npm package is designed to create and handle errors in a way that is silent, meaning it does not immediately throw or log the error. This can be useful in scenarios where you want to handle errors gracefully without disrupting the flow of your application.
What are silent-error's main functionalities?
Creating a Silent Error
This feature allows you to create a silent error with a custom message. The error can be logged or handled without immediately throwing it.
const SilentError = require('silent-error');
const error = new SilentError('This is a silent error');
console.log(error.message); // Output: This is a silent error
Handling Silent Errors
This feature demonstrates how to handle silent errors using a try-catch block. It checks if the error is an instance of SilentError and handles it accordingly.
const SilentError = require('silent-error');
try {
throw new SilentError('This is a silent error');
} catch (error) {
if (error instanceof SilentError) {
console.log('Handled a silent error:', error.message);
} else {
throw error;
}
}
Other packages similar to silent-error
verror
The verror package provides a way to create and handle nested errors with additional context. It is more focused on error wrapping and providing detailed error messages compared to silent-error.
custom-error-generator
The custom-error-generator package allows you to create custom error types with additional properties. It is similar to silent-error in that it provides a way to create custom errors, but it offers more flexibility in defining error properties.
create-error
The create-error package is a simple utility for creating custom error types. It is similar to silent-error but focuses on simplicity and ease of use for creating custom errors.
silent-error
An error subclass for humanized errors. This module allows for inter-module detection of errors which are fatal, but where a stacktrace by default provides negative value.
Some use-cases:
- command in your CLI tool is missing
- plugin to your build system is given invalid user-input.
Obviously stack traces can still be valuable. To view the stacks, the following environment variable can be set to true
SILENT_ERROR=verbose <run program>
Example
async function runCommand(name) {
throw new SilentError(`command: '${name}' is not installed`);
}
async function caller() {
try {
await runCommand('foo');
} catch(e) {
SilentError.debugOrThrow(e);
}
SilentError.debugOrThrow
}
Installation
yarn add silent-error
or
npm install --save silent-error