Common object error library specifically for functional typescript.
Using npm:
npm install obj-errOr using yarn:
yarn add obj-errimport { errorBuilder, InferError } from 'obj-err';
// Define an error for each specific case
export const NotFounderror = errorBuidler('NotFoundError');
export type NotFounderror = InferError<typeof NotFounderror>;
// Another error with additional properties
export const InputTooShortError = errorBuilder('InputTooShortError', {
id: 'string',
requiredLength: 123,
actualLength: 123,
});
export type InputTooShortError = InferError<typeof InputTooShortError>;
// You can use zod schema for extra properties
import { z } from 'zod';
export const AnotherError = errorBuilder(
'AnotherError',
z.object({
reason: z.string(),
code: z.number(),
})
);
export type AnotherError = InferError<typeof AnotherError>;export const validateString = (
input: string
): Result<string, InputTooShortError> => {
if (input.length < 5) {
return err(
InputTooShortError('Input is too short', {
cause: new Error('Validation failed'), // Store the original error
extra: {
details: 'The input must be at least 5 characters long.',
requiredLength: 5,
actualLength: input.length,
},
})
);
}
return ok(input);
};const handleError = (
error: InputTooShortError | NotFounderror | AnotherError
) =>
match(error)
.with(InputTooShortError.is, () => StatusCode.BadRequest)
.with(NotFounderror.is, () => StatusCode.NotFound)
.with(AnotherError.is, () => StatusCode.InternalServerError)
.exhaustive();Contributions, issues, and feature requests are welcome! Please see CONTRIBUTING.md for details.
To get started with development, fork the repository and run the following commands:
git clone https://github.com/Harineko0/obj-err.git
cd obj-err
npm installThis project is licensed under the MIT License.