error-system

1.0.1 • Public • Published

error-system

build status Coverage Status Dependency status Dev Dependency status

NPM js-standard-style

Your custom errors in your JavaScript code!

Inspired by node-errno.

Based on errors in bitcore.

Example

createError

var errorSystem = require('error-system')
var RequestError = errorSystem.createError('RequestError', 'Code: {0} (url: {1})')
var request = require('request')
 
var url = 'https://github.com/notfound11'
request(url, function (error, response) {
  if (error === null && response.statusCode !== 200) {
    error = new RequestError(response.statusCode, url)
  }
 
  if (error !== null) {
    // ErrorRequestError: Code: 404 (url: https://github.com/notfound11)
    console.error(error.stack.split('\n')[0])
  }
})

extend

var errorSystem = require('error-system')
var RequestError = errorSystem.extend(Error, [{
  name: 'RequestError',
  message: 'Code: {0} (url: {1})',
  errors: [
    {
      name: 'NotFound',
      message: 'Code: 404 (url: {0})'
    }
  ]
}])
var request = require('request')
 
var url = 'https://github.com/notfound11'
request(url, function (error, response) {
  if (error === null && response.statusCode !== 200) {
    if (response.statusCode === 404) {
      error = new RequestError.NotFound(url)
    } else if (response.statusCode !== 200) {
      error = new RequestError(response.statusCode, url)
    }
  }
 
  if (error !== null) {
    // ErrorRequestErrorNotFound: Code: 404 (url: https://github.com/notfound11)
    console.error(error.stack.split('\n')[0])
  }
})

License

Code released under the MIT license.

Readme

Keywords

Package Sidebar

Install

npm i error-system

Weekly Downloads

769

Version

1.0.1

License

MIT

Last publish

Collaborators

  • fanatid