New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

error-kid

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

error-kid

A simple toolkit to work with custom errors. Definitely not a kid.

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

error-kid

NPM Size code-badge

A simple toolkit to work with custom errors. Definitely not a kid.

Installation

# 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') }

Keywords

FAQs

Package last updated on 16 Jan 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc