js-lang-exception
Synopsis
An extendable, testable and intuitively usable error-handling Exception class built and based on the standard,
built-in Error object. Written in UMD.
Compatible with ECMAScript 6.
Install
npm install js-lang-exception
Usage - Include/Initialization
define(['js-lang-exception'], function(Exception) {
});
var Exception = require('js-lang-exception');
var Exception = js_lang_exception;
Usage - After Initialization
throw new Exception();
throw new Exception('With a custom message');
throw new Exception(['With a custom message and one argument: {}', 1]);
throw new Exception(['With 2 arguments: {}, {}', 1, 2]);
throw new Exception(['With multiple arguments: {} + {} = {}', 20, 22, 42]);
throw new Exception(['With directly addressed arguments: {2} - {1} - {0}', 1, 2, 3]);
throw new Exception('With another message', 42);
throw new Exception('With another message', 1001);
throw new Exception('With another nice message', 1404, {
custom : false,
data : 1492
});
throw new Exception(['With another nice message'], 1404, {
custom : false,
data : 1492
});
throw new Exception(['With another nice message with: {}, {} and {}', 1, 2, 3], 1404, {
custom : false,
data : 1492
});
throw new Exception('With a message.', null, {custom : 'data'});
throw new Exception(null, 1984, {custom : 'data'});
throw new Exception(null, null, {custom : 'data'});
- Advanced usage with custom exceptions
function CustomException() {
Exception.call(this);
Exception.call(this, 'Custom message', 1001, {custom : 'data'});
}
CustomException.prototype = Object.create(Exception.prototype);
CustomException.constructor = CustomException;
try {
throw new CustomException();
} catch (e) {
console.log(e.hasMessage());
console.log(e.hasID());
console.log(e.hasData());
console.log(e.getMessage());
console.log(e.getID());
console.log(e.getData());
console.log(e instanceof CustomException);
console.log(e instanceof Exception);
console.log(e instanceof Error);
}
class CustomException extends Exception {
constructor() {
super();
super('Custom message', 1001, {custom : 'data'});
}
}
try {
throw new CustomException();
} catch (e) {
console.log(e.hasMessage());
console.log(e.hasID());
console.log(e.hasData());
console.log(e.getMessage());
console.log(e.getID());
console.log(e.getData());
console.log(e instanceof CustomException);
console.log(e instanceof Exception);
console.log(e instanceof Error);
}
Documentation
Check the source
here
since it's well structured and documented. Also you can find the rendered jsDoc documentation on
Doclets.io.
Also, check the unit tests
in order to grasp the full-fledged capabilities.
Have fun! ;)
Issues
If you find any bugs and other issues, check the
GSDC Guide - Issues
section on how to submit issues in a standardized way on
the project's issues page.
In case you have any suggestions regarding the project (features, additional capabilities, etc.), check the
GSDC Guide - Suggestions
section on how to submit suggestions in an easy, standardized way on
the project's issues page.
Contribution
In order to contribute to this project, check the
GSDC Guide
for an easy, standardized way on how to contribute to projects.
Support
If you by any means find this project useful,
consider supporting the organization.
There are multiple options to support the project and the developers.
Any means of support is beneficial and helpful.
License
MIT @ Richard King