Skip to content

Error Handling Improvements for HTTP 401/403 in fetchClient.js #288

@rodrigomafra-stabl

Description

@rodrigomafra-stabl

Hi,
The HTTP status code 401 indicates Unauthorized, as defined by the W3C specification or described on MDN. However, in the fetchClient.js file, the corresponding rejected promise returns a message labeled "Forbidden" (with a capital "F") instead. HTTP 403 status code (Forbidden) returns a rejection message using a lowercase "f".

const resp = await fetchRetry(fetch)(finalUrl, {
            retries: 1,
            retryDelay: function (attempt, error, response) {
                return Math.pow(2, attempt) * 1000; // 1000, 2000, 4000
            },
            retryOn: [503, 502, 504, 500],
            headers,
            method,
            body: init.body ? init.body : undefined,
            signal: AbortSignal.timeout(timeout),
            cache: 'no-store',
        });
        if (resp.status === 204) {
            return resp;
        }
        else if (resp.status == 429) {
            const limit = parseLimitFromResponse(resp);
            return Promise.reject(new AxiomTooManyRequestsError(limit));
        }
		// error: Forbidden
        else if (resp.status === 401) {
            return Promise.reject(new Error('Forbidden'));
        }
		// error: forbidden
        else if (resp.status >= 400) {
            const payload = (await resp.json());
            return Promise.reject(new Error(payload.message));
        }
        return (await resp.json());

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions