aim-error-at
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.
Installation
$ npm install aim-error-at --save --save-exact
Usage
var aimErrorAt = ;
Modify existing error
var err = ;// => assert(err.exit === 'notCandy' && err.message === 'Hello')// => assert(err.constructor.name === 'Error')
Build new error from a message
var err = ;// => assert(err.exit === 'notCandy' && err.message === 'Hello')// => assert(err.constructor.name === 'Error')
Build anonymous error
var err = ;// => 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 _;catch e if eexit && eexit === 'notCandy' // ... handle as you like here ... // // e.g. etotalNumCandies = candieslength; throw e; else // Some other unexpected error. throw e; // Otherwise it worked.return;
In an asynchronous scenario:
async;
In a machine definition
moduleexports = // ... exits: success: description: 'All potential candies have been confirmed as such.' notCandy: description: 'One or more of the potential candies is NOT CANDY.' { var aimErrorAt = ; try inputspotentialCandies; catch e if eexit && eexit === 'notCandy' // ... handle however you like here ... // // e.g. return exits; // (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 ; // Otherwise it worked. return exits; } ;
In a Sails.js action:
if !_ return res; async;
About
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