New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

obj-err

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

obj-err

Common object error library specifically for functional typescript.

latest
Source
npmnpm
Version
1.1.3
Version published
Maintainers
1
Created
Source

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.

Keywords

typescript

FAQs

Package last updated on 21 Oct 2025

Did you know?

Socket

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.

Install

Related posts