Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
defekt is custom errors made simple.
Category | Status |
---|---|
Version | |
Dependencies | |
Dev dependencies | |
Build | |
License |
$ npm install defekt
First you need to add a reference to defekt in your application:
const { defekt } = require('defekt');
If you use TypeScript, use the following code instead:
import { defekt } from 'defekt';
Then call the defekt
function and hand over an object of custom error configurations that you would like to have created. In this object each key is the name for an error and the corresponding value is the error's configuration.
const errors = defekt({
ArgumentNull: {},
InvalidOperation: {},
// ...
});
The result is an object containing all the errors you specified. To use one of those errors, simply call the appropriate function with new
and throw it:
throw new errors.InvalidOperation();
By default, the error name is also used as the error message, but converted to a human readable form. E.g., the error name InvalidOperation
becomes the message Invalid operation.
:
try {
throw new errors.InvalidOperation();
} catch (ex) {
console.log(ex.message);
// => 'Invalid operation.'
}
If you want to specify a custom message, feel free to do so:
throw new errors.InvalidOperation('Something failed.');
Additionally, if an error was caused by another error, you can specify this error using the cause
property:
throw new errors.InvalidOperation('Something failed.', {
cause: new Error(...)
});
From time to time, you may need to provide additional data for an error. For this, you can use the data
property:
throw new errors.InvalidOperation('Something failed.', {
data: { ... }
});
The custom errors follow the same rules as the built-in ones, i.e. they have a name
and a message
property, they derive from Error
and they can be recognized by the util.isError
function.
By default, each custom error uses its uppercased name with an E
prefix as error code. E.g., an InvalidOperation
error uses EINVALIDOPERATION
as its code.
From time to time, you may want to provide custom error codes. For that specify a code
property on the configuration object:
const errors = defekt({
ArgumentNull: { code: 'ARGNULL' },
InvalidOperation: { code: 'INVALOP' }
// ...
});
To build this module use roboter:
$ npx roboter
FAQs
defekt is custom errors made simple.
The npm package defekt receives a total of 13,357 weekly downloads. As such, defekt popularity was classified as popular.
We found that defekt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.