Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
error-kid
A simple toolkit to work with custom errors. Definitely not a kid.
# yarn
yarn add error-kid
# pnpm
pnpm i error-kid
# npm
npm i error-kid
createErrorClass
Creates a new error class with predefined name and data type.
import { createErrorClass } from 'error-kid';
const UnknownError = createErrorClass('UnknownError');
UnknownError.name; // 'UnknownError'
By default, the created error class constructor accepts no arguments. It also passes nothing
to the Error
super constructor.
To change this behavior, define the arguments' type and provide the toSuper
options function
converting passed arguments to the Error
super constructor.
Here is the example:
import { createErrorClass } from 'error-kid';
const UnknownError = createErrorClass<
// Error name.
'UnknownError',
// Constructor arguments.
[errorText: string, retriesCount: number, cause?: unknown]
>('UnknownError', {
toSuper(errorText, retriesCount, cause) {
// `Error` constructor requires the first argument
// to be the error message. The second one is ErrorOptions,
// containing the `cause` property.
return [
`Unknown error occurred. Retries count: ${retriesCount}. Error text: ${errorText}`,
{ cause },
];
},
});
const error = new UnknownError('Ooopsie!', 3, new Error('Just because'));
error.message; // "Unknown error occurred. Retries count: 3. Error text: Ooopsie!"
error.cause; // Error('Just because')
If the custom error class must contain some additional data, consider using the second
generic argument and provide the toData
function converting constructor arguments to the
defined type.
import { createErrorClass } from 'error-kid';
const TimeoutError = createErrorClass<
'TimeoutError',
{ duration: number },
[duration: number]
>('UnknownError', duration => ({ duration }), {
toSuper(duration) {
return [`Timed out: ${duration}ms`];
},
});
const error = new TimeoutError(1000);
error.data; // { duration: 1000 }
error.message; // "Timed out: 1000ms"
// Or even without the third argument:
const AbortError = createErrorClass<
'AbortError',
{ reason: unknown },
[cause: unknown]
>('AbortError', cause => ({ reason: cause }));
const error2 = new AbortError(new Error('Just because'));
error2.data; // { reason: Error('Just because') }
FAQs
A simple toolkit to work with custom errors. Definitely not a kid.
We found that error-kid demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.