fix(init): recover gracefully when org listing returns 403#963
Merged
Conversation
When the CLI can't resolve an org from the codebase (no DSN, no
SENTRY_ORG env var), it calls listOrganizations() to let the user
pick one. If the auth token lacks org:read scope, that call 403s and
the exception propagates to withPreflightHandling(), which logs only
error.message ("Failed to list organizations: 403 Forbidden") and
kills the wizard — with no hint on how to proceed.
Fix: catch the ApiError(403) in resolveOrgSlug() and return a
structured { ok: false } result instead of throwing. The message
surfaces error.detail (already enriched by enrich403Detail() with
token-scope guidance) plus two actionable recovery suggestions:
sentry init <org-slug>/
SENTRY_ORG=<org-slug> sentry init
Non-403 errors are re-thrown unchanged. Fixes sentry issue #7466435851.
Contributor
|
Contributor
Codecov Results 📊✅ 6959 passed | Total: 6959 | Pass Rate: 100% | Execution Time: 0ms 📊 Comparison with Base Branch
✨ No test changes detected All tests are passing successfully. ❌ Patch coverage is 27.78%. Project has 14165 uncovered lines. Files with missing lines (1)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 76.99% 77.00% +0.01%
==========================================
Files 320 320 —
Lines 61577 61574 -3
Branches 0 0 —
==========================================
+ Hits 47406 47409 +3
- Misses 14171 14165 -6
- Partials 0 0 —Generated by Codecov Action |
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.
When a user runs `sentry init` without specifying an org, the CLI calls `listOrganizations()` to show a picker. If the auth token lacks `org:read` scope — common with limited `SENTRY_AUTH_TOKEN` env tokens — that call 403s. The exception had no handler in `resolveOrgSlug()`, so it propagated to `withPreflightHandling()` which logged only `error.message` and killed the wizard entirely, with no indication of how to proceed.
Before:
After (env-var token path shown; OAuth path substitutes "Re-authenticate with: sentry auth login"):
The fix catches `ApiError(403)` in `resolveOrgSlug()` and returns a structured `{ ok: false }` error instead of throwing. The message surfaces `error.detail` (already enriched by `enrich403Detail()` with token-scope guidance) plus two actionable recovery hints. Non-403 errors are re-thrown unchanged. The `{ ok: false }` return flows through the existing `ensureOrg()` → `withPreflightHandling()` chain with no other code changes needed.
Ref: sentry issue 7466435851
Test plan