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

x-error

v0.0.10

Published

Utility for creating and extending error objects

Downloads

2,819

Readme

X-Error

Better JavaScript errors

X-Error expands the error handling capabilities of server-side applications by inhereting and extending the vanilla Error object.

Installation

npm install x-error

Then:

var XError = require('x-error');

Usage

XError([code], [message], [data])

X-Error behaves the same way an Error would, new XError('foo') is identical to new Error('foo'). The key difference is that XError provides additional constructor parameters and convenience methods.

You can instantiate X-Error with or without the new keyword, it returns an instance of XError in either case.

Call the constructor with an object to extend its properties:

new XError('hello world', { foo: 'bar', a: 'b'  });

Resulting in:

{
  message: 'hello world',
  foo: 'bar',
  a: 'b',
  stack: '...'
}

Which is equivalent to calling extend directly:

new XError('hello world').extend({ foo: 'bar', a: 'b' });

And:

new XError().setMessage('hello world').extend({ foo: 'bar', a: 'b' });

Extend instances of Error/XError:

var vanillaError = new Error('hello');
var e = new XError(vanillaError).extend({ to: 'world' });

e.message === vanillaError.message; // true
e.stack === vanillaError.stack; // true
e.to === 'world'; // true

var e2 = new XError(e).extend({ message: 'hi' });

e2.message === vanillaError.message; // false
e2.stack === vanillaError.stack; // true
e2.to === 'world'; // true

Enumeration

Enumerate errors with a code by passing a number as the first argument.

new XError(3000, 'hello world', { foo: 'bar' });
/*
{
  code: 3000,
  message: 'hello world',
  foo: 'bar',
  stack: 'Error: hello world ...'
}
*/

HTTP status code & response

One of the most important X-Error features is being able to describe how to handle Errors within the context of HTTP responses.

e.g. using callbacks

User.find(function(err, user) {
  if (err) {
    return callback(XError(err)
                    .setHttpCode(400)
                    .setHttpResponse('Internal error'));
  }
  if (!user) {
    return callback(XError('Unable to find user')
                    .setHttpCode(400)
                    .setHttpResponse('Invalid input'));
  }
});

Then in your app's route controller/handler:

FooService.doStuff(function(err, result) {
  if (err) return res.json(err.httpCode, err.httpResponse);
  res.json(200, result);
});

Use shorthands for more terse API calls:

new XError('Server blew up').hc(500).hr('Internal error');

See API for the complete list.

X-Error with Promises

Combine X-Error with promises to create powerful error handlers.

Take a function that returns a promise:

function createFoo() {
  return User.find(userId)
  .then(function(user){
    if (!user) throw XError().hc(400).hr('Invalid input');
    return Foo.create({ user: user._id });
  })
  .catch(function(e) {
    // wrap and re-throw
    throw XError(e).setSeverity('critical');
  });
}

Create an error handler as a function of the res object:

function createErrHandler(res){
  return function(e) {
    // do some logging depending on severity
    e.severity === 'critical' && logError(e);

    res.status(e.httpCode || 500)
       .send(e.httpResponse || 'Internal error');

  };
}

Call createErrHandler at the end of the promise chain inside your API's route handler

// Foo API
function create(req, res, next) {
  createFoo.then(function(foo) {
    res.status(200).json(foo);
  })
  .catch(createErrHandler(req, res));
}

Better JSON support

X-Error is co-operates with JSON.stringify() by properly enumerating over stack and message properties. This is in contrast with the vanilla Error object which would serialize to an empty object "{}".

Better debugging

Use the built-in debug() method to better diagnose your applications. You can access:

function getStuff(id) {
  Stuff.find({ id: id })
  .then(function(stuff){
    if (!stuff) throw XError().debug(id).hr('oops').hc(400);
    return stuff;
  });
}

function doThings(req, res) {
  getStuff(req.id)
  .then(function(stuff){
    res.status(200).json(stuff);
  })
  .catch(function(err){
    err.debug(req.body);

    // err._debug is now an array containing two items
    // which you can log, or do whatever else
    logErr(err._debug);

    res.status(err.httpCode || 500)
       .send(err.httpResponse || 'Internal error');
  });
}

API

Convenience methods

| Method | Short Name | | ------------------------ | -------------- | | XError.setCode | XError.c | | XError.setMessage | XError.m | | XError.setSeverity | XError.s | | XError.setHttpCode | XError.hc | | XError.setHttpResponse | XError.hr | | XError.extend | XError.ex | | XError.debug | XError.d |

Extensible

Since X-Error is a plain JavaScript constructor you can easily extend its prototype to add custom methods for your application:

var XError = require('x-error');

XError.prototype.setPriority = function(priority) {
  this.priority = priority;
  return this;
};

var e = XError('Update failed')
        .setPriority('high')
        .debug({ accountId: '123' });

License

Standard MIT License