npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@biorate/errors

v3.1.2

Published

Errors factory

Downloads

2,050

Readme

@biorate/errors

Base class for creating typed, code‑based error classes with structured metadata and util.format‑style message templates.

Features

  • Error code — auto‑derived from the constructor name (e.code === 'MyError').
  • Templated message — uses util.format %s / %d / %j placeholders.
  • Structured metadata — any extra payload attached via the meta getter.
  • Zero dependencies in runtime — only built‑in util module.
  • Full Error contract — preserves stack, message, name, and captureStackTrace.

Installation

pnpm add @biorate/errors

Quick start

import { BaseError } from '@biorate/errors';

class MyError extends BaseError {
  constructor(args?: unknown[], meta?: unknown) {
    super('Something happened at %s, code %d', args, meta);
  }
}

const err = new MyError([new Date(), 42], { userId: 1 });

console.log(err.code);    // "MyError"
console.log(err.message); // "Something happened at Sun Jun ... 2025, code 42"
console.log(err.meta);    // { userId: 1 }
console.log(err.stack);   // Full stack trace

API Reference

BaseError

| Member | Type | Description | |--------------------|------------------------------|----------------------------------------------------------| | constructor | (message, args?, meta?, ...options?) | Template string with positional util.format args. | | .message | string | Formatted message (inherited from Error). | | .code | string | Returns this.constructor.name. | | .meta | unknown | Arbitrary metadata payload (second constructor arg). | | .stack | string \| undefined | Stack trace (V8 captureStackTrace when available). |

Constructor signature

new BaseError(message: string, args?: unknown[], meta?: unknown, ...options: unknown[]);
  • message — template using %s, %d, %j, %% etc. (Node.js util.format).
  • args — array of values injected into the template.
  • meta — any extra data stored on this.meta.
  • ...options — additional arguments forwarded to Error constructor (e.g. options.cause).

Usage patterns

Basic typed errors

class NotFoundError extends BaseError {
  constructor(id: string) {
    super('Resource not found: %s', [id]);
  }
}

throw new NotFoundError('user_42');
// NotFoundError: Resource not found: user_42

Errors with structured metadata

class ValidationError extends BaseError {
  constructor(field: string, value: unknown) {
    super('Validation failed for [%s]', [field], { field, value });
  }
}

const e = new ValidationError('email', 'invalid');
console.log(e.meta); // { field: 'email', value: 'invalid' }

Overriding HTTP status via meta

class PaymentError extends BaseError {
  constructor(msg: string) {
    super(msg, undefined, { status: 402 });
  }
}

const e = new PaymentError('Insufficient funds');
console.log(e.meta); // { status: 402 }

This pattern is used by AllExceptionsFilter from @biorate/nestjs-tools to return proper HTTP status codes.

Serialization with toJSON

class ApiError extends BaseError {
  constructor(code: number, message: string) {
    super('%s', [message], { status: code });
  }

  toJSON() {
    return { code: this.code, message: this.message, meta: this.meta };
  }
}

console.log(JSON.stringify(new ApiError(400, 'bad request')));
// {"code":"ApiError","message":"bad request","meta":{"status":400}}

Architecture

┌─────────────────┐
│   BaseError     │  extends  Error
├─────────────────┤
│  #meta: any     │
│  .code          │  → constructor.name
│  .message       │  → util.format(template, ...args)
│  .meta          │  → #meta getter
│  .stack         │  → Error.captureStackTrace
└─────────────────┘
        ▲
        │ extends
┌───────────────────┐
│   MyAwesomeError  │  ← custom error per domain
│   super(msg,args, │
│     meta)         │
└───────────────────┘

Best practices

  1. One class per error type — allows instanceof checks and catch filtering.
  2. Use %s placeholders for dynamic values, never string concatenation in the template.
  3. Add meta.status for HTTP errors to propagate status codes to NestJS filters.
  4. Keep class names descriptive — they become e.code, visible in logs.

Learn

  • Documentation can be found here - docs.

Release History

See the CHANGELOG

License

MIT

Copyright (c) 2021-present Leonid Levkin (llevkin)