Skip to content

feat(apidocs): Publish the SentryAPIException error body as a component - #131285

Merged
BYK merged 1 commit into
masterfrom
feat/apidocs-sentry-error-component
Nov 16, 2026
Merged

BYK merged 1 commit into
masterfrom
feat/apidocs-sentry-error-component

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

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.

via @BYK.

--

View Session [Sentry]

Add a SentryApiErrorBody schema component that describes the
{"detail": {"code", "message", "extra"}} body SentryAPIException
subclasses send. It is built from the exception classes: one detail
variant per code, with its status in x-sentry-status and the string keys
its extra can carry.

Each class declares those keys in a new extra_keys attribute, and a test
checks them against what the class's __init__ passes. Operations don't
reference the component, since any operation can return these errors.

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot requested review from a team as code owners November 16, 2026 16:43
@sentry-junior
sentry-junior Bot requested a review from BYK November 16, 2026 16:43
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 16, 2026
@sentry-junior
sentry-junior Bot added this pull request to stack #131288 November 16, 2026 16:50
@BYK
BYK merged commit cdf108c into master Nov 16, 2026
87 of 88 checks passed
@BYK
BYK deleted the feat/apidocs-sentry-error-component branch November 16, 2026 20:13
BYK pushed a commit that referenced this pull request Nov 16, 2026
…#131346)

Part of #131203. Follow-up to #131285.

`@sentry/api` (#131287) generates its error codes from the
`SentryApiErrorBody` component, but it still hard-codes which codes are
auth challenges, meaning errors the user clears by acting in Sentry
(SSO, sudo, superuser, staff, 2FA, member-disabled, email verification).
The spec can't say this today, and status doesn't tell them apart:
`data-secrecy` is a 401 and `user-required` is a 403, and neither is a
challenge.

- `SentryAPIException` gets an `auth_challenge` class attribute, `True`
on those seven classes.
- Each `SentryApiErrorBody` variant publishes it as
`x-sentry-auth-challenge`.
- Every 401 and 403 subclass must set the attribute in its own class
body, even to `False` (as `UserRequired` and `DataSecrecyError` do), so
a new auth-related code needs an explicit decision and can't be left out
of clients by accident.

<!-- 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>
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]>
joshuarli added a commit that referenced this pull request Nov 17, 2026
…31482)

`tests/sentry/apidocs/test_error_codes.py` (added in #131285) reads
`SentryAPIException` subclass source with `ast` instead of executing it,
so selective selection can't map exception edits to it. This adds it to
`ALWAYS_RUN_TESTS`, next to similar source-parsing entries.

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

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

--

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

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

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Joshua Li <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant