mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-04-21 23:24:49 +00:00
- unit testing seems a bit more complex because of the dependencies, this has to be started before solving above point because it will give hints to better loose coupling
40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
/*
|
|
* Logging
|
|
* =======
|
|
* Functions to handle logging and errors.
|
|
*/
|
|
|
|
// @function print(module, msg)
|
|
|
|
/*
|
|
* Prints a log to stdout.
|
|
* @param {String} module
|
|
* @param {String} msg
|
|
*/
|
|
exports.print = function(module, msg) {
|
|
console.log((new Date()).toISOString() + ' | ' + module + ' | ' + msg);
|
|
};
|
|
|
|
// @method error(module, msg)
|
|
|
|
/*
|
|
* Prints a log to stderr.
|
|
* @param {String} module
|
|
* @param {Error} err
|
|
*/
|
|
exports.error = function(module, err) {
|
|
var ts = (new Date()).toISOString() + ' | ', ai = '';
|
|
if(typeof err === 'string') {
|
|
var e = new Error();
|
|
if(module) console.error(ts + module + ' | ERROR AND BAD HANDLING: ' + err + '\n' + e.stack);
|
|
else console.error(ts + '!N/A! | ERROR, BAD HANDLING AND NO MODULE NAME: ' + err + '\n' + e.stack);
|
|
} else if(err) {
|
|
if(err.addInfo) ai = ' (' + err.addInfo + ')';
|
|
if(!err.message) err.message = 'UNKNOWN REASON!\n' + err.stack;
|
|
if(module) console.error(ts + module + ' | ERROR'+ai+': ' + err.message + '\n' + err.stack);
|
|
else console.error(ts + '!N/A! | ERROR AND NO MODULE NAME'+ai+': ' + err.message + '\n' + err.stack);
|
|
} else {
|
|
var e = new Error('Unexpected error');
|
|
console.error(e.message + ': \n' + e.stack);
|
|
}
|
|
};
|