This module provides an extendable javascript exception class for easy declaration of custom exceptions.
class HttpException extends Exception {
constructor(message, statusCode) {
super(message);
//set http status
this.httpStatus = statusCode;
}
}
try {
throw new HttpException('Page not found', 404);
} catch (error) {
console.log(error.httpStatus); // equals to 404
}
A common problem with Javascript compared to a language like Java is that the exception handling and ability to throw and use custom exceptions is not simple. This library aims to provide a common wrapper class to provide the extendability that's needed in this case.
npm install @udantha/exception
Import/require the class on your file and extend it. Simply throw your custom object instead of javascript 'Error'
npm test
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.