UNPKG

@solarity/hardhat-migrate

Version:
66 lines 2.26 kB
import { MigrateError } from "./MigrateError.js"; const WRAPPED_METHOD = Symbol("CatchClassErrorWrapped"); export function CatchClassError(value, context) { const name = context.name?.toString?.() ?? value.name; const BaseClass = value; return class extends BaseClass { constructor(...args) { super(...args); _wrapAllMethods(this, name); } }; } function _wrapAllMethods(instance, className) { let proto = Object.getPrototypeOf(instance); while (proto && proto !== Object.prototype) { for (const key of Reflect.ownKeys(proto)) { if (key === "constructor") continue; const desc = Object.getOwnPropertyDescriptor(proto, key); if (!desc || typeof desc.value !== "function") continue; if (desc.value[WRAPPED_METHOD]) continue; const wrapped = _wrapMethod(desc.value, `${className}.${String(key)}`); wrapped[WRAPPED_METHOD] = true; Object.defineProperty(proto, key, { ...desc, value: wrapped, }); } proto = Object.getPrototypeOf(proto); } } function _wrapMethod(original, propertyName) { return function ___ErrorCatcher(...args) { try { const result = original.apply(this, args); if (result && typeof result.then === "function") { return result.catch((e) => _handleError(propertyName, e)); } return result; } catch (e) { _handleError(propertyName, e); } }; } export function CatchMethodError(value, context) { const name = `${String(context.name)}`; return function (...args) { try { const result = value.apply(this, args); if (result && typeof result.then === "function") { return result.catch((e) => _handleError(name, e)); } return result; } catch (e) { _handleError(name, e); } }; } function _handleError(propertyName, error) { throw new MigrateError(`${propertyName}(): ${error.message ?? error}`, { cause: error }); } //# sourceMappingURL=error.js.map