...is a real Error.
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.md
example.js
index.js
package.json
test.js

README.md

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