fix: route pre-agent PostHog API 401s through AuthErrorScreen#477
Draft
posthog[bot] wants to merge 1 commit into
Draft
fix: route pre-agent PostHog API 401s through AuthErrorScreen#477posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
A 401 from `fetchProjectData` (raised before the Claude Code agent boots) propagated unhandled — CI mode rendered a generic "Something went wrong" and the TUI silently exited unless DEBUG was set, bypassing the AuthErrorScreen flow added in #432 for SDK-side 401s. Catch `ApiError` with `statusCode === 401` around both `getOrAskForProjectData` call sites and route through the same `showAuthError` + `wizardAbort` flow used by the SDK-side handler in `agent-interface.ts`. Also surface unexpected errors from the TUI catch in `bin.ts` instead of swallowing them when DEBUG is off. Generated-By: PostHog Code Task-Id: d476f6aa-7a61-4799-8821-37d442cde0fe
🧙 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
A 401 from PostHog's
/api/projects/...(raised before the Claude Code agent starts) propagates throughgetOrAskForProjectData→runProgram→bin.tsand aborts opaquely:Something went wrong: Authentication failed while trying to fetch project data(bin.ts:862-879).DEBUGis set (bin.ts:739-743) — the process exits cleanly with no message.The AuthErrorScreen flow added in #432 only catches the Claude Code SDK's 401 text inside
agent-interface.ts:1156-1181, so PostHog-API 401s raised earlier in the pipeline bypass it entirely.Surfaced by error tracking issue
019e619a-ebf7-7780-ab9c-68de2f59a4f9(16 occurrences, single developer's workbench — zero real-user impact, but the gap is real for anyone hitting an expired or wrong-region personal API key).Changes
handleProjectDataAuthErrorhelper insrc/utils/setup-utils.tsthat mirrors the SDK-side 401 handler: routes throughgetUI().showAuthError({ hasSettingsConflict: false, logFilePath: getLogFilePath() })thenwizardAbort({ message: 'Authentication failed (401)', error }). Non-401 errors pass through so the generic error path renders.runPrograminsrc/lib/agent/agent-runner.tsand theskipAgentbranch inbin.tsnow wrapgetOrAskForProjectDatain a try/catch that calls the helper before re-throwing.runWizardcatch inbin.tsno longer silently exits — it always prints a short error message and exits1, with the full stack still gated behindDEBUG/POSTHOG_WIZARD_DEBUG.hasSettingsConflict: falseis correct here: a PostHog-API 401 is never caused by a Claude Code settings.json conflict, so the screen shows the PAT / scope / region / region-mismatch checklist instead of the "log out of Claude" guidance.Test plan
pnpm build— passespnpm test— 647 pass, 1 pre-existing flake inprovision-cli.test.ts:141reproduces identically on cleanmain(unrelated timing issue, not introduced by this change)pnpm fix— no new lint errorssrc/utils/__tests__/handle-project-data-auth-error.test.tscover the 401 / non-401-status / non-ApiError branches (3/3 pass)Created with PostHog Code