Coder Social home page Coder Social logo

serializerr's Introduction

serializerr

Convert Errors & Objects into an easily-serialized vanilla Object.

Build Status

NPM NPM

serializerr creates a vanilla Object with a flattened prototype chain & any non-enumerable properties mapped to enumerable properties.

This allows Error objects to be serialised to JSON without losing important data.

Installation

npm install serializerr

Usage

var wellSerializedError = JSON.parse(JSON.stringify(
  serializerr(error)
))

console.log(wellSerializedError.name) // Error
console.log(wellSerializedError.message) // an error occurred
console.log(wellSerializedError.stack) // Error: an error occurred\n  at Test.<anonymous> ...

Example

var serializerr = require('serializerr')

var error = new Error('an error')

// simulate transferring an Error object over the wire as JSON
// without first passing through serializerr
var poorlySerializedError = JSON.parse(JSON.stringify(error))

// oh dear:
console.log(poorlySerializedError.name) // undefined
console.log(poorlySerializedError.message) // undefined
console.log(poorlySerializedError.stack) // undefined

// bring forth the serializerr
var errorObject = serializerr(error)

var wellSerializedError = JSON.parse(JSON.stringify(errorObject))

// properties are conserved!
console.log(wellSerializedError.name) // Error
console.log(wellSerializedError.message) // an error occurred
console.log(wellSerializedError.stack) // Error: an error occurred\n  at Test.<anonymous> ...

// note errorObject is a vanilla Object, not an Error
errorObject instanceof Error // false

Why

If you've ever tried to send an Error over a JSON-encoded connection you've probably been surprised to find all the useful information is sapped out of it; all the juicy properties like name, message & stack are non-enumerable thus they are not included in the stringified JSON. This may be non-standard behaviour, as I could not locate any mention in either the ES5.1 or the ES6 spec about it, but Error properties are non-enumerable both in V8 (Chrome/io.js/Node.js) & SpiderMonkey (Firefox).

I believe Error property non-enumerability was added as a security measure to prevent stack traces and other sensitive information accidentally leaking, but it's not uncommon to actually want to send the data in Error objects over the wire.

serializerr makes an Object suitable for serializing to & from JSON. Not restricted to use with Errors, will work with any Object.

Notes on 'ize' vs 'ise'

Name was selected as programming world is mostly Americanised, and npm search does not seem to do effective stemming.

This decision came with strong feelings of guilt and shame about what I thought was blasphemous Americanised spelling, but it turns out this is a misconception thus I am pardoned:

The -ize spelling is often incorrectly seen as an Americanism in Britain. However, the Oxford English Dictionary (OED) recommends -ize and notes that the -ise spelling is from French.

From Wikipedia: American and British English spelling differences

License

ISC

serializerr's People

Contributors

timoxley avatar greenkeeperio-bot avatar

Stargazers

Anton Kudinov avatar Noel Levy avatar Grant Carthew avatar Martin Hecher avatar Mathieu M-Gosselin avatar Kevin Nguyen avatar Jerzerak avatar Athan avatar Joseph Dykstra avatar Avétis KAZARIAN avatar Chris Buttery avatar  avatar Marwan Hilmi avatar Ellen Gummesson avatar Michael Rhodes avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.