Extends CODES and associated MESSAGES hashes using codes declaration. Method throws an error if codes uniqueness violated by extension.
Example:
var AppError = Terror .create('AppError') .extendCodes({ BROKEN_CONFIG: 'Looks like configuration file is broken.', DB_CONNECTION_FAILED: 'Can not connect to database %db_host%' });// now you can use AppError.CODES.BROKEN_CONFIG & AppError.CODES.DB_CONNECTION_FAILED to produce errorsnew AppError(AppError.CODES.BROKEN_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 = [], logger = function(message, level) { log.push([ new Date(), level, message ].join()); }, MyError = Terror.create('MyError').setLogger(logger);MyError.createError().log();console.log(log.join('\n'));
createError(code, message | originalError | data)
Creates new Terror or its inheritor instance.
Arguments:
{Number} [code] – error code from CODES 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.create('MyError', { IO_ERROR: 'Broken IO', FS_ERROR: 'Broken %fsName% file-system' });// valid createError calls// "UNKNOWN_ERROR Terror: Unknown error"Terror.createError();// same as aboveTerror.createError(Terror.CODES.UNKNOWN_ERROR);// "UNKNOWN_ERROR MyError: Unknown error"MyError.createError();// "IO_ERROR MyError: Broken IO"MyError.createError(MyError.CODES.IO_ERROR);// "IO_ERROR MyError: Broken IO (Error: kbd int broken)"MyError.createError(MyError.CODES.IO_ERROR, new Error('kbd int broken'));// "FS_ERROR MyError: Broken vfat file-system"MyError.createError(MyError.CODES.FS_ERROR, { fsName: 'vfat' });// fails, because code IO_ERROR is not defined for TerrorTerror.createError(MyError.CODES.IO_ERROR);
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.
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)