yandex-music
Version:
Creative and progressive Node.js framework for applications that interact with yandex music
49 lines (48 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.log = exports.Catch = void 0;
const exceptions_1 = require("../exceptions");
const Catch = (type) => {
return (target, propertyKey, descriptor) => {
// Save a reference to the original method
const originalMethod = descriptor.value;
// Rewrite original method with try/catch wrapper
descriptor.value = function (...args) {
try {
const result = originalMethod.apply(this, args);
// Check if method is asynchronous
if (result && result instanceof Promise) {
// Return promise
return result.catch((error) => {
console.debug(`Entering: ${propertyKey}`);
handleError(propertyKey, type, error);
});
}
// Return actual result
return result;
}
catch (error) {
console.debug(`Entering: ${propertyKey}`);
handleError(propertyKey, type, error);
}
};
return descriptor;
};
};
exports.Catch = Catch;
const log = () => (0, exports.Catch)(exceptions_1.YandexMusicError);
exports.log = log;
function handleError(ctx, errorType, error) {
// Check if error is instance of given error type
if (error instanceof errorType) {
// Run handler with error object and class context
console.log(`${error.name}: ${error.message}`);
console.log(`Exiting: ${ctx}`);
}
else {
// Throw error further
// Next decorator in chain can catch it
console.log(error);
console.log(`Exiting: ${ctx}`);
}
}