Skip to content

feat(api): Add typed Sentry error codes and auth challenges - #131253

Closed
sentry-junior[bot] wants to merge 3 commits into
masterfrom
feat/api-error-codes
Closed

sentry-junior[bot] wants to merge 3 commits into
masterfrom
feat/api-error-codes

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Part of #131203.

The backend's SentryAPIException errors return {"detail": {"code", "message", "extra"}}. Until now, consumers of @sentry/api had to dig that out of error.body themselves.

Error codes

  • When the body has that shape, SentryApiError and its subclasses, including /client's ApiError, now have code, detailMessage and extra.
  • message doesn't change; it stays the error's own message. Errors with any other body don't get the new keys.
  • KnownSentryErrorCode lists the codes in src/sentry/api/exceptions.py and the other SentryAPIException subclasses. SentryErrorCode also accepts any other string.
  • isSentryErrorCode(error, "sudo-required") narrows an error to one code.

Auth challenges

  • getSentryAuthChallenge(error) turns the SSO, sudo, superuser, staff, 2FA, member-disabled and email-verification responses into a typed SentryAuthChallenge. Each carries its code's string fields: loginUrl and organizationSlug for SSO, orgSlug for superuser, username for sudo and email verification, and next for a disabled member.
  • withAuthChallenges(fetch, { onAuthChallenge }) wraps any fetch, for example the one given to createSentryClient, createBrowserFetch or createApiTransport.
    • On a challenge it calls onAuthChallenge(challenge, request).
    • Returning "retry" sends the request once more, after the app has re-authenticated. Returning "fail" keeps the challenge response.
    • The SDK opens no UI and never redirects or reloads.

Without the wrapper, behavior is the same as before, apart from the new fields. The README documents both, including how to map codes to your own copy.

Possible type break: a SentryApiError subclass that declares its own code now needs override under noImplicitOverride, and its code has to be a string.

Project-rename classification is in #131258.

via @BYK.

--

View Session [Sentry]

SentryAPIException bodies, {"detail": {"code", "message", "extra"}},
now set code, detailMessage and extra on SentryApiError and its
subclasses. message stays the error's own, and other bodies leave the
fields unset. KnownSentryErrorCode lists the backend's codes, and
SentryErrorCode allows any other string. isSentryErrorCode narrows by
code.

getSentryAuthChallenge classifies SSO, sudo, superuser, staff, 2FA,
disabled-member and email-verification responses as data. The
withAuthChallenges fetch wrapper calls an onAuthChallenge handler and
replays the request once when it returns retry. The SDK opens no UI
and never redirects, and without the wrapper nothing is retried.

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot requested a review from a team as a code owner November 16, 2026 14:39
@sentry-junior
sentry-junior Bot requested a review from a team as a code owner November 16, 2026 14:39
@sentry-junior
sentry-junior Bot requested a review from BYK November 16, 2026 14:39
@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Nov 16, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Co-Authored-By: BYK <[email protected]>

# Conflicts:
#	static/packages/api/build.mjs
#	static/packages/api/lib/sentry-errors.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

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 2 potential issues.

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 c76cc5b. Configure here.

*/
export type SentryAuthChallenge = ChallengeBase &
(
| { code: "sso-required"; loginUrl?: string; organizationSlug?: string }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't these come from the backend directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ideally, yes. Today they can't be: the OpenAPI spec this package is generated from doesn't describe SentryAPIException bodies at all. It has no list of codes and no extra fields, and none of these codes appear in the published spec. So for now KnownSentryErrorCode and the challenge fields are kept in sync by hand with src/sentry/api/exceptions.py.

To generate them, the backend would first have to publish them, for example as a spec component that lists each SentryAPIException subclass with its code, status and extra keys. That would be a backend PR, landing first since src/ and static/ deploy separately. The SDK would then build the union and SentryAuthChallenge from that component instead of these hand-written types. Want me to take that on as a follow-up, or keep the hand-maintained list for this PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now they do:

The /client core passed no body to createApiError when an error's
detail was not a string, so SentryAPIException responses never set
code on /client errors. Pass the parsed body.

Report the superuser-required orgSlug extra on the challenge, and do
not let a failed cancel of the challenge body stop the replay.
BYK added a commit that referenced this pull request Nov 16, 2026
…nt (#131285)

Part of #131203.

`SentryAPIException` subclasses answer with `{"detail": {"code",
"message", "extra"}}`, but the OpenAPI spec doesn't describe that body.
`@sentry/api` (#131253) keeps a hand-written list of the codes, which
can drift from the backend. This adds a `SentryApiErrorBody` schema
component so clients can generate from the spec instead.

- **Built from the exception classes.** The postprocessing hook walks
every imported `SentryAPIException` subclass with a code. Each one
becomes a `detail` variant with its `code` (a one-value enum), its HTTP
status in `x-sentry-status`, and the string keys its `extra` can carry.
Two classes that share a code with different statuses fail the build.
- **`extra` keys.** `SentryAPIException` gets an `extra_keys` class
attribute, set where the keys are stable: `sso-required` (`loginUrl`,
`organizationSlug`), `sudo-required` and
`primary-email-verification-required` (`username`),
`member-disabled-over-limit` (`next`), `parameter-validation-error`
(`context`), and `superuser-required` (`orgSlug`, which only
organization-scoped checks send). Each class's `extra_keys` must match
the keywords its `__init__` passes to `super().__init__`, so a new key
there has to be declared.
- **Component only.** No operation references it. Any operation can
return these errors, so listing them per operation wouldn't be accurate.

The variants also include the Bitbucket webhook codes, since those
classes are `SentryAPIException` subclasses too. `ImageTooLarge` and
`FileTooLarge` send an empty code, so they're left out.

Next: an `@sentry/api` PR that generates `KnownSentryErrorCode` and the
auth-challenge fields from this component.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A9cddb8264df48e793bc61c15cd431e51 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A9cddb8264df48e793bc61c15cd431e51)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A9cddb8264df48e793bc61c15cd431e51/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
BYK added a commit that referenced this pull request Nov 16, 2026
…131287)

Part of #131203. Stacked on #131346, and it includes #131253's commits
until that PR merges.

#131253 added `KnownSentryErrorCode` and the auth-challenge fields to
`@sentry/api` as hand-written copies of the backend's
`SentryAPIException` classes, which can drift. #131285 publishes those
classes as the spec's `SentryApiErrorBody` component, and #131346 marks
the auth challenges in it, so the SDK now generates from it:

- A new build step, `scripts/generate-error-codes.mjs`, reads the
component with `ts.factory` into `SENTRY_ERROR_EXTRA_KEYS`, a code →
`extra` keys table, and `SENTRY_AUTH_CHALLENGE_CODES`, the codes marked
with `x-sentry-auth-challenge`. The build fails if the component is
missing, an `extra` key isn't a string, or a variant has no boolean
`x-sentry-auth-challenge`.
- `KnownSentryErrorCode` is the table's keys. `SentryAuthChallenge`
covers the marked codes, and each carries its fields from the table:
`loginUrl` and `organizationSlug` for SSO, `orgSlug` for superuser,
`username` for sudo and email verification, and `next` for a disabled
member. `getSentryAuthChallenge` returns the same objects as before.
- No code lists are left in the SDK. The README's list of challenge
codes is checked against the spec.

The known codes now also include the three Bitbucket webhook codes the
backend defines. Nothing is removed from the public exports.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A9cddb8264df48e793bc61c15cd431e51 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A9cddb8264df48e793bc61c15cd431e51)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A9cddb8264df48e793bc61c15cd431e51/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
BYK added a commit that referenced this pull request Nov 16, 2026
…131362)

Part of #131203. Stacked on #131287. Merge order: #131346, then #131253,
then #131287, then this PR.

Some endpoints document a `code` enum in their own error responses
instead of sending a `SentryAPIException` body. Today that's the Seer
autofix 402 (`seer_out_of_credits`, `seer_out_of_quota`) and 409
(`scm_integration_required`, `repos_not_linked`, `run_in_flight`).
Clients such as getsentry/toolkit hand-write those strings
(getsentry/toolkit#2234), and `SentryApiError.code` stayed unset for
them, because the code sits at the top level of the body.

- **Known codes:** `scripts/generate-error-codes.mjs` also collects
every string `code` enum value in documented non-2xx response bodies,
including `oneOf`/`anyOf`/`allOf` members. It drops the ones
`SentryApiErrorBody` already has and emits the rest as
`SentryOperationErrorCode`, which `KnownSentryErrorCode` now includes.
- **Top-level `code`:** when a body has no `SentryAPIException`
`detail`, `SentryApiError.code` reads a non-empty top-level string
`code`. `detailMessage` comes from a string `detail`, and `extra` is
empty. Other fields, such as Seer's `denialReason`, stay on `body`.
`SentryAPIException` bodies read exactly as before, and win when a body
has both.
- **README:** the "Error codes" section describes both body shapes and
where the known codes come from.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A9cddb8264df48e793bc61c15cd431e51 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A9cddb8264df48e793bc61c15cd431e51)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A9cddb8264df48e793bc61c15cd431e51/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
@sentry-junior

sentry-junior Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded: these changes landed on master in #131287 (f54da81), which carried this PR's commits.

@sentry-junior sentry-junior Bot closed this Nov 16, 2026
BYK added a commit that referenced this pull request Nov 17, 2026
Part of #131203. Follow-up to #131253, which covers error codes and auth
challenges.

When a request uses a project's old slug, the project endpoints answer
with a 302 to the same URL with the new slug. That body has `slug` and
`detail.extra`, but no error `code`. `fetch` follows the redirect, so
callers never see that body. They just get the renamed project's data
back.

`getSentryProjectRename(request, response)` reports the rename anyway:

- **Followed redirect:** it compares the request URL with the final
response URL.
- **Unfollowed 302** (`redirect: "manual"` outside browsers): it reads
the `Location` header.
- **Result:** `{ organization, previousSlug, slug, url }`, but only when
the redirect stays on the same origin and only the project segment of
`/projects/{organization}/{project}/` changes. Everything else returns
`undefined`.

It's classification only and never redirects. The README's new "Renamed
projects" section shows how to use it from a response interceptor.
Browsers hide where an unfollowed redirect points, so in a browser only
followed redirects are reported.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A9cddb8264df48e793bc61c15cd431e51 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A9cddb8264df48e793bc61c15cd431e51)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A9cddb8264df48e793bc61c15cd431e51/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>

This branch was successfully deployed

1 active deployment
Preview — 645a4203 Deployed Nov 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant