Skip to content

Commit

Permalink
fix: extrapolate errors from non-2xx responses
Browse files Browse the repository at this point in the history
fixes #4
  • Loading branch information
Sleavely committed Oct 28, 2023
1 parent b0a2efa commit 51174d4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/utils/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,24 @@ export const phin = async <T = unknown>(
Authorization: authHeader,
}

return await basePhin<T>({
const res = await basePhin<T>({
parse: 'json',
timeout: 2000,
...opts,
url,
headers,
})

if (res.statusCode && res.statusCode > 299) {
const { error, message } = res.body as { error?: string, message?: string }
if (error) {
throw new Error(`HTTP ${res.statusCode}: ${error}`)
}
if (message) {
throw new Error(`HTTP ${res.statusCode}: ${message}`)
}
throw new Error(`HTTP ${res.statusCode}: ${JSON.stringify(res.body)}`)
}

return res
}

0 comments on commit 51174d4

Please sign in to comment.