From 5bbacdc1378d552ca672b125a59f76e898c2384d Mon Sep 17 00:00:00 2001 From: harisato Date: Mon, 5 Sep 2022 11:01:41 +0700 Subject: [PATCH] Handle error on general API --- src/services/impls/general.service.ts | 120 +++++++++++++++----------- 1 file changed, 68 insertions(+), 52 deletions(-) diff --git a/src/services/impls/general.service.ts b/src/services/impls/general.service.ts index 7637347b..f569027a 100644 --- a/src/services/impls/general.service.ts +++ b/src/services/impls/general.service.ts @@ -12,6 +12,7 @@ import { LCDClient } from '@terra-money/terra.js'; import { getEvmosAccount } from 'src/chains/evmos'; import * as axios from 'axios'; import { IGasRepository } from 'src/repositories/igas.repository'; +import { CustomError } from 'src/common/customError'; export class GeneralService extends BaseService implements IGeneralService { private readonly _logger = new Logger(GeneralService.name); @@ -33,44 +34,51 @@ export class GeneralService extends BaseService implements IGeneralService { async getValidators(param: MODULE_REQUEST.GetValidatorsParam) { const { internalChainId } = param; - const chain = await this.chainRepo.findChain(internalChainId); - const result = await axios.default.get( - new URL( - '/cosmos/staking/v1beta1/validators?status=BOND_STATUS_BONDED', - chain.rest, - ).href, - ); - return ResponseDto.response(ErrorMap.SUCCESSFUL, result.data); + try { + const chain = await this.chainRepo.findChain(internalChainId); + const result = await axios.default.get( + new URL( + '/cosmos/staking/v1beta1/validators?status=BOND_STATUS_BONDED', + chain.rest, + ).href, + ); + return ResponseDto.response(ErrorMap.SUCCESSFUL, result.data); + } catch (error) { + return ResponseDto.responseError(GeneralService.name, error); + } } async showNetworkList(): Promise { - const res = new ResponseDto(); - const chains = await this.chainRepo.showNetworkList(); - for (const chain of chains) { - const gas = await this.gasRepo.findByCondition( - { - chainId: chain.chainId, - }, - undefined, - ['typeUrl', 'gasAmount', 'multiplier'], - ); - chain.defaultGas = gas; + try { + const chains = await this.chainRepo.showNetworkList(); + for (const chain of chains) { + const gas = await this.gasRepo.findByCondition( + { + chainId: chain.chainId, + }, + undefined, + ['typeUrl', 'gasAmount', 'multiplier'], + ); + chain.defaultGas = gas; + } + return ResponseDto.response(ErrorMap.SUCCESSFUL, chains); + } catch (error) { + return ResponseDto.responseError(GeneralService.name, error); } - return res.return(ErrorMap.SUCCESSFUL, chains); } async getAccountOnchain( param: MODULE_REQUEST.GetAccountOnchainParam, ): Promise { - const res = new ResponseDto(); try { const safeAddress = { safeAddress: param.safeAddress }; const safe = await this.safeRepo.findByCondition(safeAddress); - if (safe.length === 0) return res.return(ErrorMap.NO_SAFES_FOUND); + if (safe.length === 0) throw new CustomError(ErrorMap.NO_SAFES_FOUND); const condition = { id: param.internalChainId }; const chain = await this.chainRepo.findByCondition(condition); - if (chain.length === 0) return res.return(ErrorMap.CHAIN_ID_NOT_EXIST); + if (chain.length === 0) + throw new CustomError(ErrorMap.CHAIN_ID_NOT_EXIST); let client, accountOnChain; switch (chain[0].chainId) { @@ -98,46 +106,54 @@ export class GeneralService extends BaseService implements IGeneralService { accountOnChain = await client.getAccount(param.safeAddress); break; } - return res.return(ErrorMap.SUCCESSFUL, accountOnChain); + return ResponseDto.response(ErrorMap.SUCCESSFUL, accountOnChain); } catch (error) { - console.log(error); + return ResponseDto.responseError(GeneralService.name, error); } } async getDelegatorRewards(param: MODULE_REQUEST.GetDelegatorRewardsParam) { - const { delegatorAddress, internalChainId } = param; - const chain = await this.chainRepo.findChain(internalChainId); - const result = await axios.default.get( - new URL( - `/cosmos/distribution/v1beta1/delegators/${delegatorAddress}/rewards`, - chain.rest, - ).href, - ); - return ResponseDto.response(ErrorMap.SUCCESSFUL, result.data); + try { + const { delegatorAddress, internalChainId } = param; + const chain = await this.chainRepo.findChain(internalChainId); + const result = await axios.default.get( + new URL( + `/cosmos/distribution/v1beta1/delegators/${delegatorAddress}/rewards`, + chain.rest, + ).href, + ); + return ResponseDto.response(ErrorMap.SUCCESSFUL, result.data); + } catch (error) { + return ResponseDto.responseError(GeneralService.name, error); + } } async getDelegationInformation( param: MODULE_REQUEST.GetDelegationInformationParam, query: MODULE_REQUEST.GetDelegationInformationQuery, ) { - const { delegatorAddress, internalChainId } = param; - const { countTotal, key, limit, offset, reverse } = query; - const chain = await this.chainRepo.findChain(internalChainId); - const result = await axios.default.get( - new URL( - `/cosmos/staking/v1beta1/delegations/${delegatorAddress}`, - chain.rest, - ).href, - { - params: { - 'pagination.key': key, - 'pagination.offset': offset, - 'pagination.limit': limit, - 'pagination.countTotal': countTotal, - 'pagination.reverse': reverse, + try { + const { delegatorAddress, internalChainId } = param; + const { countTotal, key, limit, offset, reverse } = query; + const chain = await this.chainRepo.findChain(internalChainId); + const result = await axios.default.get( + new URL( + `/cosmos/staking/v1beta1/delegations/${delegatorAddress}`, + chain.rest, + ).href, + { + params: { + 'pagination.key': key, + 'pagination.offset': offset, + 'pagination.limit': limit, + 'pagination.countTotal': countTotal, + 'pagination.reverse': reverse, + }, }, - }, - ); - return ResponseDto.response(ErrorMap.SUCCESSFUL, result.data); + ); + return ResponseDto.response(ErrorMap.SUCCESSFUL, result.data); + } catch (error) { + return ResponseDto.responseError(GeneralService.name, error); + } } }