custom-error
Create custom errors that inherit Error
Last updated 11 years ago by andrezsanchez .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install custom-error 
SYNC missed versions from official npm registry.

custom-error

Create custom errors that inherit Error.

build status

why

Extending Error is a real pain, so this library deals with the quirks, providing a clean API to extend Error that works across JS environments, including Node and browsers.

usage

Calling customError(errorTitle[, ParentError]) creates a new error class that can be called the same way that Error is called. The new error constructor will inherit from Error, or from ParentError if you provide it.

examples

var customError = require('custom-error');

var ApocalypseError = customError('ApocalypseError');

ApocalypseError() instanceof Error           // true
ApocalypseError() instanceof ApocalypseError // true

var UnixApocalypseError = customError('UnixApocalypseError', ApocalypseError)

UnixApocalypseError() instanceof Error                 // true
UnixApocalypseError() instanceof ApocalypseError       // true
UnixApocalypseError() instanceof UnixApocalypseError   // true

if (new Date().getFullYear() === 2038) {
  throw UnixApocalypseError('OH NOES')
}

Using prototype

UnixApocalypseError.prototype.year = 2038

try {
  throw UnixApocalypseError()
}
catch (err) {
  console.log(err.year) // 2038
}

installation

npm install custom-error

license

MIT

Current Tags

  • 0.2.1                                ...           latest (11 years ago)

4 Versions

  • 0.2.1                                ...           11 years ago
  • 0.2.0                                ...           11 years ago
  • 0.1.1                                ...           12 years ago
  • 0.1.0                                ...           12 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (3)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |