fix(auth): enrich API error cause + retry the user-data fetch#908
Draft
posthog[bot] wants to merge 1 commit into
Draft
fix(auth): enrich API error cause + retry the user-data fetch#908posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
The wizard's login step surfaced a generic `ApiError: Failed to fetch user data` for every non-4xx failure, so network errors, timeouts, and 5xx all collapsed into one opaque message with no status or cause on the captured `$exception`. `handleApiError` now classifies failures with an `ApiErrorKind` discriminant (network / timeout / server / auth / forbidden / not_found / client / parse / unknown), keeps the underlying error as `cause`, and puts the transport-level code or status into the message. `ApiError` exposes `isTransient` and a `toProperties()` bag so the endpoint, status, kind, and cause actually land on the analytics exception. `fetchUserData` now retries transient failures (network / timeout / 5xx) with exponential backoff before surfacing a hard error on the OAuth login path; auth/permission/parse failures still fail fast. The exception is captured exactly once, after retries are exhausted. Generated-By: PostHog Code Task-Id: c6122001-868a-43f1-9059-050712a4b89e
🧙 Wizard CIRun the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands: Test all apps:
Test all apps in a directory:
Test an individual app:
Show more apps
Results will be posted here when complete. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The wizard's login step threw a generic
ApiError: Failed to fetch user datafor any non-4xx failure hittingGET /api/users/@me/during auth. Every network error, timeout, and 5xx collapsed into one opaque message — the captured$exceptioncarried no status code or underlying cause, andendpointwas landing asNone. That left a blind spot: transient infra blips were indistinguishable from a genuine auth regression. Low volume and intermittent, but it mostly hurts the team building the wizard, since the OAuth login path lets the throw block setup while the CI path swallows it.Changes
handleApiErrornow classifies failures with anApiErrorKinddiscriminant (network/timeout/server/auth/forbidden/not_found/client/parse/unknown), keeps the originating error ascause, and folds the transport-level code (ECONNABORTED,ETIMEDOUT,ENOTFOUND, …) or HTTP status into the message instead of the bareFailed to …string.ApiErrorgainsisTransientand atoProperties()bag;fetchUserData/fetchProjectDataspread that bag into the captured exception soendpoint,statusCode,error_kind, andcauseactually land on the$exception.fetchUserDataretries transient failures with exponential backoff before surfacing a hard failure on the OAuth path; auth/permission/parse failures still fail fast. The exception is captured exactly once, after retries are exhausted.Test plan
src/lib/__tests__/api.test.tscovers eachhandleApiErrorclassification and the retry loop (first-try success, transient-then-success with backoff, non-transient fail-fast, and retry exhaustion with enriched capture props).pnpm build && pnpm test— 1339 tests pass.pnpm lint— 0 errors;src/lib/api.tsis warning-free.LLM context
Authored by PostHog Code (Claude) from an inbox error-tracking report on the generic non-4xx fallback in
handleApiError.Created with PostHog Code from an inbox report.