Terror
Base error class which will help you organize errors, generate informative logs and as result grep logs more effectively.
Trivial usage
var Terror = // declare error type with custom error codes and messages MyError = Terror; try // ... if typeof userInput === 'undefined' // throw error throw MyErrorCODESSTRANGE_THING_HAPPENS; // or // throw MyError.createError(MyError.CODES.STRANGE_THING_HAPPENS); catch err // ensureError method returns err if it is an instance of MyError, // otherwise wrap Error instance in the MyError with default code UNKNOWN_ERROR MyError ;
Constructor
code originalError;code message;code;;
Also can be called as a function, then calls self as the constructor internally and returns created instance.
Both arguments are not required:
- if only
code
passed, message will got fromTerror.MESSAGES
hash by thecode
value as the key; - if
code
is absent, will be used default valueTerror.CODES.UNKNOWN_ERROR
.
Constructor methods
create(name, codes)
Returns constructor inherited from Terror. You must specify name
for logging purpose.
Arguments:
{String} name
– Error class name for logging;{Object} [codes]
- CODES hash { CODE_NAME: 'error message', … }
Example:
var AppError = Terror ControllerError = AppError;
extendCodes(codes)
Extends CODES
and associated MESSAGES
hashes using codes
declaration. Method throws an error if codes uniqueness violated by extension.
Example:
var AppError = Terror ; // now you can use AppError.CODES.BROKEN_CONFIG & AppError.CODES.DB_CONNECTION_FAILED to produce errorsAppErrorCODESBROKEN_CONFIG;
setLogger(logger)
Set function which error class and its inheritors will use for logging.
It called by log
method with two arguments: message
and level
.
Example:
var log = { log; } MyError = Terror; MyError; console;
createError(code, message | originalError | data)
Creates new Terror
or its inheritor instance.
Arguments:
{Number} [code]
– error code fromCODES
hash,Terror.CODES.UNKNOWN_ERROR
used as default value;{String|Error|Terror|Object} [message|originalError|data]
** if 2nd argument is String then use it as original error message; ** if 2nd argument is Error then use its message and call-stack to format original message; ** if 2nd argument is an Object then replace error message placeholders with provided data.
Example:
var MyError = Terror; // valid createError calls // "UNKNOWN_ERROR Terror: Unknown error"Terror; // same as aboveTerror; // "UNKNOWN_ERROR MyError: Unknown error"MyError; // "IO_ERROR MyError: Broken IO"MyError; // "IO_ERROR MyError: Broken IO (Error: kbd int broken)"MyError; // "FS_ERROR MyError: Broken vfat file-system"MyError; // fails, because code IO_ERROR is not defined for TerrorTerror;
ensureError(originalError, code)
Returns originalError
if it's an instance of the owning class.
Otherwise wrap originalError
into new owning class instance using code
or UNKNOWN_ERROR
code if second argument is absent.
Example:
var MyError = Terror arr = msg: "hello" msg: "" {} ; arr
isTerror(error)
Checks whether the error
is an instance of Terror class.
Example:
var err = 'average error here';Terror === false; var terr = Terror;Terror === true;
is(code, error)
Checks whether the error
is an instance of the context class and an error code equals the passed one.
Example:
var MyError = Terror; MyError; // => trueMyError; // => falseMyError; // => false
Methods of prototype
log(level)
Log error with specified level
. If method called twice or more for the same instance, logger will be called by first log
call only.
Arguments:
{*} [level=constructor.DEFAULT_LOG_LEVEL]
– any type accepted by logger.
Example:
var Terror = terr; TerrorDEFAULT_LOG_LEVEL = 'INFO'; try console; catch err terr = Terror; terr ;
terr
will be logged once with 'INFO' error level.
bind(data)
Fill an error message with values from data
object properties.
Arguments:
{Object} data
– hash, where key is a placeholder name to be replaced, value – a data to replace with.
Example:
var IOError = Terror; IOError ;// orIOError ;
Internal, but useful prototype methods
Following methods used by built-in logger routine, but may be useful for any simple text logger.
logMultilineError(message, level, logger)
Formats error message and calls logger(formattedMessage)
.
Formatting is optimized for output call-stacks to the text files and future grep through.
Format:
LEVEL CODE CLASS: MESSAGE
>>>>> CALL STACK ROW 1
…
>>>>> CALL STACK ROW N
Example:
ERROR UNKNOWN_ERROR Terror: Unknown error (TypeError: Object #<Console> has no method 'lag')
>>>>> at repl:2:9
>>>>> at REPLServer.self.eval (repl.js:110:21)
>>>>> at repl.js:249:20
>>>>> at REPLServer.self.eval (repl.js:122:7)
>>>>> at Interface.<anonymous> (repl.js:239:12)
>>>>> at Interface.EventEmitter.emit (events.js:95:17)
>>>>> at Interface._onLine (readline.js:202:10)
>>>>> at Interface._line (readline.js:531:8)
>>>>> at Interface._ttyWrite (readline.js:754:14)
>>>>> at ReadStream.onkeypress (readline.js:99:10)