pinocchio a real Error. Takes non-errors and makes them real errors, with bonus complaints

npm install pinocchio
1 downloads in the last week
6 downloads in the last month

Pinocchio 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.



var pinocchio = require('./');

var str, json, halfJson, obj;

// It works on strings!
str = 'That\'s not an error!';

// It's smart about JSON strings...
json = JSON.stringify({
  code: 200,
  message: 'a JSON string with a message'

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

// 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)


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' ] }



npm loves you