@exromany/lido-csm-sdk
Version:
[](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [](h
55 lines • 2.36 kB
JavaScript
import { callConsoleMessage } from './utils.js';
import { SDKError } from '@lidofinance/lido-ethereum-sdk';
export const ErrorHandler = function (headMessage = 'Error:') {
return function ErrorHandlerDecorator(target, context) {
const methodName = String(context.name);
if (context.kind === 'getter') {
const replacementGetter = function () {
try {
const result = target.call(this);
if (result instanceof Promise) {
return result.catch((error) => {
callConsoleMessage.call(this, headMessage, `Error in getter '${methodName}'.`, 'Error:');
const txError = SDKError.from(error);
throw txError;
});
}
else {
return result;
}
}
catch (error) {
callConsoleMessage.call(this, headMessage, `Error in getter '${methodName}'.`, 'Error:');
const txError = SDKError.from(error);
throw txError;
}
};
return replacementGetter;
}
const replacementMethod = function (...args) {
const callback = args[0]?.callback;
try {
const result = target.call(this, ...args);
if (result instanceof Promise) {
return result.catch((error) => {
callConsoleMessage.call(this, headMessage, `Error in method '${methodName}'.`, 'Error:');
const txError = SDKError.from(error);
callback?.({ stage: 'error', payload: txError });
throw txError;
});
}
else {
return result;
}
}
catch (error) {
callConsoleMessage.call(this, headMessage, `Error in method '${methodName}'.`, 'Error:');
const txError = SDKError.from(error);
callback?.({ stage: 'error', payload: txError });
throw txError;
}
};
return replacementMethod;
};
};
//# sourceMappingURL=error-handler.js.map