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

@germanamz/errno

v1.4.0

Published

Better errors make your life simpler.

Downloads

69

Readme

Errno

Better errors make your life simpler.

Errno simplifies how errors are handled on reliable systems by providing a clear interface to define known code, messages and statuses.

Usage

Just create an instance

import Errno from 'errno';

const e = new Errno('NOT_FOUND', 'That thing was not found', [
  {
    thingId: 'inexistent',
  },
]);

This will allow you to use errno in a json stringification straight forward.

const res = JSON.stringify(e);

Having the result be

{
  "code": "NOT_FOUND",
  "message": "That thing was not found",
  "context": [
    {
      "thingId": "inexistent"
    }
  ]
}

Known error

A better way of using Errno is by creating a set of known errors.

const NOT_FOUND = (context?: unknown[]) => new Errno('NOT_FOUND', 'Not found', context);

const e = NOT_FOUND({
  thingId: 'inexistent',
});

JSON.stringify(e);

Having the same result as the prev example but with a bit less code

{
  "code": "NOT_FOUND",
  "message": "Not found",
  "context": [
    {
      "thingId": "inexistent"
    }
  ]
}

Note: There's no way for us to know your errors in advance, so the best thing to do is for you to write your own set of known errors

Translate to Errno

You can take a plain Error and translate it to be an Errno using translateToErrno

import { translateToErrno } from 'errno';

const e = translateToErrno(new Error('something went wrong'), 'UNHANDLED_ERROR');

JSON.stringify(e);

Having the json be

{
  "code": "UNHANDLED_ERROR",
  "message": "something went wrong",
  "context": []
}

By doing a simple console.log() you'll get something like this on the console

Errno [Error]: something went wrong
    at REPL23:1:5
    at Script.runInThisContext (node:vm:123:12)
    at REPLServer.defaultEval (node:repl:569:29)
    at bound (node:domain:433:15)
    at REPLServer.runBound [as eval] (node:domain:444:12)
    at REPLServer.onLine (node:repl:899:10)
    at REPLServer.emit (node:events:529:35)
    at REPLServer.emit (node:domain:489:12)
    at [_onLine] [as _onLine] (node:internal/readline/interface:423:12)
    at [_line] [as _line] (node:internal/readline/interface:894:18) {
  code: 'UNHANDLED_ERROR',
  context: [ ],
  [Symbol(isErrno)]: true
}

Remember Errno is an Error subclass which is why if a new instance of Errno is created it will have a e.stack property.

Another fun thing you could do is to implement your own class using the ErrnoI interface, but that's a rabbit hole on its own... so I'll leave it to you.

Additional notes

A very good approach for creating reliable systems is to use some kind of interface for responses between two system parts, neverthrow provides a simple structure for that, in combination with Errno it can have an even more robust interface for error handling and definition.

Licence

Open source licensed as MIT.

Credits

German Meza (germanamz.com / @germanamz)