Giter Club home page Giter Club logo

err's Introduction

@smallwins/err

Slightly better custom Errors

  • Same runtime interface: just pass a message to the constructor
  • Respects extends with clean name and stack properties
  • Adds toObject returns a plain object representation
  • Bundles common HTTP error types with a code property
npm i @smallwins/err --save

Bundled Error Types

  • err.Err a base Error type intended for extending
  • err.InternalError has a code property of 500
  • err.DatabaseError has a code property of 500
  • err.NotFoundError has a code property of 404
  • err.NotAuthorizedError has a code property of 403

Usage

Example usage:

var err = require('@smallwins/err')

let notFound = new err.NotFoundError('missing record')
console.log(err.code) // logs 404

Subclass to add additional properties such as code:

var err = require('@smallwins/err')

class CoffeeError extends err.Err {
  constructor(params) {
    super(params)
    this.code = 500
  }
}

let e = new CoffeeError('lactose intolerant')
console.log(e.code) // logs 500

Get a clean representation:

console.log(e.toObject())
// logs {name, code, message, stack}

Extend by requireing error directly:

var Err = require('@smallwins/err/err')

class TerribleError extends Err {
  constructor(msg) {
    super(msg)
    this.extra = 'extra info'
  }
}

var e = new TerribleError('wut')
console.log(e.extra)

Work oldschool without new:

var err = require('@smallwins/err/oldschool')

console.log(err.Err('basic') instanceof Error)
// logs true

console.log(err.NotFound('not found err').toString())
// logs NotFound: not found err

@smallwins/err/oldschool API

Factory functions which return real Error instances:

  • err.Err returns an Err instance
  • err.Internal returns an InternalError instance
  • err.Database returns a DatabaseError instance
  • err.NotFound returns a NotFoundError instance
  • err.NotAuthorized returns a NotAuthorizedError instance

Runtime type checking fully supported. Check the tests.

err's People

Contributors

brianleroux avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

chenyuan8059

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.