Create custom errors for your JavaScript projects
1- in browser:
<scriptsrc="/js/hellrazor.min.js"></script>
2- via NPM:
You'll usecreateError()
method to create an error, it takes two strings as parameters forError Name
&Error Message
and will return the new errorFunction
// if you have commonjs in your project// if not skip this linevarHellrazor=require("hellrazor");// The below is the same in any JavaScript environment (Browser or Node.js)vare=Hellrazor.createError("erropac","use hellrazor 'cause Chrome can't always be your savior");try{// you can re-assign the message if you wantthrownewe("new message");}catch(err){console.log(err.name);// erropacconsole.log(err.message);// new messageconsole.log(err.stack);console.log(errinstanceofError);// trueconsole.log(errinstanceofe);// true}
You can extra objects to the custom error usingprototype
, for example the time when the error occurred
vare=Hellrazor.createError("errorName","errorMsg");try{e.prototype.happenAt=newDate();thrownewe();}catch(err){console.log(e.happenAt);}
This project is under the MIT license.