@imqueue/rpc
Version:
RPC-like client-service implementation over messaging queue
42 lines • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.logged = logged;
// noinspection JSUnusedGlobalSymbols
/**
* Logger decorator factory for class methods. Will try, catch and log errors
* during method calls. If logger is bypassed, will use given logger, otherwise
* will try to use logger defined on class dynamically or statically or will
* fallback to console.
*
* @param {ILogger | LoggedDecoratorOptions} [options] - custom logger or
* logged decorator
* options
* @return {Function} - decorator function
*/
function logged(options) {
return (target, methodName, descriptor) => {
const original = descriptor.value;
const logger = options && options.logger
? options.logger
: options && options.error ? options :
(this === null || this === void 0 ? void 0 : this.logger) || (target === null || target === void 0 ? void 0 : target.logger) || console;
const level = (options &&
options.level)
? options.level
: 'error';
const doThrow = !options ||
!options.doNotThrow;
descriptor.value = async function (...args) {
try {
return original && await original.apply(this || target, args);
}
catch (err) {
logger[level](err);
if (doThrow) {
throw err;
}
}
};
};
}
//# sourceMappingURL=logged.js.map