UT Error
ut-error is a module that should be used for generating errors everywhere within the ut-bus framework and all implementations based upon it. This is a module that offers functionalities for better error handling in the background and gives possibilities for dynamic error classes declaration and hierarchical chaining.
ut-error should be used mainly through its 'define' method. For example:
var utError = ; var ErrorType1 = utError; var ErrorType2 = utError; var ErrorType3 = utError;
The define method will create a new error type or will just return it if it was previously defined. It will also build the hierarchy between the error classes automatically. After defining the error types, the errors themselves can be instantiated as follows:
var e1 = ;// same as://var e1 = new ErrorType1('e1 message');var e2 = ;var e3 = ;
The errors are actually real 'Error' objects but additionally the following inheritance is achieved:
console // -> trueconsole // -> true console // -> trueconsole // -> trueconsole // -> true console // -> trueconsole // -> trueconsole // -> trueconsole // -> true
And the 'type' properties of the error instances themselves are as follows.
console // -> 'ErrorType1'console // -> 'ErrorType1.ErrorType2'console // -> 'ErrorType1.ErrorType2.ErrorType3'
The 'define' method accepts 2 arguments: name
and superType
, where:
name
(required) is a string, specifying the name of the error class ()superType
(optional - if inheritance is desired) is a function (i.e an already defined errorType class) or a string, representing the type of an already defined errorType class.
superType can accept either a function or a string, which means that:
var utError = ; var ErrorType1 = utError; var ErrorType2 = utError;/* is the same as:var ErrorType2 = utError.define('ErrorType2', 'ErrorType1');*/ var ErrorType3 = utError;/* is the same as:var ErrorType3 = utError.define('ErrorType3', 'ErrorType1.ErrorType2');*/
The error constructors that get generated accept only 1 argument but that argument is actually very flexible: it can be undefined, string, object or even a real javascript exception.
So errors can be generated either way:
var generateError = ; var e1 = ;var e2 = ;var e3 - ;var e4 - ;/* i.e.try { somethingWrong();} catch (e) { throw generateError(e);}*/
Recommended pattern for defining errors
Create a file errors.js in your module, having contents following this pattern:
'use strict';const create = define;const Category1 = ;const Category2 = ; moduleexports = category1: Category1 errorA: errorB: errorC: category2: Category2 errorD: errorE: errorF: ;
Use the errors, following these patterns:
var errors = ; { throw errors;} { throw errors;} { throw errors;} { try ; catch e throw errors; } { throw errors;} { return Promise;}
Error fields
A serialized error is represented by the following JSON
Property | Type | Required | Description |
---|---|---|---|
cause | String | no | In case another error caused this error. This allows for error chaining as the inner error can also have a cause and so on. |
message | String | yes | Human readable error message |
type | String | yes | The error's unique identifier. Each error has an unique type. The type can also illustrates the error class hierarchy - in case there is an error which is an instance of class B with type 'b' inheriting class A with type 'a' then the error would have type: 'a.b' |
level | String | no | The error serverity level - could be error, fatal, etc. for additional classification |
method | String | no | in the context of what method had the error been raised |