Skip to content

Harineko0/obj-err

Repository files navigation

obj-err

npm version License Actions Status

Common object error library specifically for functional typescript.

Installation

Using npm:

npm install obj-err

Or using yarn:

yarn add obj-err

Usage

Define a Custom Error

import { 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>;

Throwing the Custom Error (e.g. using neverthrow)

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);
};

Handling the Custom Error (e.g. using ts-pattern)

const handleError = (
  error: InputTooShortError | NotFounderror | AnotherError
) =>
  match(error)
    .with(InputTooShortError.is, () => StatusCode.BadRequest)
    .with(NotFounderror.is, () => StatusCode.NotFound)
    .with(AnotherError.is, () => StatusCode.InternalServerError)
    .exhaustive();

Contributing

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 install

License

This project is licensed under the MIT License.


Copyright © 2025 Harineko0.
This project is MIT licensed.