Create custom errors that inherit Error
custom-errorCreate custom errors that inherit Error.
whyExtending 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.
usageCalling 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.
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-errorlicense
MIT
HomePage
https://github.com/andrezsanchez/custom-error