-
Notifications
You must be signed in to change notification settings - Fork 0
OUT-3544: Add retry mechanisms for transient network errors #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d827e33
refactor(OUT-3544): drop dead null guards in IntuitAPI
SandipBajracharya 53dae5f
feat(OUT-3544): timeouts + HttpFetchError + broader retry classifier …
SandipBajracharya 59ab70b
refactor(OUT-3544): adapt hooks/SWR to fetch.helper throw-on-error co…
SandipBajracharya c8307cb
fix(OUT-3544): unwrap IntuitAPI read methods to eliminate withRetry n…
SandipBajracharya dd108c2
chore(OUT-3544): tidy withRetry + fetch.helper test names and remove …
SandipBajracharya 8e21c56
chore(OUT-3544): trim verbose inline comments
SandipBajracharya 5f3db26
fix(OUT-3544): drop AbortError from retryable set — only TimeoutError
SandipBajracharya 77df1c3
fix(OUT-3544): strict retry classifier for non-idempotent write paths
SandipBajracharya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,106 @@ | ||
| import { externalFetchTimeoutMs } from '@/config' | ||
| import { HttpFetchError } from '@/utils/error' | ||
|
|
||
| export type FetcherOptions = { | ||
| // number = use this timeout; null = disable timeout entirely (no AbortSignal | ||
| // attached); undefined = fall back to externalFetchTimeoutMs. | ||
| timeoutMs?: number | null | ||
| } | ||
|
|
||
| // Pulls a human-readable detail out of common upstream error shapes so the | ||
| // thrown HttpFetchError surfaces *why* the call failed in `error.message` | ||
| // (which is what gets written to qb_sync_logs). Without this, the message | ||
| // degrades to a generic "HTTP 400 Bad Request from <url>" and the real | ||
| // reason — e.g. Intuit's "Required param missing" — only lives in | ||
| // `error.body`, which most downstream consumers don't inspect. | ||
| const extractUpstreamDetail = (body: unknown): string | undefined => { | ||
| if (!body || typeof body !== 'object') return undefined | ||
| const b = body as Record<string, any> | ||
|
|
||
| // Intuit (QBO): | ||
| // { Fault: { Error: [{ Message, Detail, code }], type: '...' } } | ||
| // Detail is usually the diagnostic; Message is a short label. | ||
| const intuitErr = b?.Fault?.Error?.[0] | ||
| if (intuitErr && typeof intuitErr === 'object') { | ||
| const parts = [intuitErr.Message, intuitErr.Detail] | ||
| .filter((p) => typeof p === 'string' && p.length > 0) | ||
| .join(' — ') | ||
| if (parts) return parts | ||
| } | ||
|
|
||
| // Copilot and many other JSON APIs: { message: '...' } or { error: '...' } | ||
| if (typeof b.message === 'string' && b.message) return b.message | ||
| if (typeof b.error === 'string' && b.error) return b.error | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| // Exported so other clients with their own fetch wrappers (e.g. CopilotAPI's | ||
| // manualFetch) can produce consistently-shaped HttpFetchError instances | ||
| // without duplicating the JSON/text body-parsing logic. | ||
| export const buildHttpFetchError = async ( | ||
| response: Response, | ||
| url: string, | ||
| ): Promise<HttpFetchError> => { | ||
| const rawBody = await response.text().catch(() => '') | ||
| let body: unknown = rawBody | ||
| if (rawBody) { | ||
| try { | ||
| body = JSON.parse(rawBody) | ||
| } catch { | ||
| // not JSON; keep raw text | ||
| } | ||
| } | ||
|
|
||
| // Prefer the upstream detail as the message when present — it's what | ||
| // qb_sync_logs.message and any user-facing surface actually want to show. | ||
| // The HTTP status lives on error.status / qb_sync_logs.code and the URL | ||
| // lives on error.url, so we don't lose them by omitting them here. | ||
| const detail = extractUpstreamDetail(body) | ||
| const message = | ||
| detail ?? `HTTP ${response.status} ${response.statusText || ''}`.trim() | ||
|
|
||
| return new HttpFetchError({ | ||
| status: response.status, | ||
| statusText: response.statusText, | ||
| url, | ||
| body, | ||
| message, | ||
| }) | ||
| } | ||
|
|
||
| const resolveSignal = (opts: FetcherOptions): AbortSignal | undefined => { | ||
| if (opts.timeoutMs === null) return undefined | ||
| return AbortSignal.timeout(opts.timeoutMs ?? externalFetchTimeoutMs) | ||
| } | ||
|
|
||
| export const postFetcher = async ( | ||
| url: string, | ||
| headers: Record<string, string>, | ||
| body: Record<string, any>, | ||
| opts: FetcherOptions = {}, | ||
| ) => { | ||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| headers, | ||
| body: JSON.stringify(body), // body data type must match "Content-Type" header | ||
| body: JSON.stringify(body), | ||
| signal: resolveSignal(opts), | ||
| }) | ||
|
|
||
| if (!response.ok) throw await buildHttpFetchError(response, url) | ||
| return response.json() | ||
| } | ||
|
|
||
| export const getFetcher = async ( | ||
| url: string, | ||
| headers: Record<string, string>, | ||
| opts: FetcherOptions = {}, | ||
| ) => { | ||
| const response = await fetch(url, { headers }) | ||
| const response = await fetch(url, { | ||
| headers, | ||
| signal: resolveSignal(opts), | ||
| }) | ||
|
|
||
| if (!response.ok) throw await buildHttpFetchError(response, url) | ||
| return response.json() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.