| 36 | const shownErrors = new Set<string>(); |
| 37 | |
| 38 | export const createAgentServerQueryClient = () => { |
| 39 | const reportedQueryErrors = new WeakMap<Query<unknown, unknown>, string>(); |
| 40 | const client = new QueryClient({ |
| 41 | defaultOptions: { |
| 42 | queries: { |
| 43 | // Jittered exponential backoff, with extra backoff on a confirmed |
| 44 | // 429, so a burst of queries that all got rate-limited together |
| 45 | // (e.g. every open browser tab refetching on window focus at once) |
| 46 | // don't retry in lockstep and immediately re-trip the limiter. |
| 47 | // Queries that set their own `retryDelay` are unaffected. |
| 48 | retryDelay: getQueryRetryDelay, |
| 49 | }, |
| 50 | }, |
| 51 | queryCache: new QueryCache({ |
| 52 | onSuccess: (_data, query) => { |
| 53 | reportedQueryErrors.delete(query); |
| 54 | const backendId = |
| 55 | query.meta?.backendId ?? query.options.meta?.backendId; |
| 56 | if (typeof backendId === "string") { |
| 57 | recordBackendSuccess(backendId); |
| 58 | } |
| 59 | }, |
| 60 | onError: (error, query) => { |
| 61 | const isAuthQuery = |
| 62 | query.queryKey[0] === "user" && query.queryKey[1] === "authenticated"; |
| 63 | if (!isAuthQuery) { |
| 64 | handle401Error(error, client); |
| 65 | } |
| 66 | |
| 67 | const disableToast = |
| 68 | query.meta?.disableToast ?? query.options.meta?.disableToast; |
| 69 | |
| 70 | // Keep cached content during a transient refresh failure; the query |
| 71 | // still exposes its error, and the next successful fetch clears it. |
| 72 | if (query.state.data !== undefined && isTransientRequestError(error)) |
| 73 | return; |
| 74 | |
| 75 | if (!disableToast && !isActiveCloudBackendAuthError(error)) { |
| 76 | const errorMessage = retrieveAxiosErrorMessage(error); |
| 77 | if (reportedQueryErrors.get(query) === (errorMessage || "")) return; |
| 78 | reportedQueryErrors.set(query, errorMessage || ""); |
| 79 | |
| 80 | if (!shownErrors.has(errorMessage || "")) { |
| 81 | displayErrorToast(errorMessage || i18n.t(I18nKey.ERROR$GENERIC)); |
| 82 | shownErrors.add(errorMessage || ""); |
| 83 | |
| 84 | setTimeout(() => { |
| 85 | shownErrors.delete(errorMessage || ""); |
| 86 | }, 3000); |
| 87 | } |
| 88 | } |
| 89 | }, |
| 90 | }), |
| 91 | mutationCache: new MutationCache({ |
| 92 | onError: (error, _, __, mutation) => { |
| 93 | handle401Error(error, client); |
| 94 | |
| 95 | const disableToast = |