Skip to content

Commit

Permalink
fix: update getJson to handle errors properly (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolcalmi authored Jan 8, 2023
1 parent fa1c30f commit 93dfe0f
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios'
import axios, { AxiosError } from 'axios'
import isURL from 'is-url'
import { bech32 } from 'bech32'
import * as bolt11 from 'bolt11'
Expand Down Expand Up @@ -116,11 +116,22 @@ export const getJson = async ({
url: string
params?: { [key: string]: string | number }
}): Promise<{ [key: string]: string | number }> => {
return axios.get(url, { params }).then((response) => {
if (response.data.status === 'ERROR')
throw new Error(response.data.reason + '')
return response.data
})
try {
const { status, statusText, data } = await axios.get(url, { params })
if (status >= 400 || data.status === 'ERROR')
throw new Error(
data.reason || data.detail || statusText || 'Invalid request'
)
return data
} catch (error) {
if (error instanceof AxiosError && error.response) {
const { data, statusText } = error.response
throw new Error(
data.reason || data.detail || statusText || 'Invalid request'
)
}
throw error
}
}

export const decodeInvoice = (
Expand Down

0 comments on commit 93dfe0f

Please sign in to comment.