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 🙏

© 2024 – Pkg Stats / Ryan Hefner

error3

v3.1.1

Published

Error3 is proper JS error implementation. It supports error codes, message formatting (e.g. i18n) and nested errors.

Downloads

1,906

Readme

Error3 is an Error with extra powers. It has been designed to be extensible and easy to use. Though it has codes, message formatters and nested errors.

  • Modern: designed for TypeScript and ES2019.
  • IDE friendly: it's using classes and class fields to be inspectable for autosuggetion tools.
  • i18n ready: formatter could produce localized messages with help of Intl API.
  • Easy serialization and deserealization: good for network apps and JSON logging.
  • Frontend caring: 0 dependencies, gzipped version is less then 1 KiB.

Table of Contents

Install

Usage

Error3 suppose that you will create some base error class for your application or library and then use it as a parent for all your errors. Watch example in examples folder. Here it is interface realization:

import Error3 from 'error3'

class NotFoundErr extends Error3 {
  code = 'fs_not_found'

  format({filepath}) {
    return `File "${filepath}" not found`
  }
}

This is what it gives to us:

const error = new NotFoundErr({filepath: './index.js'});
error.toString() // -> "NotFoundErr: [#fs_not_found] File "./index.js" not found"
error.message // -> "File "./index.js" not found"
error.code // -> fs_not_found
error.details // -> {filepath: './index.js'}

The same error TypeScript implementation:

import Error3 from 'error3'

class NotFoundErr extends Error3<{filepath: string}, void> {
  code = 'fs_not_found'

  format({filepath}): string {
    return `File "${filepath}" not found`
  }
}

JSON serialization

Calling Error3#toJSON() on Error3 instance returns an object with properties code, message, details, and errors. Example output:

{
  "code": "fs_not_found",
  "message": "File \"./index.js\" not found",
  "details": {
    "filepath": "./index.js"
  },
  "errors": []
}

Examples

  • HTTP errors JS · TS
  • FileSystem errors JS · TS
  • Localized I18n error messages JS · TS

API

Error3()

(details:object = {}, errors:Error[] = []) -> Error3

abstract. Both of Error3 constructor arguments are optional. The resposibility of ancestor class is to implement proper interface and pass details object and errors list into super() call.

details is using to describe error with JS primitives. Though it could be sent via network to frontend, db, or ELK without extra parsing as it should be done with regular Error instance.

TS Interface

abstract class Error3<Details, Errors> extends Error implements IError3 {
  public readonly code: string|number
  public readonly name: string
  public readonly details: object
  public readonly errors: Error[]

  constructor(details: Details, errors: Errors) {}
  abstract format(detials: Details, errors: Errors): string
}

Example

const error = new UserMissed(
  {userId: 1}, [new Error('Collection removed')]
);

error.code // -> user_missed
error.message // -> User #1 not loaded
error.details // -> {userId: 1}
error.errors // -> [Error('Collection removed')]

Error#code

string|number

Error code should be a string or a number. It could be defined using class fields syntax:

class HttpNotFound extends HttpError {
  code = 404
}

Error3#format()

(details: object, errors: Error[]) -> string

abstract. Creates formatted message string from details and other errors. This method is calling from Error3 constrcutor to define message property.

JS

class PortInUse extends Error3 {
  format({port}) {
    return `Port ${port} is already in use`
  }
}

TS

class PortInUse extends Error3<{port: string|number}, void> {
  format({port}): string {
    return `Port ${port} is already in use`
  }
}

Error3#toJSON()

Wrapper of Error3#valueOf. It's created to be used by JSON.stringify().

Error3#valueOf()

() -> PlainError

This method realizes Object#valueOf() behavior and returns plain error object containing properties: code, message, details and errors.

PlainError{}

{
  code: string|number,
  message: string,
  details: object,
  errors: PlainError[],
}

It is a result of Error3#valueOf() call.

License

MIT © Rumkin