belote
Version:
A TypeScript library for playing Belote card game with bot support.
37 lines (36 loc) • 1.59 kB
JavaScript
export function withErrorHandling(_, propertyName, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args) {
try {
const result = originalMethod.apply(this, args);
if (result && typeof result === 'object' && result !== null && 'catch' in result && typeof result.catch === 'function') {
return result.catch((error) => {
if (this.emit && typeof this.emit === 'function')
this.emit('error', error instanceof Error ? error : new Error(`${String(propertyName)}: ${String(error)}`));
throw error;
});
}
return result;
}
catch (error) {
if (this.emit && typeof this.emit === 'function')
this.emit('error', error instanceof Error ? error : new Error(`${String(propertyName)}: ${String(error)}`));
throw error;
}
};
return descriptor;
}
export function withErrorHandlingClass(constructor) {
const prototype = constructor.prototype;
const methodNames = Object.getOwnPropertyNames(prototype);
methodNames.forEach((name) => {
if (name === 'constructor')
return;
const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
if (descriptor && typeof descriptor.value === 'function' && !name.startsWith('_')) {
withErrorHandling(prototype, name, descriptor);
Object.defineProperty(prototype, name, descriptor);
}
});
return constructor;
}