Skip to content

fix(init): enrich 401 Unauthorized errors with actionable guidance#971

Merged
betegon merged 1 commit into
mainfrom
fix/init-401-enrichment
May 18, 2026
Merged

fix(init): enrich 401 Unauthorized errors with actionable guidance#971
betegon merged 1 commit into
mainfrom
fix/init-401-enrichment

Conversation

@betegon
Copy link
Copy Markdown
Member

@betegon betegon commented May 18, 2026

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 loginsentry 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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 18, 2026

PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://cli.sentry.dev/_preview/pr-971/

Built to branch gh-pages at 2026-05-18 10:59 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 18, 2026

Codecov Results 📊

6975 passed | Total: 6975 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +6
Passed Tests 📈 +6
Failed Tests
Skipped Tests

All tests are passing successfully.

✅ Patch coverage is 91.53%. Project has 14086 uncovered lines.
✅ Project coverage is 77.14%. Comparing base (base) to head (head).

Files with missing lines (3)
File Patch % Lines
src/lib/init/preflight.ts 87.50% ⚠️ 3 Missing
src/commands/issue/list.ts 0.00% ⚠️ 1 Missing
src/lib/api/infrastructure.ts 97.06% ⚠️ 1 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    77.10%    77.14%    +0.04%
==========================================
  Files          320       320         —
  Lines        61568     61609       +41
  Branches         0         0         —
==========================================
+ Hits         47465     47523       +58
- Misses       14103     14086       -17
- Partials         0         0         —

Generated by Codecov Action

@betegon betegon force-pushed the fix/init-401-enrichment branch 3 times, most recently from cb8f6f8 to 7007e81 Compare May 18, 2026 10:30
@betegon betegon marked this pull request as ready for review May 18, 2026 10:31
@betegon betegon force-pushed the fix/init-401-enrichment branch from 7007e81 to 70e0c6c Compare May 18, 2026 10:33
Comment thread src/lib/api/infrastructure.ts
@betegon betegon force-pushed the fix/init-401-enrichment branch 2 times, most recently from 0400fb2 to fe033af Compare May 18, 2026 10:49
Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit fe033af. Configure here.

Comment thread src/lib/api/infrastructure.ts Outdated
403 errors already get actionable hints via enrich403Detail() — telling
users to check token scopes or re-authenticate. 401 errors (invalid or
expired token) were falling through with the raw HTTP status text
("Failed to list organizations: 401 Unauthorized"), which gives no
indication of how to fix the problem.

Adds enrich401Detail() that mirrors the 403 pattern:
- env-var token path: directs to the token settings page
- OAuth path: tells the user to run sentry auth login

Also extends the resolveOrgSlug() catch in preflight.ts (which already
handled 403) to also catch 401 and surface the enriched detail via the
same { ok: false } return path — so the culprit in Sentry points to
ensureOrg rather than the generic withPreflightHandling catch.

Fixes: throwRawApiError's JSON-stringify fallback was not excluding 401
alongside 403, which would have produced noisy {"detail":null} prefixes
in the enriched message for 401 responses with non-string detail fields.

Also corrects a stale "sentry login" string (should be "sentry auth login")
in the empty-orgs fallback path.

Ref: CLI-1SD

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@betegon betegon force-pushed the fix/init-401-enrichment branch from fe033af to be67e5b Compare May 18, 2026 10:58
@betegon betegon merged commit 058b105 into main May 18, 2026
29 checks passed
@betegon betegon deleted the fix/init-401-enrichment branch May 18, 2026 12:17
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.

1 participant