def-error
Define errors classes accessible across node processes, on the fly. Catchable with bluebird.
Why ?
Creating a custom in Javascript is a bit of an hassle even though throwing custom prototypes errors is really useful specially with Promises (even more with bluebird Promises). This package allows to create Errors on the fly and intercepting them easily throughout a node process.
Defining and intercepting errors the classic ES2015 way
defining
class CustomError extends Error { constructor(){ super(); }}
Then if you want to use the same error you need to export it and import it. So you will ceate a file let say errors.js and export all your errors this way :
// errors.js { super; } exportsCustomError = CustomError;
If you wanted to add more properties to your error, it would be even more of an hassle.
intercepting (with bluebird Promise)
// index.jsconst CustomError = CustomError;globalPromise = ; Promise
And then, you want to define a new error which can be use anywhere, well, you will have to go back in your errors file and define your error etc.
Defining and intercepting errors with def-error package
Your main script :
// index.jsconst defError = ; Promise
Done.
Moreover the error created here with defError will be available in another module using the defError package.
API
Define an error
const defError = ; const MyCustomError = ; throw ;
Adding custom message
throw 'My custom message';
Adding custom properties to error
You can add custom properties to the Error object by two ways.
Adding properties when defining
const MyCustomError = ;
When you add properties when defining, the properties will be in every instance of the Error created.
let err = ;console // bar
it will be overwritten if you do it again in the second way, which is when calling the constructor :
Adding properting when calling constructor
let err = foo: 'bar2';console // bar2
Or if you want to define a message followed by props
let err = 'my custom error message' foo: 'bar2';console // bar2
Retrieve an error
Retrieving an error is pretty easy, it's the same way as defining an error. If the Error already exist - indexed by name - it will be returned, else it will be created.
const defError = ; const MyCustomError = ;
Error store
All errors are stored in an Object, if you call defError() it will create the Error in the store if the error does not exist, otherwise it will return a the Error class created. Sometimes you might want to create an Error with the same name, but with a different signature. In that case, you can create your Error in a new store. You can do this by two means :