Skip to content

Commit

Permalink
Adds error handling to chainport requests
Browse files Browse the repository at this point in the history
1. Adds a new makeChainportRequest function that wraps the existing makeRequest function and adds error handling for chainport requests
2. This function takes a generic type parameter T that represents the expected response type
3. If the response is not successful, the function throws an error with the response status and message
  • Loading branch information
patnir committed Jun 20, 2024
1 parent c237236 commit f19ada9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
48 changes: 26 additions & 22 deletions ironfish-cli/src/utils/chainport/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import axios from 'axios'
import { getConfig } from './config'
import {
ChainportBridgeTransaction,
ChainportError,
ChainportNetwork,
ChainportTransactionStatus,
ChainportVerifiedToken,
Expand All @@ -19,38 +20,28 @@ export const fetchChainportTransactionStatus = async (
const config = getConfig(networkId)
const url = `${config.endpoint}/api/port?base_tx_hash=${hash}&base_network_id=${config.chainportId}`

const response: {
data: ChainportTransactionStatus
} = await axios.get(url)

return response.data
return await makeChainportRequest<ChainportTransactionStatus>(url)
}

export const fetchChainportNetworkMap = async (
networkId: number,
): Promise<{ [key: string]: ChainportNetwork }> => {
const config = getConfig(networkId)
const response: {
data: {
cp_network_ids: {
[key: string]: ChainportNetwork
}
}
} = await axios.get(`${config.endpoint}/meta`)
const url = `${config.endpoint}/meta`

return response.data.cp_network_ids
return (
await makeChainportRequest<{ cp_network_ids: { [key: string]: ChainportNetwork } }>(url)
).cp_network_ids
}

export const fetchChainportVerifiedTokens = async (
networkId: number,
): Promise<ChainportVerifiedToken[]> => {
const config = getConfig(networkId)
const url = `${config.endpoint}/token/list?network_name=IRONFISH`

const response: {
data: { verified_tokens: ChainportVerifiedToken[] }
} = await axios.get(`${config.endpoint}/token/list?network_name=IRONFISH`)

return response.data.verified_tokens
return (await makeChainportRequest<{ verified_tokens: ChainportVerifiedToken[] }>(url))
.verified_tokens
}

export const fetchChainportBridgeTransaction = async (
Expand All @@ -65,9 +56,22 @@ export const fetchChainportBridgeTransaction = async (
asset.web3_address
}&target_network_id=${network.chainport_network_id.toString()}&target_web3_address=${to}`

const response: {
data: ChainportBridgeTransaction
} = await axios.get(url)
return await makeChainportRequest<ChainportBridgeTransaction>(url)
}

const makeChainportRequest = async <T extends object>(url: string): Promise<T> => {
const response = await axios
.get<T | ChainportError>(url)
.then((response) => {
if ('Error' in response.data) {
throw new Error(response.data.Error)
}

return response.data
})
.catch((error) => {
throw new Error('Chainport error: ' + error)
})

return response.data
return response
}
4 changes: 4 additions & 0 deletions ironfish-cli/src/utils/chainport/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ export type ChainportTransactionStatus =
created_at: string | null
port_in_ack: boolean | null
}

export type ChainportError = {
Error: string
}

0 comments on commit f19ada9

Please sign in to comment.