You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(init): enrich 401 Unauthorized errors with actionable guidance (#971)
When `sentry init` runs without an org argument it calls
`listOrganizations()` to show a picker. If the token is invalid or
expired that call returns 401. Before this change users saw:
```
✗ Failed to list organizations: 401 Unauthorized
✗ Setup failed.
```
No hint about what to do. Compare that to 403, which already had
`enrich403Detail()` pointing at the token settings page or suggesting
re-auth. 401 was just falling through with the raw HTTP status.
**After (env-var token):**
```
✗ Could not list organizations (401 Unauthorized).
Your SENTRY_AUTH_TOKEN token is invalid or has expired.
Regenerate it at: https://sentry.io/settings/auth-tokens/
✗ Setup failed.
```
**After (OAuth):**
```
✗ Could not list organizations (401 Unauthorized).
Not authenticated or your session has expired.
Re-authenticate with: sentry auth login
✗ Setup failed.
```
## What changed
- `enrich401Detail()` in `infrastructure.ts` — mirrors
`enrich403Detail()`, applied in both `throwApiError()` and
`throwRawApiError()`
- `resolveOrgSlug()` in `preflight.ts` — extended the existing 403 catch
to also handle 401, returning `{ ok: false }` so the error surfaces
through `ensureOrg` with the full enriched message (same flow as 403,
same Sentry culprit attribution)
- Fixed `throwRawApiError`'s JSON-stringify fallback to exclude 401
alongside 403 — otherwise `{"detail":null}` responses would produce
noisy prefixes in the enriched message
- Fixed a stale `sentry login` → `sentry auth login` in the empty-orgs
fallback
## Test plan
- Set `SENTRY_AUTH_TOKEN` to a garbage value, run `sentry init` with no
org arg — should print the token hint + settings URL
- `sentry auth logout`, run `sentry init` with no org arg — should print
the session-expired + re-auth hint
- `bun test test/lib/api/infrastructure.test.ts` — 17 tests pass
Ref: CLI-1SD
Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
0 commit comments