diff --git a/src/lib/api_consistency.ts b/src/lib/api_consistency.ts index bcbb67917..f11a3c399 100644 --- a/src/lib/api_consistency.ts +++ b/src/lib/api_consistency.ts @@ -5,9 +5,13 @@ const d = debug("mw:api-consistency"); export function configureConsistencyHandling(axios: AxiosInstance) { let lastEventId: string | undefined = undefined; + let mutatedPaths: string[] | undefined = undefined; axios.interceptors.request.use((config) => { - if (lastEventId !== undefined) { + if ( + lastEventId !== undefined && + mutatedPaths?.some((path) => config.url?.startsWith(path)) + ) { d("setting if-event-reached to %o", lastEventId); config.headers["if-event-reached"] = lastEventId; } @@ -22,11 +26,18 @@ export function configureConsistencyHandling(axios: AxiosInstance) { const headers = response.headers as AxiosHeaders; if (headers.has("etag") && isMutatingRequest) { + lastEventId = headers.get("etag") as string; + + const mutatedPath = response.config?.url; + if (mutatedPath !== undefined) { + mutatedPaths = [mutatedPath]; + } + d( - "setting last event id to %o after mutating request", - headers.get("etag"), + "setting last event id to %o after mutating request for path %o", + headers["etag"], + mutatedPath, ); - lastEventId = headers.get("etag") as string; } return response; }); diff --git a/src/lib/api_retry.ts b/src/lib/api_retry.ts index bddb95ded..8fe090d6f 100644 --- a/src/lib/api_retry.ts +++ b/src/lib/api_retry.ts @@ -24,7 +24,10 @@ export function configureAxiosRetry(axios: AxiosInstance) { axiosRetry(axios, { retries: 10, retryDelay: axiosRetry.exponentialDelay, - onRetry(count, error) { + onRetry(count, error, config) { + if (error.response?.status === 412 && config.headers !== undefined) { + delete config.headers["if-event-reached"]; + } d("retrying request after %d attempts; error: %o", count, error.message); }, retryCondition(error) { @@ -37,8 +40,13 @@ export function configureAxiosRetry(axios: AxiosInstance) { } const isSafeRequest = error.config?.method?.toLowerCase() === "get"; + const isPreconditionFailed = error.response?.status === 412; const isAccessDenied = error.response?.status === 403; + if (isPreconditionFailed) { + return true; + } + return isSafeRequest && isAccessDenied && shouldRetryAccessDenied; }, });