Ex-Error
An extendible error class for creating custom error.
Why Use This Package?
Custom error object is quite useful if you need to filter specific error type. You can read this article to get more context why custom error is very useful.
Example:
// With standard error objecttry // Normally one will throw an error throw 'Error on auth'; catch err // catch error in here // With custom error objecttry throw 'Your password is missing'; catch err if err instanceof AuthError // process error for AuthError else // process error for other than AuthError
Using Package
;// orconst exError = ; // creating default error object from standard Error classconst DefaultError = ; try // adding error message throw 'Error using default error'; catch e console; // -> 'Error using default error' console; // -> 'Error' console; // -> true // creating custom error objectconst CustomError = ; try // adding error message and custom properties throw 'Custom error' code: 500 otherMessage: 'other message' ; catch e console; // -> 'Custom error' console; // -> 'CustomError' console; // -> true console; // -> 500 console; // -> 'other message'
One can also use it as class extension
; const CustomError = ; {} try // adding error message and custom properties throw 'Custom error' code: 500 otherMessage: 'other message' ; catch e console; // -> 'Custom error' console; // -> 'CustomError' console; // -> true console; // -> 500 console; // -> 'other message'
License
Licensed under the MIT License. You can find a copy of it in LICENSE.