UNPKG

belote

Version:

A TypeScript library for playing Belote card game with bot support.

41 lines (40 loc) 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withErrorHandling = withErrorHandling; exports.withErrorHandlingClass = withErrorHandlingClass; 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; } 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; }