1.0.1 • Published 9 years ago

error-system v1.0.1

Weekly downloads
920
License
MIT
Repository
github
Last release
9 years ago

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.