extended-exceptions.js
Introduction
Allow easy declaration of custom javascript errors/exceptions.
Also provides common exceptions for Javascript that are missing in the standard : RuntimeError, NotImplementedError, InvalidArgument, OutOfRange, etc. (inspiration taken from the C++11 standard)
Works in node.js + browser, AMD.
Full testsuite. No dependencies.
Includes special workarounds for Firefox "Error" object which has sometimes a strange behaviour.
License : public domain (http://unlicense.org/)
Interesting reads :
Predefined errors
Note : we keep the "error" naming scheme of standard javascript.
Error (standard) <-- javascript root exception
+ EvalError <-- Vanilla javascript exceptions
+ RangeError <-- ...
+ ReferenceError <-- ...
+ SyntaxError <-- ...
+ TypeError <-- ...
+ URIError <-- ...
+ ExtendedError <-- Here start our custom exceptions
+ LogicError
+ InvalidArgument
+ LengthError
+ OutOfRange
+ InvariantNotMet
+ RuntimeError
+ NotImplemented
+ UnknownEnumValue
+ IllegalState
Usage
if typeof define !== 'function' var define = module; // node only
Defining our own exceptions inheriting from an existing one :
Catching and testing exceptions :
- exactly as you expect it.
Works in chai exactly as you expect it :
if typeof define !== 'function' var define = module; // node only ; // requirejs module
Installation
Bower : bower install extended-exceptions.js
Npm : npm install extended-exceptions
Unit tests
Browser : open test_runner/mocha.html
Node : npm test