( response: Response, attempt: number, retryOptions?: RetryOptions )
| 248 | }; |
| 249 | |
| 250 | function shouldRetry( |
| 251 | response: Response, |
| 252 | attempt: number, |
| 253 | retryOptions?: RetryOptions |
| 254 | ): ShouldRetryResult { |
| 255 | function shouldRetryForOptions(): ShouldRetryResult { |
| 256 | const retry = { ...defaultRetryOptions, ...retryOptions }; |
| 257 | |
| 258 | const delay = calculateNextRetryDelay(retry, attempt); |
| 259 | |
| 260 | if (delay) { |
| 261 | return { retry: true, delay }; |
| 262 | } else { |
| 263 | return { retry: false }; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Note this is not a standard header. |
| 268 | const shouldRetryHeader = response.headers.get("x-should-retry"); |
| 269 | |
| 270 | // If the server explicitly says whether or not to retry, obey. |
| 271 | if (shouldRetryHeader === "true") return shouldRetryForOptions(); |
| 272 | if (shouldRetryHeader === "false") return { retry: false }; |
| 273 | |
| 274 | // Retry on request timeouts. |
| 275 | if (response.status === 408) return shouldRetryForOptions(); |
| 276 | |
| 277 | // Retry on lock timeouts. |
| 278 | if (response.status === 409) return shouldRetryForOptions(); |
| 279 | |
| 280 | // Retry on rate limits. |
| 281 | if (response.status === 429) return shouldRetryForOptions(); |
| 282 | |
| 283 | // Retry internal errors. |
| 284 | if (response.status >= 500) return shouldRetryForOptions(); |
| 285 | |
| 286 | return { retry: false }; |
| 287 | } |
| 288 | |
| 289 | function safeJsonParse(text: string): any { |
| 290 | try { |
no test coverage detected
searching dependent graphs…