Skip to content

fix(api): retry transient user/project fetch failures in CI auth path#928

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/retry-transient-user-fetch
Draft

fix(api): retry transient user/project fetch failures in CI auth path#928
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/retry-transient-user-fetch

Conversation

@posthog

@posthog posthog Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Problem

The wizard's CI / non-interactive auth path crashed when the first /api/users/@me/ call failed transiently. In CI mode without a projectId, fetchProjectDataWithApiKey has to call fetchUserData first to resolve the team/project id, and that call was not guarded by any retry or best-effort handling — so a brief upstream /api/users/@me/ blip (5xx / network / timeout) propagated up through authenticate and scopeInstallDirToProject and killed the whole CI pre-run. handleApiError had no retry and fell back to a generic "Failed to fetch user data" for exactly the transient statuses worth riding out. Observed as a tight burst of failures across many users in a short window — the shape of a transient upstream hiccup, not a permanent bug.

Changes

  • Add retry-with-backoff for transient failures (5xx, and any error with no HTTP response — network / DNS / connection reset / timeout) to the idempotent bootstrap GETs fetchUserData and fetchProjectData. 4xx (auth / scope / not-found) and parse errors are never retried.
  • Add a per-request timeout so a hung /api/users/@me/ connection fails fast into the retry path instead of blocking the run indefinitely.
  • When retries are exhausted, handleApiError now surfaces a clearer, actionable terminal message distinguishing a temporary API outage from a connectivity problem, instead of the generic fallback.

Since the first call genuinely needs the team id, it can't be swallowed like the second (best-effort) fetchUserData — retry plus a better terminal message is the right shape.

Test plan

  • New src/lib/__tests__/api.test.ts covers: transient 5xx / network retries that recover, retry exhaustion surfacing the clearer messages, fast-fail on 4xx, and no-retry on parse errors.
  • pnpm build && pnpm test (1453 tests) and pnpm fix all pass.

Created with PostHog Code from this inbox report.

The CI / non-interactive auth path resolves the team/project id by calling
fetchUserData first (inside fetchProjectDataWithApiKey). That call had no retry,
so a brief upstream /api/users/@me/ 5xx or network blip propagated straight up
through authenticate and killed the whole pre-run.

Add retry-with-backoff for transient (5xx / network / timeout) failures in the
idempotent GETs (fetchUserData, fetchProjectData) and a per-request timeout so a
hung connection fails fast into that retry path. When retries are exhausted,
handleApiError now surfaces a clearer, actionable terminal message
distinguishing a temporary API outage from a connectivity problem, instead of
the generic "Failed to fetch user data". 4xx (auth/scope/not-found) and parse
errors are never retried.

Generated-By: PostHog Code
Task-Id: 4b8c0393-9452-408e-8646-ba240adcadd6
@github-actions

Copy link
Copy Markdown

🧙 Wizard CI

Run 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:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci basic-integration
  • /wizard-ci mcp-analytics
  • /wizard-ci revenue

Test an individual app:

  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
Show more apps
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci mcp-analytics/custom-dispatcher
  • /wizard-ci mcp-analytics/typescript-sdk
  • /wizard-ci revenue/stripe

Results will be posted here when complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants