@exadel/esl
Version:
Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components
28 lines (27 loc) • 969 B
JavaScript
import { resolveProperty } from '../misc/functions';
/** Implementation */
export function safe(fallback) {
return function (target, propertyKey, descriptor) {
const originalMethod = descriptor.value || descriptor.get;
if (typeof originalMethod !== 'function')
throw new Error('@safe can only be applied to methods or getters');
const isGetter = !!descriptor.get && !descriptor.value;
const wrappedMethod = function (...args) {
try {
return originalMethod.apply(this, args);
}
catch (error) {
if (typeof this.$$error === 'function')
this.$$error(error, propertyKey);
return resolveProperty(fallback, this);
}
};
if (isGetter) {
descriptor.get = wrappedMethod;
}
else {
descriptor.value = wrappedMethod;
}
return descriptor;
};
}