@pmbservices/pmbstoreerrormanager
v1.0.0
Published
Error handler for all modules of PMBStore
Downloads
2
Readme
PMBStore ErrorManager
PMBStore ErrorManager is used to throw errors by others module with usefull information.
How to use the ErrorManager in others modules
Just require ErrorManager in each class file of the module:
const ErrorManager=require('ErrorManager');
Then when you want to throw an error call throw static method :
ErrorManager.throw(code,[e],[options]);
code
is the code of the error defined in the file errorsDef.json
, e is optional and may be the error catched who lead you to throw this error, options
is an object containing properties to replace in the message defined in errorsDef.json
(see below).
the file errorsDef.json
It must contain an object with properties that have the same name as the class names in the module. The value associated with each property is on object containing properties for each code and an associated a message. In the message you can put some variables name between '%' that will be replaced when the error is thrown.
Example
If you have 2 classes Disk
and Record
in a PMBStoreDisk module that can throw errors, then create an errorsDef.json
containing :
{
"Disk":{
"CantOpenRW":"Can't open disk %disk% in read/write mode",
"Read":"Read error at position %position%",
"NoPath":"Please provide path of the disk"
},
"Record":{
"BadHeader":"Header of the record is not constitent",
}
}
The PMBStoreError error class
This class extends the javascript Error class. The name
property is the code of the error and message
the message in errorsDef.json
with variables substituted by the properties of the options
object. It has also specific fields :
jsclass
: the class of the module who thrown this errormodule
: the module of PMBStore from where this error where throwneOrigin
: the original error catched by the module who led to throw this erroroptions
: theoptions
object who contain the variables value of the message. You may also put extra properties that you need to manage this error
Example
e
is the error thrown by fs.openSync
ErrorManager.throw('CantOpenRW',e,{
"disk":"/dev/sdb"
});
will throw a PMBStoreError object like this :
PMBStoreError {
name:"CantOpenRW",
message:"Can't open disk /dev/sdb in read/write mode",
jsclass:"Disk",
module:"pmbstoredisk",
eOrigin:e, //error thrown by fs.openSync
options:{
"disk":"/dev/sdb"
}
}