Giter Club home page Giter Club logo

perr's Introduction

perr

npm version javascript standard style travis build coveralls coverage david dependencies david dev dependencies

Portable errors for Javascript

npm install perr


usage

import perr from 'perr'

//
// Perr lets you create "portable" error objects to use throughout
// your applications.
//

const plainError = new Error('something happened')
plainError.statusCode = 500
String(plainError) // Error: something happened
JSON.parse(JSON.stringify(plainError))
// {
//   "statusCode": 500
// }

//
// Now, let's try to create a portable error out of this.
// perr.convert(err, props) takes an Error instance and an
// optional "props" object, and returns an Error instance,
// modified to be JSON stringifiable
//

const portableError1 = perr.convert(plainError)
String(portableError1) // Error: something happened
JSON.parse(JSON.stringify(portableError1))
// {
//   "name": "Error",
//   "message": "something happened",
//   "stack": "Error: something happened\n   at <anonymous>:1:20"
// }

//
// Even if the plain error's properties are enumerable, perr.convert(err, props)
// does not pick them onto the portable error unless you explicitly specify
// so.
// This is to prevent surprises, since with modules like `got()`, errors can
// have additional properties which aren't necessarily enumerable.
//

const portableError2 = perr.convert(plainError, {
  statusCode: plainError.statusCode,
  somethingElse: 'nice'
})
String(portableError2) // Error: something happened
JSON.parse(JSON.stringify(portableError2))
// {
//   "name": "Error",
//   "message": "something happened",
//   "stack": "Error: something happened\n   at <anonymous>:1:20",
//   "statusCode": 500,
//   "somethingElse": "nice"
// }

//
// perr.toObj(err, props) creates a plain object from an error.
// This is a more efficient equivalent of calling
// JSON.parse(JSON.stringify(perr.toObj(err, props)))
//
// This works on both plain and "portable" errors.
//

const plainErrObj = perr.toObj(plainError, {
  somethingElse: 'nice'
})
// {
//   "name": "Error",
//   "message": "something happened",
//   "stack": "Error: something happened\n   at <anonymous>:1:20",
//   "somethingElse": "nice"
// }

//
// perr.toErr(obj) converts an error object to a portable Error
// instance. The error will be run through the guaranteed-error module,
// to fill in name, message, and stack, if they are missing.
//
// If obj is a simple string, that will be the error's `message`.
//

const createdError1 = perr.toErr({
  name: 'Error',
  message: 'something happened',
  stack: 'Error: something happened\n   at <anonymous>:1:20',
  somethingElse: 'nice'
})
createdError1 instanceof Error // true
createdError1.stack // Error: something happened\n   at <anonymous>:1:20
createdError1.somethingElse // nice

const createdError2 = perr.toErr({
  stack: 'asdf: something happened'
})
createdError2.name // Error
createdError2.message // <No error message provided>
createdError2.stack // asdf: something happened

const createdError3 = perr.toErr('something happened')
createdError3.name // Error
createdError3.message // something happened
createdError3.stack // Error: something happened\n    at <unknown>

perr's People

Stargazers

abe haile avatar

Watchers

James Cloos avatar ivan k avatar

perr's Issues

what i want

side note: for cross-platform compatibility, we should only require error.message, since errors like golang errors don't have names or stacks, where error.name would default to "Error", and error.stack would default to " at <unknown>"


  • toString() works
  • json-ifiable
  • err.name, err.message, err.stack as base (guaranteed) fields
  • additional error fields (???)
  • convert(err, {fields}), recreates the error, and appends fields to it (which will show up in the json format)
  • to(), creates error from plain object (run through guaranteedError)
  • from(), creates plain object from error (undefined if no error)

doc fix

// This is a more efficient equivalent of calling
// JSON.parse(JSON.stringify(perr.toObj(err, props)))

->

// This is a more efficient equivalent of calling
// JSON.parse(JSON.stringify(perr.convert(err, props)))

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.