Skip to content

Commit

Permalink
chore: improve custom fetch message when response not ok
Browse files Browse the repository at this point in the history
  • Loading branch information
dreyacosta committed Jun 2, 2024
1 parent c8ca446 commit 75076df
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/shared/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export async function fetchJson(url: string, options: FetchOptions): Promise<Res
const response = await fetchWithJsonHeaders(url, options)

if (!response.ok) {
throw new Error(`Error: ${url} - ${response.status} - ${response.statusText}`)
const message = await parseNotOkResponse(response)
throw new Error(`${url} - ${response.status} - ${message}`)
}

return response
Expand All @@ -26,6 +27,15 @@ async function fetchWithJsonHeaders(url: string, options: RequestInit): Promise<
})
}

async function parseNotOkResponse(response: Response): Promise<string> {
try {
const json = await response.json()
return JSON.stringify(json)
} catch (error) {
return response.statusText
}
}

async function handleFetchError(url: string, options: FetchOptions, error: any): Promise<Response> {
const MAX_RETRIES = 3
const retryCount = options.retryCount ?? 0
Expand Down

0 comments on commit 75076df

Please sign in to comment.