From f7fff24acfde76029051fe26a88f993518a95735 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 21 Dec 2021 22:38:21 +0100 Subject: [PATCH] fix: only retry on known response codes (resolves #31) --- src/fetch.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/fetch.ts b/src/fetch.ts index da0308d..707b7de 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -30,12 +30,26 @@ export interface $Fetch { raw(request: FetchRequest, opts?: FetchOptions): Promise>> } +// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status +const retryStatusCodes = new Set([ + 408, // Request Timeout + 409, // Conflict + 425, // Too Early + 429, // Too Many Requests + 500, // Internal Server Error + 502, // Bad Gateway + 503, // Service Unavailable + 504 // Gateway Timeout +]) + export function createFetch ({ fetch, Headers }: CreateFetchOptions): $Fetch { function onError (request: FetchRequest, opts: FetchOptions, error?: Error, response?: FetchResponse): Promise> { // Retry if (opts.retry !== false) { const retries = typeof opts.retry === 'number' ? opts.retry : (isPayloadMethod(opts.method) ? 0 : 1) - if (retries > 0) { + + const responseCode = (response && response.status) || 500 + if (retries > 0 && retryStatusCodes.has(responseCode)) { return $fetchRaw(request, { ...opts, retry: retries - 1