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

aim-error-at

v0.2.6

Published

Build or modify an Error so that it is aimed towards the specified exit. (useful in synchronous machines, big switch statements, and loops inside of try/catch blocks)

Downloads

13

Readme

Build or modify an Error so that it is aimed towards the specified exit.

Useful in synchronous machines, big switch statements, and loops inside of try/catch blocks. Also useful outside of the context of machines for assigning useful error codes to the errors sent back by ANY JavaScript function.

NPM

Installation   NPM version

$ npm install aim-error-at --save --save-exact

Usage

var aimErrorAt = require('aim-error-at');

Modify existing error

var err = aimErrorAt('notCandy', new Error('Hello'));
// => assert(err.exit === 'notCandy' && err.message === 'Hello')
// => assert(err.constructor.name === 'Error')

Build new error from a message

var err = aimErrorAt('notCandy', 'Hello');
// => assert(err.exit === 'notCandy' && err.message === 'Hello')
// => assert(err.constructor.name === 'Error')

Build anonymous error

var err = aimErrorAt('notCandy');
// => assert(err.exit === 'notCandy')
// => assert(err.constructor.name === 'Error')

Examples

A few examples of using this module to handle errors in different scenarios.

In a synchronous scenario:

try {
  _.each(candies, function (thisCandy) {
    throw aimErrorAt('notCandy', 'That\'s not a proper piece of candy!');
  });
}
catch (e) {
  if (e.exit && e.exit === 'notCandy') {
    // ... handle as you like here ...
    //
    // e.g.
    e.totalNumCandies = candies.length;
    throw e;
  }
  else {
    // Some other unexpected error.
    throw e;
  }
}

// Otherwise it worked.
return;

In an asynchronous scenario:

async.each(candies, function (thisCandy, next) {
  return next( aimErrorAt('notCandy', 'That\'s not a proper piece of candy!') );
}, function afterwards(err) {
  if (err.exit && err.exit === 'notCandy') {
    // ... handle as you like here ...
    //
    // e.g. 
    console.warn('uh oh');
    err.totalNumCandies = candies.length;
    return cb(err);
  }
  else if (err) {
    // Some other unexpected error...
    return cb(err);
  }

  // Otherwise it worked.
  return cb();
});

In a machine definition

module.exports = {

  // ...
  
  exits: {
    
    success: {
      description: 'All potential candies have been confirmed as such.'
    },
    
    notCandy: {
      description: 'One or more of the potential candies is NOT CANDY.'
    }
    
  },


  fn: function (inputs, exits) {
    var aimErrorAt = require('aim-error-at');

    try {
      inputs.potentialCandies.forEach(function (item){
        if (item !== 'candy') {
          throw aimErrorAt('notCandy');
        }
      });
    }
    catch (e) {
      if (e.exit && e.exit === 'notCandy') {
        // ... handle however you like here ...
        //
        // e.g.
        return exits.notCandy({ totalNumCandies: inputs.potentialCandies.length });
        // (note that we could have just used `return exits(e)` and the runner
        //  would have figured it out; calling the appropriate exit.
        //  Also since we're not inside a callback at this point in
        //  the code, simply throwing the error would technically work
        //  too, since the machine runner catches and handles all immediate
        //  errors)
      }
      else {
        // Using switchback to act like throwing:
        return exits(e);
      }
    }
    
    // Otherwise it worked.
    return exits.success();
  }
  
  
};

In a Sails.js action:

if (!_.isArray(req.param('candies'))) {
  return res.badRequest('`candies` should be provided as a JSON-encoded array.'); 
}

async.each(req.param('candies'), function (thisCandy, next) {
  return next( aimErrorAt('notCandy', 'That\'s not a proper piece of candy!') );
}, function afterwards(err) {
  if (err.exit && err.exit === 'notCandy') {
    // ... handle as you like here ...
    //
    // e.g. 
    res.status(401);
    return res.json({ totalNumCandies: candies.length });
  }
  else if (err) {
    // Some other unexpected error...
    return res.serverError(err);
  }

  // Otherwise it worked.
  return res.ok();
});

About   ![Gitter](https://badges.gitter.im/Join Chat.svg)

Learn more about the project and our goals at http://node-machine.org/implementing/FAQ or check out the project newsgroup.

License

MIT © 2016 Mike McNeil and contributors