Skip to content

feat(api): Generate error codes and challenge fields from the spec - #131287

Merged
BYK merged 8 commits into
feat/apidocs-auth-challenge-codesfrom
feat/api-generated-error-codes
Nov 16, 2026
Merged

BYK merged 8 commits into
feat/apidocs-auth-challenge-codesfrom
feat/api-generated-error-codes

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

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.

via @BYK.

--

View Session [Sentry]

sentry-junior Bot and others added 6 commits November 16, 2026 14:39
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]>
Co-Authored-By: BYK <[email protected]>

# Conflicts:
#	static/packages/api/build.mjs
#	static/packages/api/lib/sentry-errors.ts
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.
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]>
KnownSentryErrorCode and the auth-challenge extra fields were
hand-maintained copies of the backend's SentryAPIException classes. A
new build step reads the spec's SentryApiErrorBody component into
SENTRY_ERROR_EXTRA_KEYS, and both types now derive from it.
getSentryAuthChallenge reads each challenge's fields from the same
table and returns the same objects as before.

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot requested review from a team as code owners November 16, 2026 16:50
@sentry-junior
sentry-junior Bot requested a review from BYK November 16, 2026 16:50
@sentry-junior
sentry-junior Bot added this pull request to stack #131288 November 16, 2026 16:50
@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Nov 16, 2026
Base automatically changed from feat/apidocs-sentry-error-component to master November 16, 2026 20:13
@BYK
BYK requested a review from a team as a code owner November 16, 2026 20:13

@BYK BYK left a comment

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.

Looks like we still hard-code an error code list on the JS side? What am I missing here?

@sentry-junior

sentry-junior Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

@BYK KnownSentryErrorCode and each code's extra fields come from the spec now. The one list left in JS is CHALLENGE_CODES in lib/auth-challenges.ts, plus the matching README sentence. Those are the 7 codes the SDK treats as auth challenges.

That list stays because the spec doesn't say which codes are challenges, and status doesn't tell them apart either: data-secrecy is a 401 and user-required is a 403, and neither is a challenge.

If you want that generated too, I can add an auth_challenge = True attribute on those 7 exception classes in #131285, publish it as x-sentry-auth-challenge on each variant, and have this PR build CHALLENGE_CODES from it. Want me to?

@BYK

BYK commented Nov 16, 2026

Copy link
Copy Markdown
Member

If you want that generated too, I can add an auth_challenge = True attribute on those 7 exception classes in #131285, publish it as x-sentry-auth-challenge on each variant, and have this PR build CHALLENGE_CODES from it. Want me to?

Yes please. Also prevent drift or missing codes with a lint rule or test if possible

…ted-error-codes

# Conflicts:
#	src/sentry/api/exceptions.py
#	src/sentry/apidocs/error_codes.py
#	tests/sentry/apidocs/test_error_codes.py
getSentryAuthChallenge still listed its seven codes by hand. The
generator now also emits SENTRY_AUTH_CHALLENGE_CODES, the codes the
spec's SentryApiErrorBody marks with x-sentry-auth-challenge, and fails
when a variant doesn't say. SentryAuthChallenge and the classifier use
that list, and a test checks the README names the same codes.
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🚨 Warning: This pull request contains Frontend and Backend changes!

It's discouraged to make changes to Sentry's Frontend and Backend in a single pull request. The Frontend and Backend are not atomically deployed. If the changes are interdependent of each other, they must be separated into two pull requests and be made forward or backwards compatible, such that the Backend or Frontend can be safely deployed independently.

Have questions? Please ask in the #discuss-dev-infra channel.

@sentry-junior
sentry-junior Bot removed this pull request from stack #131288 November 16, 2026 20:31
@sentry-junior
sentry-junior Bot changed the base branch from master to feat/apidocs-auth-challenge-codes November 16, 2026 20:31
@sentry-junior

sentry-junior Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Done. #131285 had already merged, so the backend part is a new PR, #131346, and this PR is now stacked on it.

  • feat(apidocs): Mark auth-challenge error codes in the error component #131346: SentryAPIException gets an auth_challenge attribute, True on the 7 challenge classes, and each SentryApiErrorBody variant publishes it as x-sentry-auth-challenge. To prevent missing codes, a test requires every 401 and 403 subclass to set the attribute in its own class body, even to False (as UserRequired and DataSecrecyError do). A new auth-related code therefore can't land without an explicit decision.
  • This PR (b6b01f7): the generator also emits SENTRY_AUTH_CHALLENGE_CODES from those markers, and CHALLENGE_CODES is gone. The build fails if any variant lacks a boolean x-sentry-auth-challenge. Tests check that exactly the marked codes classify, each with the extra fields the spec lists, and that the README's list of challenge codes matches the spec.

No hand-maintained code lists are left in the SDK. Merge #131346 first.

@sentry-junior

sentry-junior Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

@BYK
BYK merged commit f54da81 into master Nov 16, 2026
101 of 103 checks passed
@BYK
BYK deleted the feat/api-generated-error-codes branch November 16, 2026 22:53
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
…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]>
jan-auer added a commit that referenced this pull request Nov 17, 2026
…-store

* ref/inline-payload-defaults: (2542 commits)
  feat(classifiers): Support search syntax in classifier list query (#131423)
  ref(settings): Update settings tour entry copy (#131424)
  fix(conversations): Track tab switch from the View Timeline button (#131196)
  ref(sourcemaps): Read has_debug_ids in the source map debugger (#130425)
  ci(acceptance): Re-run on the override selective testing label (#131408)
  feat(inbox): Include inbox URLs in item responses (#131406)
  ref(flags): Remove `organizations:jira-explicit-mapping-removals` (frontend) (#130393)
  ci(acceptance): Select acceptance tests on pull requests (#131325)
  fix(audit-log): Render legacy internal integration token entries (#131042)
  ref(api): Drop the isRecord guard from SentryApiError body reads (#131394)
  ref: bump sentry-protos to 0.120.1 (#131396)
  feat(inbox): add URL configuration for inbox events (#131336)
  fix(circuit-breaker): Don't raise from record_error or record_success when redis fails (#131364)
  fix(redis): Let cluster client_args set the per-node connection limit (#131355)
  fix(sentry-apps): Stop retrying service hooks to blocked or unreachable URLs (#131171)
  fix(charts): Stop tooltip errors when a chart refresh removes series (#131341)
  perf(crons): Make the check-in timeout index partial (#131376)
  feat(api): Know per-operation error codes and read top-level codes (#131362)
  feat(api): Generate error codes and challenge fields from the spec (#131287)
  feat(apidocs): Mark auth-challenge error codes in the error component (#131346)
  ...

# Conflicts:
#	src/sentry/conf/server.py
#	src/sentry/event_manager.py
#	src/sentry/ingest/consumer/attachment_event.py
#	src/sentry/ingest/consumer/processors.py
#	src/sentry/processing/backpressure/topology.py
#	src/sentry/tasks/store.py
#	tests/sentry/ingest/ingest_consumer/test_ingest_consumer_kafka.py
#	tests/sentry/ingest/ingest_consumer/test_ingest_consumer_processing.py
#	tests/sentry/processing/backpressure/test_checking.py
#	tests/sentry/processing/backpressure/test_monitoring.py
#	tests/sentry/tasks/test_post_process.py
#	tests/sentry/tasks/test_store.py
#	tests/sentry/tasks/test_symbolication.py
xb18 pushed a commit to xb18/sentry that referenced this pull request Nov 22, 2026
…defaults

* origin/master: (2542 commits)
  feat(classifiers): Support search syntax in classifier list query (getsentry#131423)
  ref(settings): Update settings tour entry copy (getsentry#131424)
  fix(conversations): Track tab switch from the View Timeline button (getsentry#131196)
  ref(sourcemaps): Read has_debug_ids in the source map debugger (getsentry#130425)
  ci(acceptance): Re-run on the override selective testing label (getsentry#131408)
  feat(inbox): Include inbox URLs in item responses (getsentry#131406)
  ref(flags): Remove `organizations:jira-explicit-mapping-removals` (frontend) (getsentry#130393)
  ci(acceptance): Select acceptance tests on pull requests (getsentry#131325)
  fix(audit-log): Render legacy internal integration token entries (getsentry#131042)
  ref(api): Drop the isRecord guard from SentryApiError body reads (getsentry#131394)
  ref: bump sentry-protos to 0.120.1 (getsentry#131396)
  feat(inbox): add URL configuration for inbox events (getsentry#131336)
  fix(circuit-breaker): Don't raise from record_error or record_success when redis fails (getsentry#131364)
  fix(redis): Let cluster client_args set the per-node connection limit (getsentry#131355)
  fix(sentry-apps): Stop retrying service hooks to blocked or unreachable URLs (getsentry#131171)
  fix(charts): Stop tooltip errors when a chart refresh removes series (getsentry#131341)
  perf(crons): Make the check-in timeout index partial (getsentry#131376)
  feat(api): Know per-operation error codes and read top-level codes (getsentry#131362)
  feat(api): Generate error codes and challenge fields from the spec (getsentry#131287)
  feat(apidocs): Mark auth-challenge error codes in the error component (getsentry#131346)
  ...

# Conflicts:
#	tests/sentry/ingest/ingest_consumer/test_ingest_consumer_processing.py
#	tests/sentry/tasks/test_reprocessing2.py
#	tests/sentry/tasks/test_store.py
#	tests/sentry/tasks/test_symbolication.py

This branch was successfully deployed

1 active deployment
Preview — b6b01f7c 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: Backend Automatically applied to PRs that change backend components 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