Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@dldc/erreur
Advanced tools
Type safe errors metadata
In JavaScript and TypeScript you can throw anything so when you catch something you don't know what it is which make it hard to properly handle errors.
One way to fix this is to create custom classes that extends the Error class
class HttpError extends Error {
// ...
}
try {
// ...
} catch (error) {
if (error instanceof HttpError) {
// ...
}
}
This works but it has a few drawbacks:
Error
class requires some tricks to make it work properly (Object.setPrototypeOf(this, new.target.prototype);
)To solve these issue, this librairy expose an ErreurStore
(erreur
is the french word for error) that let you link any data to an error (using a WeakMap
internally) as well as some utility functions to create and handle errors.
import { createErreurStore } from '@dldc/erreur'
const PermissionErrorStore = createErreurStore<{ userId: string }>()
const DeleteErrorStore = createErreurStore<{ postId: string }>()
function detelePost(userId: string, postId: string) {
try {
if (userId !== 'admin') {
return PermissionErrorStore.setAndThrow(new Error('Permission denied'), { userId });
}
}
if (postId === '1') {
return DeleteErrorStore.setAndThrow('Cannot delete post', { postId })
}
}
If you are building a library, you can export an ErreurStore
instance to let users of your library handle your errors.
import { doStuff, StuffErreurStore } from 'my-library';
function main() {
try {
doStuff();
} catch (error) {
if (StuffErreurStore.has(error)) {
console.log('Stuff error:', stuff);
} else {
console.error('Unknown error:', error);
}
}
}
If you want to expose an ErreurStore
instance to users of your library but you don't want them to be able to set errors, you can expose a readonly version of the store.
import { createErreurStore } from '@dldc/erreur';
// This store can be used to set errors
const PermissionErrorStore = createErreurStore<{ userId: string }>();
// This store can only be used to read errors
export const ReadonlyPermissionErrorStore = PermissionErrorStore.asReadonly;
If you have multiple stores and you want to match any of them, you can use if else
statements but we provide 2 utility functions to make it easier.
matchFirstErreur
import { matchFirstErreur } from '@dldc/erreur';
function handleError(error: Error) {
// This will return the data of the first store that has the error
const result = matchFirstErreur(error, [PermissionErrorStore, InternalErrorStore, NotFoundErrorStore]);
}
matchErreurs
function handleError(error: Error) {
const result = matchFirstErreur(matchErreurs, {
first: PermissionErrorStore,
second: InternalErrorStore,
third: NotFoundErrorStore,
});
// result is an object with the same keys and the matched data if any
if (result.first) {
// ...
} else if (result.second) {
// ...
} else if (result.third) {
// ...
}
}
If you don't need to store any data with your errors, you can use the createVoidErreurStore
function.
import { createVoidErreurStore } from '@dldc/erreur';
const NotFoundErrorStore = createVoidErreurStore();
It works the same way as the regular ErreurStore
but you don't need to pass any data when setting an error and the get
method will return true
if the error is set.
FAQs
Type safe custom errors
The npm package @dldc/erreur receives a total of 7 weekly downloads. As such, @dldc/erreur popularity was classified as not popular.
We found that @dldc/erreur demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.