(url: string, options: RequestInit, retries: number = 5)
| 36 | * @throws Error when retries are exhausted or server error occurs. |
| 37 | */ |
| 38 | export async function fetchRetry(url: string, options: RequestInit, retries: number = 5) { |
| 39 | try { |
| 40 | const response = await fetch(url, options); |
| 41 | if (response.status >= 500) { |
| 42 | const body = await response.text(); |
| 43 | throw new Error(`Server error code ${response.status}\n${body}`); |
| 44 | } |
| 45 | return response; |
| 46 | } catch (err) { |
| 47 | if (retries <= 0) { |
| 48 | throw err; |
| 49 | } |
| 50 | return fetchRetry(url, options, retries - 1); |
| 51 | } |
| 52 | } |
no outgoing calls
no test coverage detected