pinocchio

0.1.0 • Public • Published

Pinocchio

...is a real Error.

Born out of frustration at certain libraries returning "errors" that were actually strings (sometimes plain and sometimes as JSON blobs) or vanilla objects, this library takes just about anything passed to it and turns it into something that is instanceof Error.

Because hey, better late than never.

Example:

example.js:

var pinocchio = require('./');

var str, json, halfJson, obj;

//
// It works on strings!
//
str = 'That\'s not an error!';
console.log(pinocchio(str));

//
// It's smart about JSON strings...
//
json = JSON.stringify({
  code: 200,
  message: 'a JSON string with a message'
});
console.log(pinocchio(json));

//
// ...and handles some embedded JSON strings too
//
halfJson = 'I\'M HELPING!! ' + JSON.stringify({ payload: 'ponyyyyyy' });
console.log(pinocchio(halfJson));

//
// Of course, it can also handle vanilla objects
//
obj = { code: 404, message: 'not_found'};

//
// We can also get a little passive-aggressive here (or clarify what the code
// is doing, either way)
//
console.log(pinocchio.isARealError(obj));

log.log:

josh@onix:~/dev/pinocchio$ node example.js 
{ [Error: That's not an error!] fails: [ 'is a string' ] }
{ [Error: a JSON string with a message]
  code: 200,
  fails: [ 'is a string', 'is a JSON-encoded string' ] }
{ [Error: I'M HELPING!! {"payload":"ponyyyyyy"}]
  payload: 'ponyyyyyy',
  fails: [ 'is a string', 'is a string with a JSON fragment inside it' ] }
{ [Error: not_found] code: 404, fails: [ 'is a non-error object' ] }

License:

MIT

Package Sidebar

Install

npm i pinocchio

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

7.12 kB

Total Files

6

Last publish

Collaborators

  • jfhbrook