@exromany/lido-csm-sdk
Version:
[](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [](h
62 lines • 2.64 kB
JavaScript
import { callConsoleMessage } from './utils.js';
import { SDKError } from '@lidofinance/lido-ethereum-sdk';
import { TransactionCallbackStage, } from '../../tx-sdk/types.js';
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);
void callback?.({
stage: TransactionCallbackStage.ERROR,
payload: { error: txError },
});
throw txError;
});
}
else {
return result;
}
}
catch (error) {
callConsoleMessage.call(this, headMessage, `Error in method '${methodName}'.`, 'Error:');
const txError = SDKError.from(error);
void callback?.({
stage: TransactionCallbackStage.ERROR,
payload: { error: txError },
});
throw txError;
}
};
return replacementMethod;
};
};
//# sourceMappingURL=error-handler.js.map