Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(auth): answer third-party outages with 502/503 instead of 500 (#8225)
teo1c6b3cbf1e51
diff --git a/.agents/rules/rust/code.md b/.agents/rules/rust/code.mdindex d42cbb47e9..4a3e4b90ac 100644--- a/.agents/rules/rust/code.md+++ b/.agents/rules/rust/code.md@@ -21,7 +21,9 @@ Type design, idioms, errors and observability, performance, and tests.- **CS-13** Model tri-state data (not-loaded / missing / present) as one flatenum, not nested `Option`s or ad-hoc flags.- **CS-19** Give third-party errors their own variant — don't collapse e.g. a- `jsonwebtoken` failure into a generic internal error.+ `jsonwebtoken` failure into a generic internal error. A provider outage+ (5xx, 429, unreachable, undecodable) is an `upstream_error::UpstreamError`,+ answered 502/503 — see `crates/upstream_error`.- **CS-20** Depending on a rate-limited external provider requires a fallback(fallback model, retry story, or documented degradation).- **CS-21** Wire usage metering on every invocation path — MCP-triggered tooldiff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex 475bb5657d..e455121bfb 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -541,6 +541,7 @@"crates/teams","crates/trigger_context","crates/unfurl",+ "crates/upstream_error","crates/user_quota","crates/workspace-hack"],@@ -696,6 +697,7 @@"crates/teams","crates/trigger_context","crates/unfurl",+ "crates/upstream_error","crates/user_quota","crates/workspace-hack"],@@ -897,6 +899,7 @@"crates/trigger_context","crates/unfurl","crates/universal_input",+ "crates/upstream_error","crates/user_quota","crates/workspace-hack","services/agent_harness_service",@@ -1082,6 +1085,7 @@"crates/teams","crates/trigger_context","crates/unfurl",+ "crates/upstream_error","crates/user_quota","crates/workspace-hack"],@@ -2421,6 +2425,7 @@"crates/teams","crates/trigger_context","crates/unfurl",+ "crates/upstream_error","crates/user_language","crates/user_quota","crates/workspace-hack",@@ -4195,6 +4200,7 @@"crates/teams","crates/trigger_context","crates/unfurl",+ "crates/upstream_error","crates/user_quota","crates/work_feed","crates/workspace-hack"@@ -5362,6 +5368,7 @@"crates/teams","crates/trigger_context","crates/unfurl",+ "crates/upstream_error","crates/user_api_key","crates/user_quota","crates/webhook",@@ -7217,6 +7224,7 @@],"fusionauth": ["crates/fusionauth",+ "crates/upstream_error","crates/workspace-hack"],"genai_telemetry": [@@ -7358,6 +7366,7 @@"crates/teams","crates/trigger_context","crates/unfurl",+ "crates/upstream_error","crates/user_quota","crates/workspace-hack"],@@ -8648,6 +8657,7 @@"crates/teams","crates/trigger_context","crates/unfurl",+ "crates/upstream_error","crates/user_quota","crates/workspace-hack"],@@ -10182,6 +10192,7 @@"crates/macro_env_var","crates/macro_user_id","crates/remote_env_var",+ "crates/upstream_error","crates/workspace-hack","services/mcp_auth_proxy"],@@ -11997,6 +12008,7 @@"crates/teams","crates/trigger_context","crates/unfurl",+ "crates/upstream_error","crates/user_quota","crates/workspace-hack"],@@ -12503,6 +12515,7 @@"crates/sync_service_client","crates/system_properties","crates/trigger_context",+ "crates/upstream_error","crates/user_quota","crates/workspace-hack","tooling/seed_cli"@@ -14192,6 +14205,10 @@"crates/workspace-hack","services/upload_extractor_lambda_trigger"],+ "upstream_error": [+ "crates/upstream_error",+ "crates/workspace-hack"+ ],"user_api_key": ["crates/bot_id","crates/bot_token",diff --git a/Cargo.lock b/Cargo.lockindex 8e08f3646e..e21ec02430 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2245,6 +2245,7 @@ dependencies = ["tower-cookies","tower-http","tracing",+ "upstream_error","url","urlencoding","user_language",@@ -8451,6 +8452,7 @@ dependencies = ["thiserror 2.0.18","tokio","tracing",+ "upstream_error","urlencoding","uuid","wiremock",@@ -8906,6 +8908,7 @@ dependencies = ["tokio","tower 0.5.3","tracing",+ "upstream_error","url","urlencoding","utoipa",@@ -21175,6 +21178,23 @@ dependencies = ["workspace-hack",]+[[package]]+name = "upstream_error"+version = "0.1.0"+dependencies = [+ "axum",+ "http 1.4.0",+ "http-body-util",+ "reqwest 0.13.4",+ "serde",+ "serde_json",+ "thiserror 2.0.18",+ "tokio",+ "utoipa",+ "wiremock",+ "workspace-hack",+]+[[package]]name = "url"version = "2.5.8"diff --git a/Cargo.toml b/Cargo.tomlindex 995831eec7..25606907e9 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -131,6 +131,7 @@ members = ["crates/system_skills","crates/task_dedup","crates/unfurl",+ "crates/upstream_error","crates/user_api_key","crates/user_language","crates/webhook_signature",diff --git a/apps/web/src/lib/queries/auth/github-link.test.ts b/apps/web/src/lib/queries/auth/github-link.test.tsindex 7ee78e11d2..6eda07dfb0 100644--- a/apps/web/src/lib/queries/auth/github-link.test.ts+++ b/apps/web/src/lib/queries/auth/github-link.test.ts@@ -15,7 +15,7 @@ it('only treats a confirmed missing account as unlinked', async () => {mocks.check.mockResolvedValue(err([{ code: 'NOT_FOUND' }]));expect(await fetchGithubLinkStatus()).toEqual({ status: 'unlinked' });});-it.each(['SERVER_ERROR', 'UNAUTHORIZED', 'NETWORK_ERROR'])(+it.each(['SERVER_ERROR', 'HTTP_ERROR', 'UNAUTHORIZED', 'NETWORK_ERROR'])('preserves %s as a failure instead of asking for OAuth',async (code) => {mocks.check.mockResolvedValue(err([{ code }]));diff --git a/apps/web/src/lib/service-clients/service-auth/client.test.ts b/apps/web/src/lib/service-clients/service-auth/client.test.tsindex 26baaa3e74..94755a6130 100644--- a/apps/web/src/lib/service-clients/service-auth/client.test.ts+++ b/apps/web/src/lib/service-clients/service-auth/client.test.ts@@ -141,3 +141,73 @@ describe('GitHub reconnect', () => {);});});++describe('GitHub outages', () => {+ function answerWith(status: number, body: string) {+ fetchCheckout.mockImplementationOnce(+ async (_input: RequestInfo, init: FetchWithTokenInit) => {+ const error = await init.errorResponseHandler?.(+ new Response(body, { status })+ );+ if (!error) throw new Error('Expected a GitHub error handler');+ return err([error]);+ }+ );+ }+ const upstream = (message: string) =>+ JSON.stringify({ message, code: 'upstream_rate_limited' });+ const pullRequest = { owner: 'acme', repo: 'app', number: 7 };++ test.each([+ [503, 'GitHub is limiting requests right now. Try again shortly.'],+ [+ 502,+ 'GitHub returned an unexpected response. Try again in a few minutes.',+ ],+ ])(+ 'shows the %i explanation when a merge fails upstream',+ async (status, message) => {+ answerWith(status, upstream(message));+ const result =+ await authServiceClient.mergeGithubPullRequest(pullRequest);+ expect(result._unsafeUnwrapErr()[0]).toEqual({+ code: 'HTTP_ERROR',+ message,+ });+ }+ );++ test('shows the explanation when a state change or link check fails upstream', async () => {+ const message =+ 'The sign-in service is temporarily unavailable. Try again in a few minutes.';+ answerWith(503, upstream(message));+ const state = await authServiceClient.setGithubPullRequestState({+ ...pullRequest,+ state: 'closed',+ });+ expect(state._unsafeUnwrapErr()[0]).toEqual({+ code: 'HTTP_ERROR',+ message,+ });++ answerWith(503, upstream(message));+ const link = await authServiceClient.checkGithubLinkStatus();+ expect(link._unsafeUnwrapErr()[0]).toEqual({ code: 'HTTP_ERROR', message });+ });++ test('falls back to the status for an unreadable outage body and keeps 500s', async () => {+ answerWith(503, '<html>maintenance</html>');+ const outage = await authServiceClient.mergeGithubPullRequest(pullRequest);+ expect(outage._unsafeUnwrapErr()[0]).toEqual({+ code: 'HTTP_ERROR',+ message: 'HTTP error! status: 503',+ });++ answerWith(500, JSON.stringify({ message: 'internal error' }));+ const bug = await authServiceClient.mergeGithubPullRequest(pullRequest);+ expect(bug._unsafeUnwrapErr()[0]).toEqual({+ code: 'SERVER_ERROR',+ message: 'Internal server error',+ });+ });+});diff --git a/apps/web/src/lib/service-clients/service-auth/client.ts b/apps/web/src/lib/service-clients/service-auth/client.tsindex ec79c788b6..3fe1da5a2c 100644--- a/apps/web/src/lib/service-clients/service-auth/client.ts+++ b/apps/web/src/lib/service-clients/service-auth/client.ts@@ -320,6 +320,16 @@ const githubErrorResponseHandler: ErrorResponseHandler<GithubReauthenticationErrreturn { code: 'GONE', message: 'Resource deleted' };case 500:return { code: 'SERVER_ERROR', message: 'Internal server error' };+ // GitHub, Google, or the sign-in service is down or throttling. The+ // body names which and says to retry later, in words for the user.+ case 502:+ case 503:+ return {+ code: 'HTTP_ERROR',+ message:+ (await readErrorMessage(response)) ??+ `HTTP error! status: ${response.status}`,+ };default:return {code: 'HTTP_ERROR',diff --git a/apps/web/src/lib/service-clients/service-auth/generated/client.ts b/apps/web/src/lib/service-clients/service-auth/generated/client.tsindex 4c1696b18b..8ca534aaf9 100644--- a/apps/web/src/lib/service-clients/service-auth/generated/client.ts+++ b/apps/web/src/lib/service-clients/service-auth/generated/client.ts@@ -107,6 +107,7 @@ import type {TrialEligibility,UpdateAutoReloadRequest,UpdateOverageRequest,+ UpstreamErrorResponse,UsageSnapshot,UserLanguage,UserLinkResponse,@@ -1457,6 +1458,16 @@ export type setGithubPullRequestDraftResponse500 = {status: 500;};+export type setGithubPullRequestDraftResponse502 = {+ data: UpstreamErrorResponse;+ status: 502;+};++export type setGithubPullRequestDraftResponse503 = {+ data: UpstreamErrorResponse;+ status: 503;+};+export type setGithubPullRequestDraftResponseSuccess =setGithubPullRequestDraftResponse200 & {headers: Headers;@@ -1468,6 +1479,8 @@ export type setGithubPullRequestDraftResponseError = (| setGithubPullRequestDraftResponse422| setGithubPullRequestDraftResponse428| setGithubPullRequestDraftResponse500+ | setGithubPullRequestDraftResponse502+ | setGithubPullRequestDraftResponse503) & {headers: Headers;};@@ -1531,6 +1544,16 @@ export type enrichGithubPullRequestsResponse500 = {status: 500;};+export type enrichGithubPullRequestsResponse502 = {+ data: UpstreamErrorResponse;+ status: 502;+};++export type enrichGithubPullRequestsResponse503 = {+ data: UpstreamErrorResponse;+ status: 503;+};+export type enrichGithubPullRequestsResponseSuccess =enrichGithubPullRequestsResponse200 & {headers: Headers;@@ -1540,6 +1563,8 @@ export type enrichGithubPullRequestsResponseError = (| enrichGithubPullRequestsResponse404| enrichGithubPullRequestsResponse428| enrichGithubPullRequestsResponse500+ | enrichGithubPullRequestsResponse502+ | enrichGithubPullRequestsResponse503) & {headers: Headers;};@@ -1620,6 +1645,16 @@ export type mergeGithubPullRequestResponse500 = {status: 500;};+export type mergeGithubPullRequestResponse502 = {+ data: UpstreamErrorResponse;+ status: 502;+};++export type mergeGithubPullRequestResponse503 = {+ data: UpstreamErrorResponse;+ status: 503;+};+export type mergeGithubPullRequestResponseSuccess =mergeGithubPullRequestResponse200 & {headers: Headers;@@ -1632,6 +1667,8 @@ export type mergeGithubPullRequestResponseError = (| mergeGithubPullRequestResponse422| mergeGithubPullRequestResponse428| mergeGithubPullRequestResponse500+ | mergeGithubPullRequestResponse502+ | mergeGithubPullRequestResponse503) & {headers: Headers;};@@ -1702,6 +1739,16 @@ export type getGithubPullRequestMergeabilityResponse500 = {status: 500;};+export type getGithubPullRequestMergeabilityResponse502 = {+ data: UpstreamErrorResponse;+ status: 502;+};++export type getGithubPullRequestMergeabilityResponse503 = {+ data: UpstreamErrorResponse;+ status: 503;+};+export type getGithubPullRequestMergeabilityResponseSuccess =getGithubPullRequestMergeabilityResponse200 & {headers: Headers;@@ -1712,6 +1759,8 @@ export type getGithubPullRequestMergeabilityResponseError = (| getGithubPullRequestMergeabilityResponse422| getGithubPullRequestMergeabilityResponse428| getGithubPullRequestMergeabilityResponse500+ | getGithubPullRequestMergeabilityResponse502+ | getGithubPullRequestMergeabilityResponse503) & {headers: Headers;};@@ -1786,6 +1835,16 @@ export type setGithubPullRequestStateResponse500 = {status: 500;};+export type setGithubPullRequestStateResponse502 = {+ data: UpstreamErrorResponse;+ status: 502;+};++export type setGithubPullRequestStateResponse503 = {+ data: UpstreamErrorResponse;+ status: 503;+};+export type setGithubPullRequestStateResponseSuccess =setGithubPullRequestStateResponse200 & {headers: Headers;@@ -1797,6 +1856,8 @@ export type setGithubPullRequestStateResponseError = (| setGithubPullRequestStateResponse422| setGithubPullRequestStateResponse428| setGithubPullRequestStateResponse500+ | setGithubPullRequestStateResponse502+ | setGithubPullRequestStateResponse503) & {headers: Headers;};@@ -2597,6 +2658,16 @@ export type initGithubLinkResponse500 = {status: 500;};+export type initGithubLinkResponse502 = {+ data: UpstreamErrorResponse;+ status: 502;+};++export type initGithubLinkResponse503 = {+ data: UpstreamErrorResponse;+ status: 503;+};+export type initGithubLinkResponseSuccess = initGithubLinkResponse200 & {headers: Headers;};@@ -2605,6 +2676,8 @@ export type initGithubLinkResponseError = (| initGithubLinkResponse401| initGithubLinkResponse429| initGithubLinkResponse500+ | initGithubLinkResponse502+ | initGithubLinkResponse503) & {headers: Headers;};@@ -2671,6 +2744,16 @@ export type deleteGithubLinkResponse500 = {status: 500;};+export type deleteGithubLinkResponse502 = {+ data: UpstreamErrorResponse;+ status: 502;+};++export type deleteGithubLinkResponse503 = {+ data: UpstreamErrorResponse;+ status: 503;+};+export type deleteGithubLinkResponseSuccess = deleteGithubLinkResponse200 & {headers: Headers;};@@ -2678,6 +2761,8 @@ export type deleteGithubLinkResponseError = (| deleteGithubLinkResponse400| deleteGithubLinkResponse401| deleteGithubLinkResponse500+ | deleteGithubLinkResponse502+ | deleteGithubLinkResponse503) & {headers: Headers;};@@ -2736,6 +2821,16 @@ export type checkGithubLinkStatusResponse500 = {status: 500;};+export type checkGithubLinkStatusResponse502 = {+ data: UpstreamErrorResponse;+ status: 502;+};++export type checkGithubLinkStatusResponse503 = {+ data: UpstreamErrorResponse;+ status: 503;+};+export type checkGithubLinkStatusResponseSuccess =checkGithubLinkStatusResponse200 & {headers: Headers;@@ -2745,6 +2840,8 @@ export type checkGithubLinkStatusResponseError = (| checkGithubLinkStatusResponse404| checkGithubLinkStatusResponse428| checkGithubLinkStatusResponse500+ | checkGithubLinkStatusResponse502+ | checkGithubLinkStatusResponse503) & {headers: Headers;};diff --git a/apps/web/src/lib/service-clients/service-auth/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-auth/generated/schemas/index.tsindex 7d80e03a6e..23d9d7022e 100644--- a/apps/web/src/lib/service-clients/service-auth/generated/schemas/index.ts+++ b/apps/web/src/lib/service-clients/service-auth/generated/schemas/index.ts@@ -234,6 +234,8 @@ export * from './trialEligibility';export * from './updateAutoReloadRequest';export * from './updateAutoReloadRequestMonthlySpendLimitCents';export * from './updateOverageRequest';+export * from './upstreamErrorCode';+export * from './upstreamErrorResponse';export * from './usageSnapshot';export * from './usageSnapshotBlockedReason';export * from './userLanguage';diff --git a/apps/web/src/lib/service-clients/service-auth/generated/schemas/upstreamErrorCode.ts b/apps/web/src/lib/service-clients/service-auth/generated/schemas/upstreamErrorCode.tsnew file mode 100644index 0000000000..d5eec99c1a--- /dev/null+++ b/apps/web/src/lib/service-clients/service-auth/generated/schemas/upstreamErrorCode.ts@@ -0,0 +1,20 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * authentication_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * What kind of upstream failure a 502 or 503 reports, for clients that+branch on it.+ */+export type UpstreamErrorCode =+ (typeof UpstreamErrorCode)[keyof typeof UpstreamErrorCode];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const UpstreamErrorCode = {+ upstream_unavailable: 'upstream_unavailable',+ upstream_rate_limited: 'upstream_rate_limited',+ upstream_invalid_response: 'upstream_invalid_response',+} as const;diff --git a/apps/web/src/lib/service-clients/service-auth/generated/schemas/upstreamErrorResponse.ts b/apps/web/src/lib/service-clients/service-auth/generated/schemas/upstreamErrorResponse.tsnew file mode 100644index 0000000000..9be9131d8d--- /dev/null+++ b/apps/web/src/lib/service-clients/service-auth/generated/schemas/upstreamErrorResponse.ts@@ -0,0 +1,17 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * authentication_service+ * OpenAPI spec version: 0.1.0+ */+import type { UpstreamErrorCode } from './upstreamErrorCode';++/**+ * The body of a 502 or 503 caused by a third-party service.+ */+export interface UpstreamErrorResponse {+ /** The kind of failure. */+ code: UpstreamErrorCode;+ /** What failed and what to do about it, written for the user. */+ message: string;+}diff --git a/apps/web/src/lib/service-clients/service-auth/openapi.json b/apps/web/src/lib/service-clients/service-auth/openapi.jsonindex 9ffcbb275d..a846226a1c 100644--- a/apps/web/src/lib/service-clients/service-auth/openapi.json+++ b/apps/web/src/lib/service-clients/service-auth/openapi.json@@ -1123,6 +1123,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -1192,6 +1212,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -1291,6 +1331,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -1370,6 +1430,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -1459,6 +1539,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -2155,6 +2255,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}},@@ -2202,6 +2322,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -2261,6 +2401,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -8316,6 +8476,30 @@}}},+ "UpstreamErrorCode": {+ "type": "string",+ "description": "What kind of upstream failure a 502 or 503 reports, for clients that\nbranch on it.",+ "enum": [+ "upstream_unavailable",+ "upstream_rate_limited",+ "upstream_invalid_response"+ ]+ },+ "UpstreamErrorResponse": {+ "type": "object",+ "description": "The body of a 502 or 503 caused by a third-party service.",+ "required": ["message", "code"],+ "properties": {+ "code": {+ "$ref": "#/components/schemas/UpstreamErrorCode",+ "description": "The kind of failure."+ },+ "message": {+ "type": "string",+ "description": "What failed and what to do about it, written for the user."+ }+ }+ },"UsageSnapshot": {"type": "object","description": "The payer's current-period position, as shown in Billing settings and used\nby the gate.",diff --git a/crates/fusionauth/AGENTS.md b/crates/fusionauth/AGENTS.mdindex b20713704e..8f09bf956a 100644--- a/crates/fusionauth/AGENTS.md+++ b/crates/fusionauth/AGENTS.md@@ -24,7 +24,7 @@ passwordless/password flows against the FusionAuth HTTP API.## Boundaries-- Depends on: nothing Macro-specific beyond `workspace-hack`; this is a plain+- Depends on: `upstream_error` for outage classification; otherwise a plainHTTP client crate.- Used by: `authentication_service`, `mcp_auth_proxy`, `mcp_service`,`tooling/seed_cli`, `github` (for linking).@@ -38,9 +38,18 @@ passwordless/password flows against the FusionAuth HTTP API.## Checks- `cargo test -p fusionauth`.+- `just check` runs `rules/ast-grep/rust-fusionauth-classify-transport.yml`.## Gotchas+- Map request failures with `.map_err(FusionAuthClientError::transport)` and+ statuses the endpoint does not document with+ `FusionAuthClientError::unexpected_response` (or `unexpected` after reading+ the body). They turn outages, throttling, timeouts, and undecodable bodies+ into `Upstream` (502/503 via `crates/upstream_error`); a hand-written+ `Generic` becomes a 500.+- Calls to Google or Microsoft name the provider:+ `FusionAuthClientError::provider_transport("Google", e)`.- Link lookup and unlink map HTTP 400 `userId` / `[invalid]userId` errors to`UserDoesNotExist`; other validation, authentication, and server failuresmust remain errors. Never log identity-provider link tokens.diff --git a/crates/fusionauth/Cargo.toml b/crates/fusionauth/Cargo.tomlindex 96fbc2fb77..d752aabe9b 100644--- a/crates/fusionauth/Cargo.toml+++ b/crates/fusionauth/Cargo.toml@@ -14,6 +14,9 @@ serde_json = { workspace = true }serde_urlencoded = { workspace = true }thiserror = { workspace = true }tracing = { workspace = true }+upstream_error = { path = "../upstream_error", default-features = false, features = [+ "reqwest",+] }urlencoding = { workspace = true }uuid = { workspace = true }workspace-hack = { version = "0.1", path = "../workspace-hack" }diff --git a/crates/fusionauth/src/apple.rs b/crates/fusionauth/src/apple.rsindex 38ffd99acf..8dc919f8d5 100644--- a/crates/fusionauth/src/apple.rs+++ b/crates/fusionauth/src/apple.rs@@ -1,9 +1,6 @@use std::borrow::Cow;-use crate::{- FusionAuthClient, Result, UnauthedClient,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{FusionAuthClient, Result, UnauthedClient, error::FusionAuthClientError};#[derive(serde::Serialize, serde::Deserialize, Debug, Default)]struct AppleLoginRequestData<'a> {@@ -46,38 +43,21 @@ async fn login(.json(&request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status = res.status();match status {reqwest::StatusCode::OK => {tracing::trace!("apple login complete");- let body = res.json::<AppleLoginResponse>().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let body = res+ .json::<AppleLoginResponse>()+ .await+ .map_err(FusionAuthClientError::transport)?;Ok((body.token, body.refresh_token))}reqwest::StatusCode::NOT_FOUND => Err(FusionAuthClientError::IncorrectCode),- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(status=%status, body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}diff --git a/crates/fusionauth/src/error.rs b/crates/fusionauth/src/error.rsindex 388a9470d4..4a2190eb66 100644--- a/crates/fusionauth/src/error.rs+++ b/crates/fusionauth/src/error.rs@@ -1,3 +1,12 @@+use upstream_error::{UpstreamError, UpstreamFailure};++#[cfg(test)]+mod test;++/// FusionAuth's user-facing name in error messages. Users know it as the+/// place they sign in, not as FusionAuth.+pub const SERVICE_NAME: &str = "The sign-in service";+#[derive(thiserror::Error, Debug, serde::Serialize)]#[serde(tag = "type")]/// Errors that can occur when interacting with the FusionAuth API.@@ -32,6 +41,18 @@ pub enum FusionAuthClientError {/// Microsoft OAuth credentials were not configured.#[error("Microsoft OAuth not configured")]MicrosoftOAuthNotConfigured,+ /// FusionAuth, or a provider this crate calls directly (Google), was+ /// unreachable, overloaded, or answered with something unusable. Not the+ /// caller's fault; retry later.+ #[error("{service} {failure}")]+ Upstream {+ /// The failing service's user-facing name.+ #[serde(skip)]+ service: &'static str,+ /// What went wrong.+ #[serde(skip)]+ failure: UpstreamFailure,+ },/// A generic error occurred.#[error("an unknown error occurred")]Generic(GenericErrorResponse),@@ -40,6 +61,64 @@ pub enum FusionAuthClientError {InvalidGrant,}+impl FusionAuthClientError {+ /// The failure to answer with when FusionAuth itself failed, for callers+ /// that map errors to HTTP responses.+ pub fn upstream(&self) -> Option<UpstreamError> {+ match self {+ Self::Upstream { service, failure } => Some(UpstreamError {+ service,+ failure: *failure,+ }),+ _ => None,+ }+ }++ /// A request that got no usable answer: FusionAuth was unreachable, the+ /// connection dropped, or the body could not be decoded.+ pub(crate) fn transport(error: reqwest::Error) -> Self {+ Self::provider_transport(SERVICE_NAME, error)+ }++ /// [`Self::transport`] for a request to another provider.+ pub(crate) fn provider_transport(service: &'static str, error: reqwest::Error) -> Self {+ tracing::error!(error=?error, service, "request failed");+ Self::Upstream {+ service,+ failure: UpstreamFailure::from_transport(&error),+ }+ }++ /// Classifies a response with a status the call did not expect: FusionAuth+ /// outages and throttling become [`Self::Upstream`], and anything else+ /// stays [`Self::Generic`] with the body.+ pub(crate) async fn unexpected_response(response: reqwest::Response) -> Self {+ let status = response.status();+ let headers = response.headers().clone();+ match response.text().await {+ Ok(body) => Self::unexpected(status, &headers, body),+ Err(error) => Self::transport(error),+ }+ }++ /// [`Self::unexpected_response`] for a call that already read the body.+ pub(crate) fn unexpected(+ status: reqwest::StatusCode,+ headers: &reqwest::header::HeaderMap,+ body: String,+ ) -> Self {+ if let Some(failure) = UpstreamFailure::from_status(status, headers) {+ tracing::error!(status=%status, body=%body, "fusionauth is failing");+ return Self::Upstream {+ service: SERVICE_NAME,+ failure,+ };+ }+ tracing::error!(status=%status, body=%body, "unexpected response from fusionauth");+ Self::Generic(GenericErrorResponse { message: body })+ }+}+/// A generic error response containing a message.#[derive(serde::Serialize, Debug)]pub struct GenericErrorResponse {diff --git a/crates/fusionauth/src/error/test.rs b/crates/fusionauth/src/error/test.rsnew file mode 100644index 0000000000..4345a6da21--- /dev/null+++ b/crates/fusionauth/src/error/test.rs@@ -0,0 +1,150 @@+use std::{borrow::Cow, time::Duration};++use serde_json::json;+use upstream_error::{UpstreamError, UpstreamFailure};+use wiremock::matchers::{method, path};+use wiremock::{Mock, MockServer, ResponseTemplate};++use crate::FusionAuthClient;+use crate::error::FusionAuthClientError;+use crate::identity_provider::{IdentityProviderLink, LinkUserRequest};++use super::SERVICE_NAME;++fn client(base_url: String) -> FusionAuthClient {+ FusionAuthClient::new(+ "api-key".into(),+ "application-id".into(),+ "client-secret".into(),+ base_url,+ "http://localhost:28011/oauth/redirect".into(),+ "google-client-id".into(),+ "google-client-secret".into(),+ )+}++async fn links_answering(response: ResponseTemplate) -> FusionAuthClientError {+ let server = MockServer::start().await;+ Mock::given(method("GET"))+ .and(path("/api/identity-provider/link"))+ .respond_with(response)+ .mount(&server)+ .await;++ client(server.uri())+ .get_links("fusionauth-user-id", Some("github-idp-id".into()))+ .await+ .expect_err("non-200 responses are errors")+}++async fn link_user_answering(response: ResponseTemplate) -> FusionAuthClientError {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path("/api/identity-provider/link"))+ .respond_with(response)+ .mount(&server)+ .await;++ client(server.uri())+ .link_user(LinkUserRequest {+ identity_provider_link: IdentityProviderLink {+ display_name: Cow::Borrowed("octocat"),+ identity_provider_id: Cow::Borrowed("github-idp-id"),+ identity_provider_user_id: Cow::Borrowed("1"),+ user_id: Cow::Borrowed("fusionauth-user-id"),+ token: Cow::Borrowed("token"),+ },+ })+ .await+ .expect_err("non-200 responses are errors")+}++#[tokio::test]+async fn a_failing_fusionauth_is_unavailable() {+ let error = links_answering(ResponseTemplate::new(502).set_body_string("Bad Gateway")).await;++ assert_eq!(+ error.upstream(),+ Some(UpstreamError {+ service: SERVICE_NAME,+ failure: UpstreamFailure::Unavailable,+ })+ );+}++#[tokio::test]+async fn a_throttling_fusionauth_is_rate_limited() {+ let error =+ links_answering(ResponseTemplate::new(429).insert_header("retry-after", "30")).await;++ assert_eq!(+ error.upstream().map(|upstream| upstream.failure),+ Some(UpstreamFailure::RateLimited {+ retry_after: Some(Duration::from_secs(30)),+ })+ );+}++#[tokio::test]+async fn an_undecodable_success_is_an_invalid_response() {+ let error = {+ let server = MockServer::start().await;+ Mock::given(method("GET"))+ .and(path("/api/identity-provider/link"))+ .respond_with(ResponseTemplate::new(200).set_body_string("<html>"))+ .mount(&server)+ .await;+ client(server.uri())+ .get_links("fusionauth-user-id", None)+ .await+ .expect_err("an html body is not a link list")+ };++ assert_eq!(+ error.upstream().map(|upstream| upstream.failure),+ Some(UpstreamFailure::InvalidResponse)+ );+}++#[tokio::test]+async fn an_unreachable_fusionauth_is_unavailable() {+ let address = {+ let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind a free port");+ listener.local_addr().expect("listener address")+ };++ let error = client(format!("http://{address}"))+ .get_links("fusionauth-user-id", None)+ .await+ .expect_err("nothing listens on the port");++ assert_eq!(+ error.upstream().map(|upstream| upstream.failure),+ Some(UpstreamFailure::Unavailable)+ );+}++#[tokio::test]+async fn linking_an_already_linked_identity_is_reported() {+ let error = link_user_answering(ResponseTemplate::new(400).set_body_json(json!({+ "fieldErrors": {+ "identityProviderUserId": [{ "code": "[alreadyLinked]identityProviderUserId" }]+ }+ })))+ .await;++ assert!(matches!(+ error,+ FusionAuthClientError::IdentityProviderLinkAlreadyExists+ ));+}++#[tokio::test]+async fn linking_while_fusionauth_fails_is_unavailable() {+ let error = link_user_answering(ResponseTemplate::new(503)).await;++ assert_eq!(+ error.upstream().map(|upstream| upstream.failure),+ Some(UpstreamFailure::Unavailable)+ );+}diff --git a/crates/fusionauth/src/google/oauth.rs b/crates/fusionauth/src/google/oauth.rsindex 7960da43d1..57add4b98b 100644--- a/crates/fusionauth/src/google/oauth.rs+++ b/crates/fusionauth/src/google/oauth.rs@@ -8,6 +8,10 @@ use base64::{Engine,engine::general_purpose::{self},};+use upstream_error::UpstreamFailure;++#[cfg(test)]+mod test;#[derive(serde::Serialize, serde::Deserialize, Debug)]/// Request to refresh a Google OAuth2 access token.@@ -33,6 +37,10 @@ pub struct GoogleTokenResponse {pub id_token: String,}+/// Google's name in error messages users see.+const GOOGLE: &str = "Google";+const GOOGLE_TOKEN_URL: &str = "https://oauth2.googleapis.com/token";+/// Refreshes a Google OAuth2 access token using a refresh token./// See https://developers.google.com/identity/protocols/oauth2/web-server#offlinepub(crate) async fn refresh_google_token(@@ -40,6 +48,23 @@ pub(crate) async fn refresh_google_token(client_id: &str,client_secret: &str,refresh_token: &str,+) -> Result<GoogleTokenResponse> {+ refresh_google_token_at(+ client,+ GOOGLE_TOKEN_URL,+ client_id,+ client_secret,+ refresh_token,+ )+ .await+}++async fn refresh_google_token_at(+ client: &UnauthedClient,+ token_url: &str,+ client_id: &str,+ client_secret: &str,+ refresh_token: &str,) -> Result<GoogleTokenResponse> {let token_request = GoogleTokenRequest {client_id: client_id.to_string(),@@ -50,25 +75,18 @@ pub(crate) async fn refresh_google_token(let res = client.client()- .post("https://oauth2.googleapis.com/token")+ .post(token_url).json(&token_request).send().await- .map_err(|e| {- tracing::error!(error=?e, "failed to send google access token request");- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(|e| FusionAuthClientError::provider_transport(GOOGLE, e))?;match res.status() {reqwest::StatusCode::OK => {- let response = res.json::<GoogleTokenResponse>().await.map_err(|e| {- tracing::error!(error=?e, "unable to parse token response");- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let response = res+ .json::<GoogleTokenResponse>()+ .await+ .map_err(|e| FusionAuthClientError::provider_transport(GOOGLE, e))?;tracing::debug!(expires_in=?response.expires_in,@@ -80,6 +98,7 @@ pub(crate) async fn refresh_google_token(Ok(response)}status => {+ let headers = res.headers().clone();let error_text = res.text().await@@ -95,6 +114,13 @@ pub(crate) async fn refresh_google_token(return Err(FusionAuthClientError::InvalidGrant);}+ if let Some(failure) = UpstreamFailure::from_status(status, &headers) {+ return Err(FusionAuthClientError::Upstream {+ service: GOOGLE,+ failure,+ });+ }+Err(FusionAuthClientError::Generic(GenericErrorResponse {message: format!("Google token refresh failed with status {}: {}",diff --git a/crates/fusionauth/src/google/oauth/test.rs b/crates/fusionauth/src/google/oauth/test.rsnew file mode 100644index 0000000000..9c484a656c--- /dev/null+++ b/crates/fusionauth/src/google/oauth/test.rs@@ -0,0 +1,51 @@+use serde_json::json;+use upstream_error::{UpstreamError, UpstreamFailure};+use wiremock::matchers::method;+use wiremock::{Mock, MockServer, ResponseTemplate};++use super::{GOOGLE, refresh_google_token_at};+use crate::{UnauthedClient, error::FusionAuthClientError};++async fn refresh_answering(response: ResponseTemplate) -> FusionAuthClientError {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .respond_with(response)+ .mount(&server)+ .await;++ refresh_google_token_at(+ &UnauthedClient::new(),+ &format!("{}/token", server.uri()),+ "client-id",+ "client-secret",+ "refresh-token",+ )+ .await+ .expect_err("non-200 responses are errors")+}++#[tokio::test]+async fn a_failing_google_is_unavailable() {+ let error = refresh_answering(+ ResponseTemplate::new(503).set_body_json(json!({ "error": "internal_failure" })),+ )+ .await;++ assert_eq!(+ error.upstream(),+ Some(UpstreamError {+ service: GOOGLE,+ failure: UpstreamFailure::Unavailable,+ })+ );+}++#[tokio::test]+async fn a_revoked_grant_is_still_invalid_grant() {+ let error = refresh_answering(+ ResponseTemplate::new(400).set_body_json(json!({ "error": "invalid_grant" })),+ )+ .await;++ assert!(matches!(error, FusionAuthClientError::InvalidGrant));+}diff --git a/crates/fusionauth/src/identity_provider/link.rs b/crates/fusionauth/src/identity_provider/link.rsindex a2508f8942..c96e7cd8a7 100644--- a/crates/fusionauth/src/identity_provider/link.rs+++ b/crates/fusionauth/src/identity_provider/link.rs@@ -1,10 +1,7 @@use std::borrow::Cow;use std::collections::HashMap;-use crate::{- AuthedClient, Result,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{AuthedClient, Result, error::FusionAuthClientError};/// Structured shape of FusionAuth validation error responses./// See https://fusionauth.io/docs/v1/tech/apis/errors@@ -98,21 +95,13 @@ pub(crate) async fn get_links(.timeout(super::LINK_REQUEST_TIMEOUT).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status = res.status();match status {reqwest::StatusCode::OK => {- let response: RetrieveLinkResponse = res.json().await.map_err(|e| {- tracing::error!(error=?e, "unable to parse response");- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let response: RetrieveLinkResponse =+ res.json().await.map_err(FusionAuthClientError::transport)?;tracing::trace!(link_count = response.idp_links.len(), "links found");@@ -120,19 +109,12 @@ pub(crate) async fn get_links(}reqwest::StatusCode::NOT_FOUND => Err(FusionAuthClientError::NoIdentityProviderFound),_ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;if status == reqwest::StatusCode::BAD_REQUEST && is_missing_user(&body) {return Err(FusionAuthClientError::UserDoesNotExist);}- tracing::error!(%status, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: "unknown error".to_string(),- }))+ Err(FusionAuthClientError::unexpected(status, &headers, body))}}}@@ -176,35 +158,24 @@ pub(crate) async fn link_user(.json(&request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- match res.status() {- reqwest::StatusCode::OK => Ok(()),- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- if serde_json::from_str::<FusionAuthErrorBody>(&body)- .ok()- .as_ref()- .is_some_and(FusionAuthErrorBody::is_already_linked)- {- tracing::info!(body=%body, "fusionauth idp link already exists");- return Err(FusionAuthClientError::IdentityProviderLinkAlreadyExists);- }+ .map_err(FusionAuthClientError::transport)?;- tracing::error!(body=%body, "unexpected response from fusionauth");+ let status = res.status();+ if status == reqwest::StatusCode::OK {+ return Ok(());+ }- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;++ if serde_json::from_str::<FusionAuthErrorBody>(&body)+ .ok()+ .as_ref()+ .is_some_and(FusionAuthErrorBody::is_already_linked)+ {+ tracing::info!(body=%body, "fusionauth idp link already exists");+ return Err(FusionAuthClientError::IdentityProviderLinkAlreadyExists);}++ Err(FusionAuthClientError::unexpected(status, &headers, body))}diff --git a/crates/fusionauth/src/identity_provider/login.rs b/crates/fusionauth/src/identity_provider/login.rsindex 83e75119ef..fdba127b7f 100644--- a/crates/fusionauth/src/identity_provider/login.rs+++ b/crates/fusionauth/src/identity_provider/login.rs@@ -1,9 +1,6 @@use std::borrow::Cow;-use crate::{- Result, UnauthedClient,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{Result, UnauthedClient, error::FusionAuthClientError};#[derive(serde::Serialize, serde::Deserialize, Debug)]#[serde(rename_all = "snake_case")]@@ -52,11 +49,7 @@ pub(crate) async fn complete_identity_provider_login(.json(&request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK => {@@ -64,12 +57,7 @@ pub(crate) async fn complete_identity_provider_login(let response = res.json::<IdentityProviderLoginResponse>().await- .map_err(|e| {- tracing::error!(error=?e, "unable to parse response");- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;Ok((response.token, response.refresh_token))}@@ -82,18 +70,6 @@ pub(crate) async fn complete_identity_provider_login(tracing::trace!("user does not exist");Err(FusionAuthClientError::UserDoesNotExist)}- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}diff --git a/crates/fusionauth/src/identity_provider/lookup.rs b/crates/fusionauth/src/identity_provider/lookup.rsindex 46cd9486a3..de3e2319b5 100644--- a/crates/fusionauth/src/identity_provider/lookup.rs+++ b/crates/fusionauth/src/identity_provider/lookup.rs@@ -49,20 +49,11 @@ pub(crate) async fn get_idp_id_by_domain()).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK => {- let response = res.json().await.map_err(|e| {- tracing::error!(error=?e, "unable to parse response");- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let response = res.json().await.map_err(FusionAuthClientError::transport)?;let response = serde_json::from_value::<IdentityProviderLookupResponse>(response).map_err(|e| {@@ -80,8 +71,6 @@ pub(crate) async fn get_idp_id_by_domain(tracing::trace!("no identify provider matches domain");Ok(None)}- _ => Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: "".to_string(),- })),+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}diff --git a/crates/fusionauth/src/identity_provider/search.rs b/crates/fusionauth/src/identity_provider/search.rsindex 25f3fed053..bd18ac3dea 100644--- a/crates/fusionauth/src/identity_provider/search.rs+++ b/crates/fusionauth/src/identity_provider/search.rs@@ -38,11 +38,7 @@ pub(crate) async fn get_idp_id_by_name()).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK => {@@ -50,11 +46,7 @@ pub(crate) async fn get_idp_id_by_name(let body = res.json::<IdentityProviderSearchResponse>().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;if body.total == 0 {return Err(FusionAuthClientError::NoIdentityProviderFound);@@ -80,18 +72,6 @@ pub(crate) async fn get_idp_id_by_name(Ok(body.identity_providers[0].id.to_string())}- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}diff --git a/crates/fusionauth/src/identity_provider/test.rs b/crates/fusionauth/src/identity_provider/test.rsindex 372862e700..17f5175064 100644--- a/crates/fusionauth/src/identity_provider/test.rs+++ b/crates/fusionauth/src/identity_provider/test.rs@@ -65,8 +65,12 @@ async fn grant_requests_time_out_when_the_provider_stops_responding() {server.abort();request.abort();assert!(- matches!(result, Ok(Ok(Err(FusionAuthClientError::Generic(_))))),- "{method} must time out as an infrastructure error: {result:?}"+ matches!(+ &result,+ Ok(Ok(Err(error))) if error.upstream().map(|upstream| upstream.failure)+ == Some(upstream_error::UpstreamFailure::Unavailable)+ ),+ "{method} must time out as an unavailable FusionAuth: {result:?}");}}@@ -107,7 +111,6 @@ async fn other_failures_are_not_misclassified_as_deleted_users() {(400, json!({ "fieldErrors": { "identityProviderId": [{ "code": "[invalid]identityProviderId" }] } }).to_string()),(400, "invalid JSON".into()),(401, String::new()),- (500, json!({ "fieldErrors": { "userId": [{ "code": "[invalid]userId" }] } }).to_string()),] {let server = MockServer::start().await;Mock::given(path("/api/identity-provider/link"))@@ -119,6 +122,33 @@ async fn other_failures_are_not_misclassified_as_deleted_users() {}}+#[tokio::test]+async fn a_failing_fusionauth_is_not_misclassified_as_a_deleted_user() {+ let server = MockServer::start().await;+ Mock::given(path("/api/identity-provider/link"))+ .respond_with(ResponseTemplate::new(500).set_body_string(+ json!({ "fieldErrors": { "userId": [{ "code": "[invalid]userId" }] } }).to_string(),+ ))+ .expect(2)+ .mount(&server)+ .await;+ let client = client(server.uri());+ let unavailable = Some(upstream_error::UpstreamFailure::Unavailable);+ let lookup = client+ .get_links("user", Some("idp".into()))+ .await+ .unwrap_err();+ assert_eq!(+ lookup.upstream().map(|upstream| upstream.failure),+ unavailable+ );+ let unlink = client.unlink_user("user", "idp", "1").await.unwrap_err();+ assert_eq!(+ unlink.upstream().map(|upstream| upstream.failure),+ unavailable+ );+}+#[tokio::test]async fn absent_link_and_empty_links_are_distinct_from_infrastructure_errors() {let server = MockServer::start().await;diff --git a/crates/fusionauth/src/identity_provider/unlink.rs b/crates/fusionauth/src/identity_provider/unlink.rsindex 5192aaa2e8..7650f79ba6 100644--- a/crates/fusionauth/src/identity_provider/unlink.rs+++ b/crates/fusionauth/src/identity_provider/unlink.rs@@ -1,7 +1,4 @@-use crate::{- AuthedClient, Result,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{AuthedClient, Result, error::FusionAuthClientError};/// Unlinks a user from an identity provider/// https://fusionauth.io/docs/apis/identity-providers/links#unlink-a-user@@ -21,27 +18,19 @@ pub(crate) async fn unlink(.timeout(super::LINK_REQUEST_TIMEOUT).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status = res.status();- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-match status {reqwest::StatusCode::OK => Ok(()),reqwest::StatusCode::NOT_FOUND => Err(FusionAuthClientError::NoIdentityProviderFound), // no idp to unlink- reqwest::StatusCode::BAD_REQUEST if super::link::is_missing_user(&body) => {- Err(FusionAuthClientError::UserDoesNotExist)+ _ => {+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;+ if status == reqwest::StatusCode::BAD_REQUEST && super::link::is_missing_user(&body) {+ return Err(FusionAuthClientError::UserDoesNotExist);+ }+ Err(FusionAuthClientError::unexpected(status, &headers, body))}- _ => Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- })),}}diff --git a/crates/fusionauth/src/jwt/refresh.rs b/crates/fusionauth/src/jwt/refresh.rsindex a7d0673d5b..c56a5cfc79 100644--- a/crates/fusionauth/src/jwt/refresh.rs+++ b/crates/fusionauth/src/jwt/refresh.rs@@ -1,9 +1,6 @@use std::borrow::Cow;-use crate::{- Result, UnauthedClient,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{Result, UnauthedClient, error::FusionAuthClientError};#[derive(serde::Serialize, serde::Deserialize, Debug)]#[serde(rename_all = "camelCase")]@@ -45,38 +42,27 @@ pub async fn refresh_token(.json(&request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK => {tracing::trace!("token refreshed");- let body = res.json::<RefreshJwtResponse>().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let body = res+ .json::<RefreshJwtResponse>()+ .await+ .map_err(FusionAuthClientError::transport)?;Ok((body.token.into(), body.refresh_token.into()))}_ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let status = res.status();+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;if body.contains("The refresh token was not found or has expired") {Err(FusionAuthClientError::InvalidRefreshToken)} else {- tracing::error!(body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))+ Err(FusionAuthClientError::unexpected(status, &headers, body))}}}diff --git a/crates/fusionauth/src/jwt/revoke.rs b/crates/fusionauth/src/jwt/revoke.rsindex 4b96a2a8d3..1357121aa8 100644--- a/crates/fusionauth/src/jwt/revoke.rs+++ b/crates/fusionauth/src/jwt/revoke.rs@@ -1,7 +1,4 @@-use crate::{- AuthedClient, Result,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{AuthedClient, Result, error::FusionAuthClientError};/// Revokes a single refresh token. A token FusionAuth does not know (already/// revoked or expired) counts as revoked, so callers can retry.@@ -21,29 +18,13 @@ pub(crate) async fn revoke_refresh_token(.form(&[("token", refresh_token)]).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK | reqwest::StatusCode::NOT_FOUND => {tracing::trace!("refresh token revoked");Ok(())}- status => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(body=%body, %status, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}diff --git a/crates/fusionauth/src/jwt/test.rs b/crates/fusionauth/src/jwt/test.rsindex 268697b0c3..8a1c46fa26 100644--- a/crates/fusionauth/src/jwt/test.rs+++ b/crates/fusionauth/src/jwt/test.rs@@ -60,8 +60,12 @@ async fn revoke_refresh_token_reports_a_server_error() {.mount(&server).await;- client(server.uri())+ let error = client(server.uri()).revoke_refresh_token("refresh-token").await.expect_err("a server error must not count as a revocation");+ assert_eq!(+ error.upstream().map(|upstream| upstream.failure),+ Some(upstream_error::UpstreamFailure::Unavailable)+ );}diff --git a/crates/fusionauth/src/logout.rs b/crates/fusionauth/src/logout.rsindex a092da191f..187bb059bc 100644--- a/crates/fusionauth/src/logout.rs+++ b/crates/fusionauth/src/logout.rs@@ -1,7 +1,4 @@-use crate::{- FusionAuthClient, Result, UnauthedClient,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{FusionAuthClient, Result, UnauthedClient, error::FusionAuthClientError};use std::borrow::Cow;#[derive(serde::Serialize, serde::Deserialize, Debug)]@@ -20,11 +17,7 @@ async fn logout(client: &UnauthedClient, base_url: &str, request: LogoutRequest<.json(&request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status_code = res.status();match status_code {@@ -33,21 +26,18 @@ async fn logout(client: &UnauthedClient, base_url: &str, request: LogoutRequest<Ok(())}_ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;if status_code == reqwest::StatusCode::NOT_FOUND {return Err(FusionAuthClientError::UserDoesNotExist);}- tracing::error!(body=%body, status=%status_code, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))+ Err(FusionAuthClientError::unexpected(+ status_code,+ &headers,+ body,+ ))}}}diff --git a/crates/fusionauth/src/oauth.rs b/crates/fusionauth/src/oauth.rsindex 5a7e016784..6256f484a2 100644--- a/crates/fusionauth/src/oauth.rs+++ b/crates/fusionauth/src/oauth.rs@@ -81,11 +81,7 @@ async fn complete(.body(body).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;tracing::trace!("request sent");@@ -95,12 +91,7 @@ async fn complete(let body = res.json::<AuthorizationCodeGrantCompleteResponse>().await- .map_err(|e| {- tracing::error!(error=?e, "unable to decode successful oauth2 token response");- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;Ok(OAuth2Grant {access_token: body.access_token.into(),@@ -108,19 +99,7 @@ async fn complete(expires_in: body.expires_in,})}- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}diff --git a/crates/fusionauth/src/password.rs b/crates/fusionauth/src/password.rsindex 3244ede7e3..ba2eb8896c 100644--- a/crates/fusionauth/src/password.rs+++ b/crates/fusionauth/src/password.rs@@ -1,9 +1,6 @@use std::borrow::Cow;-use crate::{- AuthedClient, FusionAuthClient, Result,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{AuthedClient, FusionAuthClient, Result, error::FusionAuthClientError};#[derive(serde::Serialize, serde::Deserialize, Debug)]#[serde(rename_all = "camelCase")]@@ -39,41 +36,29 @@ async fn login(.json(&request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status = res.status();match status {reqwest::StatusCode::OK => {tracing::trace!("passwordless login complete");- let body = res.json::<PasswordLoginResponse>().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let body = res+ .json::<PasswordLoginResponse>()+ .await+ .map_err(FusionAuthClientError::transport)?;Ok((body.token.into(), body.refresh_token.into()))}reqwest::StatusCode::ACCEPTED => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;tracing::warn!(body=%body, "user not registered to application");Err(FusionAuthClientError::UserNotRegistered)}reqwest::StatusCode::NOT_FOUND => Err(FusionAuthClientError::IncorrectCode),_ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;if status.as_u16() == 212 {// Email not verified@@ -81,11 +66,7 @@ async fn login(return Err(FusionAuthClientError::UserNotVerified);}- tracing::error!(status=%status, body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))+ Err(FusionAuthClientError::unexpected(status, &headers, body))}}}diff --git a/crates/fusionauth/src/passwordless.rs b/crates/fusionauth/src/passwordless.rsindex 99eee55c4e..c8c3b28c16 100644--- a/crates/fusionauth/src/passwordless.rs+++ b/crates/fusionauth/src/passwordless.rs@@ -1,9 +1,6 @@use std::borrow::Cow;-use crate::{- AuthedClient, FusionAuthClient, Result,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{AuthedClient, FusionAuthClient, Result, error::FusionAuthClientError};#[derive(serde::Serialize, serde::Deserialize, Debug)]#[serde(rename_all = "snake_case")]@@ -87,11 +84,7 @@ async fn start(.json(&request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK => {@@ -99,11 +92,7 @@ async fn start(let body = res.json::<PasswordlessLoginStartResponse>().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;Ok(body.code.to_string())}@@ -111,19 +100,7 @@ async fn start(tracing::trace!("no user with loginId {} found", request.login_id);Err(FusionAuthClientError::UserDoesNotExist)}- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}@@ -141,30 +118,14 @@ async fn send(.json(&request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK => {tracing::trace!("code generated");Ok(())}- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}@@ -184,11 +145,7 @@ async fn complete(.json(&request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status = res.status();match status {@@ -197,11 +154,7 @@ async fn complete(let body = res.json::<PasswordlessLoginCompleteResponse>().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;Ok(body)}@@ -210,17 +163,11 @@ async fn complete(return Err(FusionAuthClientError::IncorrectCode);}- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let headers = res.headers().clone();- tracing::error!(status=%status, body=%body, "unexpected response from fusionauth");+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))+ Err(FusionAuthClientError::unexpected(status, &headers, body))}}}diff --git a/crates/fusionauth/src/user/create.rs b/crates/fusionauth/src/user/create.rsindex 1c8e474e98..0c7e4121fc 100644--- a/crates/fusionauth/src/user/create.rs+++ b/crates/fusionauth/src/user/create.rs@@ -1,9 +1,6 @@use std::{borrow::Cow, net::IpAddr};-use crate::{- AuthedClient, Result,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{AuthedClient, Result, error::FusionAuthClientError};/// A user to create in FusionAuth.#[derive(serde::Serialize, serde::Deserialize, Debug)]@@ -85,56 +82,40 @@ pub(crate) async fn create_user(.json(&request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK => {tracing::trace!("user created");- let body = res.json::<CreateUserResponse>().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let body = res+ .json::<CreateUserResponse>()+ .await+ .map_err(FusionAuthClientError::transport)?;Ok(body.user.id.into())}reqwest::StatusCode::BAD_REQUEST => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let status = res.status();+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;if body.contains("[duplicate]") {Err(FusionAuthClientError::UserAlreadyExists)} else {- tracing::error!(body=%body, "unexpected response from fusionauth");- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))+ Err(FusionAuthClientError::unexpected(status, &headers, body))}}_ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let status = res.status();+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;if body.contains("[duplicate]user.email") {tracing::error!("user already exists");return Err(FusionAuthClientError::UserAlreadyExists);}- tracing::error!(body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))+ Err(FusionAuthClientError::unexpected(status, &headers, body))}}}diff --git a/crates/fusionauth/src/user/delete.rs b/crates/fusionauth/src/user/delete.rsindex c0fade35d9..ca8241c477 100644--- a/crates/fusionauth/src/user/delete.rs+++ b/crates/fusionauth/src/user/delete.rs@@ -1,7 +1,4 @@-use crate::{- AuthedClient, Result,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{AuthedClient, Result, error::FusionAuthClientError};/// Deletes a user in fusionauth/// https://fusionauth.io/docs/apis/users#delete-a-user@@ -16,11 +13,7 @@ pub(crate) async fn delete_user(.delete(format!("{base_url}/api/user/{user_id}?hardDelete=true")).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK => {@@ -28,19 +21,7 @@ pub(crate) async fn delete_user(Ok(())}reqwest::StatusCode::NOT_FOUND => Err(FusionAuthClientError::UserDoesNotExist),- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}@@ -57,30 +38,14 @@ pub(crate) async fn deactivate_user(.delete(format!("{base_url}/api/user/{user_id}")).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK => {tracing::trace!("user deactivated");Ok(())}- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}@@ -96,29 +61,13 @@ pub(crate) async fn reactivate_user(.put(format!("{base_url}/api/user/{user_id}?reactivate=true")).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;match res.status() {reqwest::StatusCode::OK => {tracing::trace!("user reactivated");Ok(())}- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(body=%body, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}diff --git a/crates/fusionauth/src/user/get.rs b/crates/fusionauth/src/user/get.rsindex c75fe7b69b..3a64ae9c58 100644--- a/crates/fusionauth/src/user/get.rs+++ b/crates/fusionauth/src/user/get.rs@@ -1,9 +1,6 @@use std::borrow::Cow;-use crate::{- AuthedClient, Result,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{AuthedClient, Result, error::FusionAuthClientError};#[derive(serde::Serialize, serde::Deserialize, Debug)]#[serde(rename_all = "camelCase")]@@ -37,37 +34,20 @@ pub(crate) async fn get_user_active(.get(format!("{base_url}/api/user/{user_id}")).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status_code = res.status();match status_code {reqwest::StatusCode::OK => {- let body = res.json::<GetUserResponse>().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let body = res+ .json::<GetUserResponse>()+ .await+ .map_err(FusionAuthClientError::transport)?;Ok(body.user.active.unwrap_or(true))}reqwest::StatusCode::NOT_FOUND => Err(FusionAuthClientError::UserDoesNotExist),- _ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;-- tracing::error!(body=%body, status=%status_code, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))- }+ _ => Err(FusionAuthClientError::unexpected_response(res).await),}}@@ -85,40 +65,32 @@ pub(crate) async fn get_user_id_by_email(.get(format!("{base_url}/api/user?email={url_email}")).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status_code = res.status();match status_code {reqwest::StatusCode::OK => {tracing::trace!("user found");- let body = res.json::<GetUserResponse>().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let body = res+ .json::<GetUserResponse>()+ .await+ .map_err(FusionAuthClientError::transport)?;Ok(body.user.id.into())}_ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;if status_code == reqwest::StatusCode::NOT_FOUND {return Err(FusionAuthClientError::UserDoesNotExist);}- tracing::error!(body=%body, status=%status_code, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))+ Err(FusionAuthClientError::unexpected(+ status_code,+ &headers,+ body,+ ))}}}diff --git a/crates/fusionauth/src/user/register.rs b/crates/fusionauth/src/user/register.rsindex 4cbf5b35af..c4e420e840 100644--- a/crates/fusionauth/src/user/register.rs+++ b/crates/fusionauth/src/user/register.rs@@ -1,9 +1,6 @@use std::borrow::Cow;-use crate::{- AuthedClient, Result,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{AuthedClient, Result, error::FusionAuthClientError};#[derive(serde::Serialize, serde::Deserialize, Debug)]#[serde(rename_all = "camelCase")]@@ -35,11 +32,7 @@ pub(crate) async fn register_user<'a>(.json(®ister_user_request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status_code = res.status();match status_code {@@ -48,11 +41,8 @@ pub(crate) async fn register_user<'a>(Ok(())}_ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;if status_code == reqwest::StatusCode::BAD_REQUEST&& body.contains("[duplicate]registration")@@ -64,11 +54,11 @@ pub(crate) async fn register_user<'a>(return Err(FusionAuthClientError::UserDoesNotExist);}- tracing::error!(body=%body, status=%status_code, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))+ Err(FusionAuthClientError::unexpected(+ status_code,+ &headers,+ body,+ ))}}}diff --git a/crates/fusionauth/src/user/verify.rs b/crates/fusionauth/src/user/verify.rsindex f822aafa5d..5c754bfee5 100644--- a/crates/fusionauth/src/user/verify.rs+++ b/crates/fusionauth/src/user/verify.rs@@ -1,9 +1,6 @@use std::{borrow::Cow, net::IpAddr};-use crate::{- AuthedClient, Result,- error::{FusionAuthClientError, GenericErrorResponse},-};+use crate::{AuthedClient, Result, error::FusionAuthClientError};#[derive(serde::Serialize, serde::Deserialize, Debug)]#[serde(rename_all = "camelCase")]@@ -28,11 +25,7 @@ pub(crate) async fn verify_email<'a>(.json(&verify_email_request).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status_code = res.status();match status_code {@@ -41,11 +34,8 @@ pub(crate) async fn verify_email<'a>(Ok(())}_ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;if status_code == reqwest::StatusCode::BAD_REQUEST {return Err(FusionAuthClientError::UserAlreadyRegistered);@@ -55,11 +45,11 @@ pub(crate) async fn verify_email<'a>(return Err(FusionAuthClientError::UserDoesNotExist);}- tracing::error!(body=%body, status=%status_code, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))+ Err(FusionAuthClientError::unexpected(+ status_code,+ &headers,+ body,+ ))}}}@@ -81,11 +71,7 @@ pub async fn resend_verify_email()).send().await- .map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ .map_err(FusionAuthClientError::transport)?;let status_code = res.status();match status_code {@@ -94,21 +80,18 @@ pub async fn resend_verify_email(Ok(())}_ => {- let body = res.text().await.map_err(|e| {- FusionAuthClientError::Generic(GenericErrorResponse {- message: e.to_string(),- })- })?;+ let headers = res.headers().clone();+ let body = res.text().await.map_err(FusionAuthClientError::transport)?;if status_code == reqwest::StatusCode::NOT_FOUND {return Err(FusionAuthClientError::UserDoesNotExist);}- tracing::error!(body=%body, status=%status_code, "unexpected response from fusionauth");-- Err(FusionAuthClientError::Generic(GenericErrorResponse {- message: body,- }))+ Err(FusionAuthClientError::unexpected(+ status_code,+ &headers,+ body,+ ))}}}diff --git a/crates/github/AGENTS.md b/crates/github/AGENTS.mdindex d61e2deac9..9fd433347d 100644--- a/crates/github/AGENTS.md+++ b/crates/github/AGENTS.md@@ -59,7 +59,7 @@ link flow, following the hexagonal architecture pattern.## Gotchas- Missing FusionAuth identities/grants require reauthentication; infrastructure- errors remain errors. After OAuth proves GitHub identity, refresh the grant+ errors remain errors (outages as `GithubError::Upstream`, below). After OAuth proves GitHub identity, refresh the grantbefore returning an existing link. Reassign every sharer when its grant owneris gone, using the old owner as a compare-and-swap condition. Hold the userguard then the GitHub account guard through OAuth grant writes and DB changes;@@ -74,6 +74,14 @@ link flow, following the hexagonal architecture pattern.- Webhook signature verification uses `hmac`/`subtle` for constant-timecomparison — don't swap in a plain `==` when touching`github_sync_router.rs`.+- A GitHub or sign-in outage is `GithubError::Upstream`, answered 502/503 by+ `inbound.rs`. Outbound adapters classify it (`github_oauth_client.rs`+ `throttling`/`unexpected_response`, `github_auth_client.rs`+ `fusionauth_error`); everything else they cannot explain stays `Internal`.+ Port errors reach the service as `GithubError`, so map them with+ `.map_err(Into::into)`, never `GithubError::Internal(e.into())`, which turns+ an outage back into a 500. A GitHub 403 is a refusal only after+ `throttling` rules out a rate limit.- `GithubError::Forbidden`, `SetupUserNotLinked`, and `RepositoryUnavailable`all map to the same 403 deliberately — don't let a response distinguish"app not installed" from "installed somewhere you have no claim to".diff --git a/crates/github/Cargo.toml b/crates/github/Cargo.tomlindex 74ee7c3b98..7298b191b0 100644--- a/crates/github/Cargo.toml+++ b/crates/github/Cargo.toml@@ -13,6 +13,7 @@ axum = ["dep:model-error-response","dep:utoipa","github_pull_requests/schema",+ "upstream_error/axum",]default = ["axum", "inbound", "link", "outbound", "ports", "sync"]inbound = ["axum", "ports"]@@ -28,6 +29,7 @@ outbound = ["dep:tokio","dep:urlencoding","ports",+ "upstream_error/reqwest",]ports = []sync = [@@ -85,6 +87,7 @@ subtle = { workspace = true }thiserror = { workspace = true }tracing = { workspace = true }urlencoding = { workspace = true, optional = true }+upstream_error = { path = "../upstream_error", default-features = false }url = { workspace = true }uuid = { workspace = true }diff --git a/crates/github/src/domain/models.rs b/crates/github/src/domain/models.rsindex 0b4512b60f..42d454ae06 100644--- a/crates/github/src/domain/models.rs+++ b/crates/github/src/domain/models.rs@@ -45,9 +45,11 @@ pub use sync::{TeamTaskReference, ValidatedGithubWebhookEvent, extract_github_mentions,pull_request_task_reference, strip_markdown_code,};+use upstream_error::UpstreamError;pub use webhook_delivery::{GithubWebhookReceived, GithubWebhookRepository, GithubWebhookSender, MAX_WEBHOOK_PAYLOAD_BYTES,};+/// Errors that can occur during github operations.#[derive(Debug, thiserror::Error)]pub enum GithubError {@@ -118,4 +120,9 @@ pub enum GithubError {/// The request asked for more pull requests than one call may cover.#[error("too many pull requests requested")]TooManyPullRequests,+ /// GitHub, or the sign-in service holding the user's GitHub grant, was+ /// down, throttling, or answered with something unusable. Not the+ /// caller's fault and not ours; retrying later can help.+ #[error(transparent)]+ Upstream(#[from] UpstreamError),}diff --git a/crates/github/src/domain/ports/link.rs b/crates/github/src/domain/ports/link.rsindex 0e482df175..ccf77c2aac 100644--- a/crates/github/src/domain/ports/link.rs+++ b/crates/github/src/domain/ports/link.rs@@ -97,7 +97,7 @@ pub trait GithubRepo: Send + Sync + 'static {#[cfg_attr(test, mockall::automock(type Err = anyhow::Error;))]pub trait GithubOauth: Send + Sync + 'static {/// The error type returned by repository operations.- type Err: Into<anyhow::Error> + Send + std::fmt::Debug;+ type Err: Into<GithubError> + Send + std::fmt::Debug;/// Constructs the oauth url to authenticate with github.///@@ -197,7 +197,7 @@ pub trait GithubOauth: Send + Sync + 'static {#[cfg_attr(test, mockall::automock(type Err = anyhow::Error;))]pub trait Auth: Send + Sync + 'static {/// The error type returned by repository operations.- type Err: Into<anyhow::Error> + Send + std::fmt::Debug;+ type Err: Into<GithubError> + Send + std::fmt::Debug;/// Creates or refreshes the grant after OAuth has verified the GitHub account.fn link_user(diff --git a/crates/github/src/domain/service/link.rs b/crates/github/src/domain/service/link.rsindex ecb82989ae..6f7cc96835 100644--- a/crates/github/src/domain/service/link.rs+++ b/crates/github/src/domain/service/link.rs@@ -99,14 +99,14 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService>&link.github_user_id,).await- .map_err(|e| GithubError::Internal(e.into()))?+ .map_err(Into::into)?.ok_or(GithubError::ReauthenticationRequired)?;let token_is_expired = self.oauth.is_access_token_expired(access_token.as_str()).await- .map_err(|e| GithubError::Internal(e.into()))?;+ .map_err(Into::into)?;if token_is_expired {return Err(GithubError::ReauthenticationRequired);@@ -208,7 +208,7 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> Githubfn construct_oauth_url(&self, redirect_uri: &str, state: &str) -> Result<String, GithubError> {self.oauth.construct_oauth_url(&self.config.client_id, redirect_uri, state)- .map_err(|e| GithubError::Internal(e.into()))+ .map_err(Into::into)}#[tracing::instrument(skip(self), err)]@@ -300,7 +300,7 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> Githubmerge_method,).await- .map_err(|error| GithubError::Internal(error.into()))?;+ .map_err(Into::into)?;let merge = match outcome {GithubMergeOutcome::Merged(merge) => merge,@@ -338,7 +338,7 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> Githubrequest.draft,).await- .map_err(|error| GithubError::Internal(error.into()))?;+ .map_err(Into::into)?;let draft = match outcome {GithubDraftOutcome::Changed { draft } => draft,@@ -375,7 +375,7 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> Githubrequest.state,).await- .map_err(|error| GithubError::Internal(error.into()))?;+ .map_err(Into::into)?;let state = match outcome {GithubStateOutcome::Changed { state } => state,@@ -412,7 +412,7 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> Githubself.oauth.get_pull_request_mergeability(access_token.as_str(), &pull_requests).await- .map_err(|error| GithubError::Internal(error.into()))+ .map_err(Into::into)}#[tracing::instrument(skip(self), err)]@@ -464,7 +464,7 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> Githubself.auth.delete_user_link(&link, &self.config.idp_id).await- .map_err(|e| GithubError::Internal(e.into()))?;+ .map_err(Into::into)?;}// Delete from repo@@ -494,13 +494,13 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> Githubcode,).await- .map_err(|e| GithubError::Internal(e.into()))?;+ .map_err(Into::into)?;let user_info = self.oauth.get_user_info(&tokens.access_token).await- .map_err(|e| GithubError::Internal(e.into()))?;+ .map_err(Into::into)?;tracing::trace!(github_user_id = user_info.id, "got github user identity");@@ -543,7 +543,7 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> Github&tokens.access_token,).await- .map_err(|e| GithubError::Internal(e.into()))?;+ .map_err(Into::into)?;if result == GithubGrantLinkOutcome::IdentityMissing {// OAuth verified this GitHub account; migrate every sharer// only after storing the grant on the current identity.@@ -557,7 +557,7 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> Github&tokens.access_token,).await- .map_err(|e| GithubError::Internal(e.into()))?;+ .map_err(Into::into)?;if result == GithubGrantLinkOutcome::IdentityMissing {return Err(GithubError::ReauthenticationRequired);}@@ -591,7 +591,7 @@ impl<R: GithubRepo, U: GithubOauth, F: Auth, E: GithubPullRequestService> Github&tokens.access_token,).await- .map_err(|e| GithubError::Internal(e.into()))?;+ .map_err(Into::into)?;if result == GithubGrantLinkOutcome::IdentityMissing {return Err(GithubError::ReauthenticationRequired);diff --git a/crates/github/src/domain/service/link_test.rs b/crates/github/src/domain/service/link_test.rsindex 8ab5b37fc0..042cbac04b 100644--- a/crates/github/src/domain/service/link_test.rs+++ b/crates/github/src/domain/service/link_test.rs@@ -35,6 +35,8 @@ use crate::domain::{ports::{Auth, GithubLinkService, GithubOauth, GithubRepo},};+use upstream_error::{UpstreamError, UpstreamFailure};+use super::{GithubLinkConfig, GithubLinkServiceImpl};#[derive(Clone)]@@ -206,6 +208,7 @@ struct StubGithubOauth {#[derive(Default)]struct StubGithubOauthState {token_is_expired: bool,+ token_check_failure: Option<UpstreamFailure>,validated_tokens: Vec<String>,pull_request_detail_calls: Vec<PullRequestDetailCall>,pull_request_details: Option<GithubPullRequestDetails>,@@ -249,6 +252,11 @@ impl StubGithubOauth {}}+ fn fail_token_check(self, failure: UpstreamFailure) -> Self {+ self.state.lock().unwrap().token_check_failure = Some(failure);+ self+ }+fn validated_tokens(&self) -> Vec<String> {self.state.lock().unwrap().validated_tokens.clone()}@@ -354,7 +362,7 @@ fn default_pull_request_details() -> GithubPullRequestDetails {}impl GithubOauth for StubGithubOauth {- type Err = anyhow::Error;+ type Err = GithubError;fn construct_oauth_url(&self,@@ -394,6 +402,9 @@ impl GithubOauth for StubGithubOauth {async fn is_access_token_expired(&self, access_token: &str) -> Result<bool, Self::Err> {let mut state = self.state.lock().unwrap();state.validated_tokens.push(access_token.to_string());+ if let Some(failure) = state.token_check_failure {+ return Err(github_upstream("GitHub", failure));+ }Ok(state.token_is_expired)}@@ -413,7 +424,7 @@ impl GithubOauth for StubGithubOauth {});if let Some(error) = &state.pull_request_detail_error {- return Err(anyhow::anyhow!(error.clone()));+ return Err(GithubError::Internal(anyhow::anyhow!(error.clone())));}Ok(state@@ -431,7 +442,7 @@ impl GithubOauth for StubGithubOauth {let mut state = self.state.lock().unwrap();state.merge_settings_calls += 1;if let Some(error) = &state.merge_settings_error {- return Err(anyhow::anyhow!(error.clone()));+ return Err(GithubError::Internal(anyhow::anyhow!(error.clone())));}Ok(state.merge_settings@@ -461,7 +472,7 @@ impl GithubOauth for StubGithubOauth {state.merge_outcome.clone()- .ok_or_else(|| anyhow::anyhow!("GitHub unreachable"))+ .ok_or_else(|| GithubError::Internal(anyhow::anyhow!("GitHub unreachable")))}async fn set_pull_request_draft(@@ -479,7 +490,7 @@ impl GithubOauth for StubGithubOauth {state.draft_outcome.clone()- .ok_or_else(|| anyhow::anyhow!("GitHub unreachable"))+ .ok_or_else(|| GithubError::Internal(anyhow::anyhow!("GitHub unreachable")))}async fn set_pull_request_state(@@ -497,7 +508,7 @@ impl GithubOauth for StubGithubOauth {state.state_outcome.clone()- .ok_or_else(|| anyhow::anyhow!("GitHub unreachable"))+ .ok_or_else(|| GithubError::Internal(anyhow::anyhow!("GitHub unreachable")))}async fn get_pull_request_mergeability(@@ -530,6 +541,7 @@ struct StubAuthState {link_user_calls: u32,delete_user_link_calls: u32,access_token_error: Option<String>,+ access_token_failure: Option<UpstreamFailure>,missing_access_token: bool,missing_identity: Option<uuid::Uuid>,link_error: Option<String>,@@ -537,6 +549,10 @@ struct StubAuthState {link_barrier: Option<(Arc<tokio::sync::Notify>, Arc<tokio::sync::Notify>)>,}+fn github_upstream(service: &'static str, failure: UpstreamFailure) -> GithubError {+ GithubError::Upstream(UpstreamError { service, failure })+}+impl StubAuth {fn new(access_token: &str) -> Self {Self {@@ -550,6 +566,11 @@ impl StubAuth {self}+ fn fail_access_token_upstream(self, failure: UpstreamFailure) -> Self {+ self.state.lock().unwrap().access_token_failure = Some(failure);+ self+ }+fn link_user_calls(&self) -> u32 {self.state.lock().unwrap().link_user_calls}@@ -560,7 +581,7 @@ impl StubAuth {}impl Auth for StubAuth {- type Err = anyhow::Error;+ type Err = GithubError;async fn link_user(&self,@@ -582,7 +603,7 @@ impl Auth for StubAuth {return Ok(GithubGrantLinkOutcome::IdentityMissing);}if let Some(error) = &state.link_error {- return Err(anyhow::anyhow!(error.clone()));+ return Err(GithubError::Internal(anyhow::anyhow!(error.clone())));}Ok(GithubGrantLinkOutcome::Linked)}@@ -603,7 +624,10 @@ impl Auth for StubAuth {_github_user_id: &str,) -> Result<Option<GithubAccessToken>, Self::Err> {if let Some(error) = &self.state.lock().unwrap().access_token_error {- return Err(anyhow::anyhow!(error.clone()));+ return Err(GithubError::Internal(anyhow::anyhow!(error.clone())));+ }+ if let Some(failure) = self.state.lock().unwrap().access_token_failure {+ return Err(github_upstream("The sign-in service", failure));}if self.state.lock().unwrap().missing_access_token {return Ok(None);diff --git a/crates/github/src/domain/service/link_test/lookup.rs b/crates/github/src/domain/service/link_test/lookup.rsindex f5a44fbed6..a8210caa98 100644--- a/crates/github/src/domain/service/link_test/lookup.rs+++ b/crates/github/src/domain/service/link_test/lookup.rs@@ -69,3 +69,43 @@ async fn get_user_link_status_surfaces_token_lookup_failures() {assert!(matches!(result, Err(GithubError::Internal(_))));assert!(oauth.validated_tokens().is_empty());}++#[tokio::test]+async fn get_user_link_status_surfaces_sign_in_service_outages() {+ let user_id = test_user_id();+ let service = service(+ StubGithubRepo::linked(test_link(&user_id)),+ StubGithubOauth::new(false),+ StubAuth::new("valid-token").fail_access_token_upstream(UpstreamFailure::Unavailable),+ );++ let result = service.get_user_link_status(&user_id).await;++ assert!(matches!(+ result,+ Err(GithubError::Upstream(UpstreamError {+ failure: UpstreamFailure::Unavailable,+ ..+ }))+ ));+}++#[tokio::test]+async fn get_user_link_status_surfaces_github_rate_limits() {+ let user_id = test_user_id();+ let rate_limited = UpstreamFailure::RateLimited {+ retry_after: Some(std::time::Duration::from_secs(60)),+ };+ let service = service(+ StubGithubRepo::linked(test_link(&user_id)),+ StubGithubOauth::new(false).fail_token_check(rate_limited),+ StubAuth::new("valid-token"),+ );++ let result = service.get_user_link_status(&user_id).await;++ assert!(matches!(+ result,+ Err(GithubError::Upstream(UpstreamError { failure, .. })) if failure == rate_limited+ ));+}diff --git a/crates/github/src/inbound.rs b/crates/github/src/inbound.rsindex 286e61552e..65bedb5c71 100644--- a/crates/github/src/inbound.rs+++ b/crates/github/src/inbound.rs@@ -62,6 +62,7 @@ impl axum::response::IntoResponse for crate::domain::models::GithubError {StatusCode::UNPROCESSABLE_ENTITY,"too many pull requests requested".into(),),+ crate::domain::models::GithubError::Upstream(error) => return error.into_response(),};(diff --git a/crates/github/src/outbound/github_auth_client.rs b/crates/github/src/outbound/github_auth_client.rsindex 9844ad31d6..9a673e7b81 100644--- a/crates/github/src/outbound/github_auth_client.rs+++ b/crates/github/src/outbound/github_auth_client.rs@@ -9,7 +9,7 @@ use redis::AsyncCommands;use redis::aio::MultiplexedConnection;use crate::domain::{- models::{GithubAccessToken, GithubGrantLinkOutcome, GithubLink},+ models::{GithubAccessToken, GithubError, GithubGrantLinkOutcome, GithubLink},ports::Auth,};@@ -48,16 +48,28 @@ impl GithubAuthImpl {&self,fusionauth_user_id: &uuid::Uuid,github_user_id: &str,- ) -> Result<(), anyhow::Error> {+ ) -> Result<(), GithubError> {let key = github_access_token_key!(fusionauth_user_id, github_user_id);let mut conn = self.conn.clone();- conn.del::<&str, ()>(&key).await?;+ conn.del::<&str, ()>(&key).await.map_err(cache_error)?;Ok(())}}+/// FusionAuth outages are upstream failures; anything else is ours.+fn fusionauth_error(error: FusionAuthClientError) -> GithubError {+ match error.upstream() {+ Some(upstream) => GithubError::Upstream(upstream),+ None => GithubError::Internal(error.into()),+ }+}++fn cache_error(error: redis::RedisError) -> GithubError {+ GithubError::Internal(error.into())+}+impl Auth for GithubAuthImpl {- type Err = anyhow::Error;+ type Err = GithubError;#[tracing::instrument(skip(self, username, access_token), err)]async fn link_user(@@ -84,7 +96,8 @@ impl Auth for GithubAuthImpl {self.conn.clone().set_ex::<&str, &str, ()>(&key, access_token, TTL_SECONDS)- .await?;+ .await+ .map_err(cache_error)?;} else {self.clear_access_token_cache(fusionauth_user_id, github_user_id).await?;@@ -102,8 +115,14 @@ impl Auth for GithubAuthImpl {let key = github_access_token_key!(fusionauth_user_id, github_user_id);let mut conn = self.conn.clone();- if let Some(access_token) = conn.get::<&str, Option<String>>(&key).await? {- conn.expire::<&str, ()>(&key, TTL_SECONDS as i64).await?;+ if let Some(access_token) = conn+ .get::<&str, Option<String>>(&key)+ .await+ .map_err(cache_error)?+ {+ conn.expire::<&str, ()>(&key, TTL_SECONDS as i64)+ .await+ .map_err(cache_error)?;return Ok(Some(GithubAccessToken::new(access_token)));}@@ -120,7 +139,7 @@ impl Auth for GithubAuthImpl {FusionAuthClientError::UserDoesNotExist| FusionAuthClientError::NoIdentityProviderFound,) => return Ok(None),- Err(error) => return Err(error.into()),+ Err(error) => return Err(fusionauth_error(error)),};// An identity may retain another account's grant for its remaining sharers.@@ -140,10 +159,12 @@ impl Auth for GithubAuthImpl {.arg(TTL_SECONDS).arg("NX").query_async(&mut conn)- .await?;+ .await+ .map_err(cache_error)?;let token = if inserted.is_none() {conn.get::<&str, Option<String>>(&key)- .await?+ .await+ .map_err(cache_error)?.unwrap_or_else(|| link.token.clone())} else {link.token.clone()@@ -178,7 +199,7 @@ impl Auth for GithubAuthImpl {.await?;Ok(())}- Err(e) => Err(e.into()),+ Err(e) => Err(fusionauth_error(e)),}}}@@ -192,7 +213,7 @@ async fn refresh_grant(github_user_id: &str,username: &str,access_token: &str,-) -> Result<GithubGrantLinkOutcome, anyhow::Error> {+) -> Result<GithubGrantLinkOutcome, GithubError> {// Read FusionAuth directly: a cached token can outlive its identity.let links = match fusionauth_client.get_links(&fusionauth_user_id.to_string(), Some(idp_id.to_string()))@@ -203,7 +224,7 @@ async fn refresh_grant(return Ok(GithubGrantLinkOutcome::IdentityMissing);}Err(FusionAuthClientError::NoIdentityProviderFound) => Vec::new(),- Err(error) => return Err(error.into()),+ Err(error) => return Err(fusionauth_error(error)),};let existing = links.iter()@@ -223,7 +244,8 @@ async fn refresh_grant(if existing.is_some() {fusionauth_client.unlink_user(&fusionauth_user_id.to_string(), idp_id, github_user_id)- .await?;+ .await+ .map_err(fusionauth_error)?;}if let Err(error) = fusionauth_client.link_user(request(access_token.to_string()))@@ -239,7 +261,7 @@ async fn refresh_grant(tracing::error!(error=?rollback_error, "unable to restore github grant after failed refresh");}}- return Err(error.into());+ return Err(fusionauth_error(error));}Ok(GithubGrantLinkOutcome::Linked)}diff --git a/crates/github/src/outbound/github_oauth_client.rs b/crates/github/src/outbound/github_oauth_client.rsindex b95545170e..d7b9a95835 100644--- a/crates/github/src/outbound/github_oauth_client.rs+++ b/crates/github/src/outbound/github_oauth_client.rs@@ -1,13 +1,19 @@//! Github Oauth Client implementation of the [`GithubOauth`] port.-use std::time::Duration;+use std::time::{Duration, SystemTime, UNIX_EPOCH};++use reqwest::{+ StatusCode,+ header::{HeaderMap, RETRY_AFTER},+};+use upstream_error::{UpstreamError, UpstreamFailure};use super::pull_request_metadata::fetch_pull_request_metadata;use crate::domain::{models::{- GithubDraftOutcome, GithubExchangeTokenResponse, GithubMergeMethod, GithubMergeOutcome,- GithubMergeRejection, GithubPullRequestDetails, GithubPullRequestMerge,+ GithubDraftOutcome, GithubError, GithubExchangeTokenResponse, GithubMergeMethod,+ GithubMergeOutcome, GithubMergeRejection, GithubPullRequestDetails, GithubPullRequestMerge,GithubPullRequestMergeability, GithubPullRequestMergeabilityEntry, GithubPullRequestNumber,GithubPullRequestState, GithubPullRequestUpdateRejection, GithubRepositoryMergeSettings,GithubStateOutcome, GithubUserInfo,@@ -19,9 +25,87 @@ use crate::domain::{mod test;const GITHUB_API_BASE_URL: &str = "https://api.github.com";+/// GitHub's name in error messages users see.+const GITHUB: &str = "GitHub";const USER_AGENT: &str = "Macro-Auth-Service";const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);+fn upstream(failure: UpstreamFailure) -> GithubError {+ GithubError::Upstream(UpstreamError {+ service: GITHUB,+ failure,+ })+}++/// A request that got no usable answer from GitHub.+fn transport_error(error: reqwest::Error) -> GithubError {+ tracing::error!(error=?error, "request to github failed");+ upstream(UpstreamFailure::from_transport(&error))+}++/// GitHub throttling. A 429, or a 403 that carries `Retry-After`, exhausts+/// `x-ratelimit-remaining` (primary limit), or says it hit a secondary rate+/// limit, which GitHub may send without either header; any other 403 is a+/// real refusal.+/// See https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api+fn throttling(status: StatusCode, headers: &HeaderMap, body: &str) -> Option<UpstreamFailure> {+ let quota_exhausted = headers+ .get("x-ratelimit-remaining")+ .is_some_and(|remaining| remaining == "0");+ let throttled = status == StatusCode::TOO_MANY_REQUESTS+ || (status == StatusCode::FORBIDDEN+ && (headers.contains_key(RETRY_AFTER)+ || quota_exhausted+ || body.to_ascii_lowercase().contains("secondary rate limit")));+ // `x-ratelimit-reset` is when the primary quota refills, so it only says+ // when to retry once that quota is spent.+ throttled.then(|| UpstreamFailure::RateLimited {+ retry_after: upstream_error::retry_after(headers)+ .or_else(|| quota_exhausted.then(|| rate_limit_reset(headers)).flatten()),+ })+}++/// How long until `x-ratelimit-reset`, a Unix time in seconds.+fn rate_limit_reset(headers: &HeaderMap) -> Option<Duration> {+ let reset: u64 = headers+ .get("x-ratelimit-reset")?+ .to_str()+ .ok()?+ .parse()+ .ok()?;+ let now = SystemTime::now().duration_since(UNIX_EPOCH).ok()?.as_secs();+ Some(Duration::from_secs(reset.saturating_sub(now)))+}++/// A response the call did not expect: GitHub throttling or failing is+/// upstream; anything else is ours to look into.+async fn unexpected_response(response: reqwest::Response, context: &str) -> GithubError {+ let status = response.status();+ let headers = response.headers().clone();+ let body = response_text(response).await;+ unexpected(status, &headers, body, context)+}++/// [`unexpected_response`] for a call that already read the body.+fn unexpected(status: StatusCode, headers: &HeaderMap, body: String, context: &str) -> GithubError {+ let failure = throttling(status, headers, &body)+ .or_else(|| UpstreamFailure::from_status(status, headers));+ match failure {+ Some(failure) => {+ tracing::error!(status=%status, body=%body, context, "github is failing");+ upstream(failure)+ }+ None => GithubError::Internal(anyhow::anyhow!("{context} (status {status}): {body}")),+ }+}++async fn response_text(response: reqwest::Response) -> String {+ response+ .text()+ .await+ .unwrap_or_else(|_| "unknown error".to_string())+}+/// Github email information from /user/emails endpoint#[derive(Debug, serde::Deserialize)]struct GithubEmail {@@ -190,7 +274,7 @@ impl GithubOauthImpl {access_token: &str,query: &str,variables: serde_json::Value,- ) -> anyhow::Result<GraphqlResponse<T>> {+ ) -> Result<GraphqlResponse<T>, GithubError> {let response = self.client.post(format!("{}/graphql", self.api_base_url()))@@ -199,18 +283,14 @@ impl GithubOauthImpl {.json(&serde_json::json!({ "query": query, "variables": variables })).timeout(REQUEST_TIMEOUT).send()- .await?;+ .await+ .map_err(transport_error)?;- let status = response.status();- if !status.is_success() {- let error_body = response- .text()- .await- .unwrap_or_else(|_| "unknown error".to_string());- anyhow::bail!("GitHub GraphQL request failed (status {status}): {error_body}");+ if !response.status().is_success() {+ return Err(unexpected_response(response, "GitHub GraphQL request failed").await);}- Ok(response.json().await?)+ response.json().await.map_err(transport_error)}fn api_base_url(&self) -> &str {@@ -232,7 +312,7 @@ impl GithubOauthImpl {}impl GithubOauth for GithubOauthImpl {- type Err = anyhow::Error;+ type Err = GithubError;#[tracing::instrument(skip(self), err)]fn construct_oauth_url(@@ -282,19 +362,19 @@ impl GithubOauth for GithubOauthImpl {.json(&token_request).timeout(Duration::from_secs(30)).send()- .await?;-- let status = response.status();+ .await+ .map_err(transport_error)?;- if !status.is_success() {- let error_body = response- .text()- .await- .unwrap_or_else(|_| "unknown error".to_string());- anyhow::bail!("token exchange failed {}", error_body)+ if !response.status().is_success() {+ return Err(unexpected_response(response, "token exchange failed").await);}- let token_response: GithubExchangeTokenResponse = response.json().await?;+ // GitHub answers a bad or expired code with a 200 and an error body,+ // which fails to decode here. That is the code's fault, not GitHub's.+ let token_response: GithubExchangeTokenResponse = response+ .json()+ .await+ .map_err(|error| GithubError::Internal(error.into()))?;Ok(token_response)}@@ -304,31 +384,31 @@ impl GithubOauth for GithubOauthImpl {// Get basic user infolet user_response = self.client- .get("https://api.github.com/user")+ .get(format!("{}/user", self.api_base_url())).header("Authorization", format!("Bearer {}", access_token)).header("User-Agent", "Macro-Auth-Service").timeout(Duration::from_secs(30)).send()- .await?;+ .await+ .map_err(transport_error)?;let status = user_response.status();if !status.is_success() {- let error_body = user_response- .text()- .await- .unwrap_or_else(|_| "unknown error".to_string());-// Check for 401 Unauthorized - token expired or invalidif status.as_u16() == 401 {+ let error_body = user_response+ .text()+ .await+ .unwrap_or_else(|_| "unknown error".to_string());tracing::warn!(error_body=%error_body, "GitHub token expired or invalid");- anyhow::bail!("token expired")+ return Err(GithubError::Internal(anyhow::anyhow!("token expired")));}- anyhow::bail!("failed to get user info {}", error_body)+ return Err(unexpected_response(user_response, "failed to get user info").await);}- let mut user_info: GithubUserInfo = user_response.json().await?;+ let mut user_info: GithubUserInfo = user_response.json().await.map_err(transport_error)?;// If email is not public, try to fetch from emails endpoint (optional)if user_info.email.is_none() {@@ -336,7 +416,7 @@ impl GithubOauth for GithubOauthImpl {match self.client- .get("https://api.github.com/user/emails")+ .get(format!("{}/user/emails", self.api_base_url())).header("Authorization", format!("Bearer {}", access_token)).header("User-Agent", "Macro-Auth-Service").timeout(Duration::from_secs(30))@@ -389,12 +469,13 @@ impl GithubOauth for GithubOauthImpl {async fn is_access_token_expired(&self, access_token: &str) -> Result<bool, Self::Err> {let user_response = self.client- .get("https://api.github.com/user")+ .get(format!("{}/user", self.api_base_url())).header("Authorization", format!("Bearer {}", access_token)).header("User-Agent", "Macro-Auth-Service").timeout(Duration::from_secs(30)).send()- .await?;+ .await+ .map_err(transport_error)?;let status = user_response.status();@@ -403,11 +484,9 @@ impl GithubOauth for GithubOauthImpl {}if !status.is_success() {- let error_body = user_response- .text()- .await- .unwrap_or_else(|_| "unknown error".to_string());- anyhow::bail!("failed to validate access token {}", error_body);+ return Err(+ unexpected_response(user_response, "failed to validate access token").await,+ );}Ok(false)@@ -421,7 +500,9 @@ impl GithubOauth for GithubOauthImpl {repo: &str,number: u64,) -> Result<GithubPullRequestDetails, Self::Err> {- fetch_pull_request_metadata(&self.client, access_token, owner, repo, number).await+ fetch_pull_request_metadata(&self.client, access_token, owner, repo, number)+ .await+ .map_err(GithubError::Internal)}#[tracing::instrument(skip(self, access_token), err)]@@ -440,18 +521,15 @@ impl GithubOauth for GithubOauthImpl {.header("X-GitHub-Api-Version", "2022-11-28").timeout(REQUEST_TIMEOUT).send()- .await?;+ .await+ .map_err(transport_error)?;- let status = response.status();- if !status.is_success() {- let error_body = response- .text()- .await- .unwrap_or_else(|_| "unknown error".to_string());- anyhow::bail!("failed to read repository settings (status {status}): {error_body}");+ if !response.status().is_success() {+ return Err(unexpected_response(response, "failed to read repository settings").await);}- let settings: RepositorySettingsResponse = response.json().await?;+ let settings: RepositorySettingsResponse =+ response.json().await.map_err(transport_error)?;Ok(GithubRepositoryMergeSettings {allow_merge_commit: settings.allow_merge_commit,@@ -493,17 +571,30 @@ impl GithubOauth for GithubOauthImpl {.json(&MergeRequest { merge_method }).timeout(REQUEST_TIMEOUT).send()- .await?;+ .await+ .map_err(transport_error)?;let status = response.status();if status.is_success() {- let merged: MergeResponse = response.json().await?;+ let merged: MergeResponse = response.json().await.map_err(transport_error)?;return Ok(GithubMergeOutcome::Merged(GithubPullRequestMerge {sha: merged.sha,message: merged.message,}));}+ let headers = response.headers().clone();+ let body = response_text(response).await;+ // A throttled merge is a 403 too; it must not read as a refusal.+ if throttling(status, &headers, &body).is_some() {+ return Err(unexpected(+ status,+ &headers,+ body,+ "failed to merge pull request",+ ));+ }+// The statuses GitHub documents for this endpoint, each a decision// about the pull request rather than a failure to reach GitHub.let rejection = match status {@@ -513,16 +604,15 @@ impl GithubOauth for GithubOauthImpl {reqwest::StatusCode::FORBIDDEN => GithubMergeRejection::Forbidden,reqwest::StatusCode::UNPROCESSABLE_ENTITY => GithubMergeRejection::Invalid,_ => {- let error_body = response- .text()- .await- .unwrap_or_else(|_| "unknown error".to_string());- anyhow::bail!("failed to merge pull request (status {status}): {error_body}");+ return Err(unexpected(+ status,+ &headers,+ body,+ "failed to merge pull request",+ ));}};- let message = response- .json::<GithubErrorBody>()- .await+ let message = serde_json::from_str::<GithubErrorBody>(&body).map(|body| body.message).unwrap_or_else(|_| "GitHub declined to merge the pull request.".to_string());@@ -616,16 +706,29 @@ impl GithubOauth for GithubOauthImpl {.json(&PullRequestState { state }).timeout(REQUEST_TIMEOUT).send()- .await?;+ .await+ .map_err(transport_error)?;let status = response.status();if status.is_success() {- let changed: PullRequestState = response.json().await?;+ let changed: PullRequestState = response.json().await.map_err(transport_error)?;return Ok(GithubStateOutcome::Changed {state: changed.state,});}+ let headers = response.headers().clone();+ let body = response_text(response).await;+ // A throttled change is a 403 too; it must not read as a refusal.+ if throttling(status, &headers, &body).is_some() {+ return Err(unexpected(+ status,+ &headers,+ body,+ "failed to change pull request state",+ ));+ }+// The statuses GitHub documents for this endpoint, each a decision// about the pull request rather than a failure to reach GitHub.let rejection = match status {@@ -633,18 +736,15 @@ impl GithubOauth for GithubOauthImpl {reqwest::StatusCode::NOT_FOUND => GithubPullRequestUpdateRejection::NotFound,reqwest::StatusCode::UNPROCESSABLE_ENTITY => GithubPullRequestUpdateRejection::Invalid,_ => {- let error_body = response- .text()- .await- .unwrap_or_else(|_| "unknown error".to_string());- anyhow::bail!(- "failed to change pull request state (status {status}): {error_body}"- );+ return Err(unexpected(+ status,+ &headers,+ body,+ "failed to change pull request state",+ ));}};- let message = response- .json::<GithubErrorBody>()- .await+ let message = serde_json::from_str::<GithubErrorBody>(&body).map(GithubErrorBody::reason).unwrap_or_else(|_| "GitHub declined to change the pull request.".to_string());@@ -676,7 +776,9 @@ impl GithubOauth for GithubOauthImpl {.next().map(|error| error.message).unwrap_or_else(|| "no data".to_string());- anyhow::bail!("failed to read pull request mergeability: {message}");+ return Err(GithubError::Internal(anyhow::anyhow!(+ "failed to read pull request mergeability: {message}"+ )));};Ok(pull_requestsdiff --git a/crates/github/src/outbound/github_oauth_client/test.rs b/crates/github/src/outbound/github_oauth_client/test.rsindex 5e5c4eaae5..4f3bbde3a8 100644--- a/crates/github/src/outbound/github_oauth_client/test.rs+++ b/crates/github/src/outbound/github_oauth_client/test.rs@@ -189,7 +189,7 @@ async fn merge_pull_request_maps_head_change_and_access_statuses() {}#[tokio::test]-async fn merge_pull_request_treats_unexpected_status_as_error() {+async fn merge_pull_request_treats_a_failing_github_as_unavailable() {let server = MockServer::start(MockResponse {status: "502 Bad Gateway",body: "upstream down".to_string(),@@ -201,10 +201,172 @@ async fn merge_pull_request_treats_unexpected_status_as_error() {.await.unwrap_err();- assert!(error.to_string().contains("502"));+ assert_eq!(upstream_failure(error), Some(UpstreamFailure::Unavailable));server.finish();}+fn upstream_failure(error: GithubError) -> Option<UpstreamFailure> {+ match error {+ GithubError::Upstream(UpstreamError { service, failure }) => {+ assert_eq!(service, "GitHub");+ Some(failure)+ }+ _ => None,+ }+}++async fn github_answering(response: wiremock::ResponseTemplate) -> wiremock::MockServer {+ let server = wiremock::MockServer::start().await;+ wiremock::Mock::given(wiremock::matchers::any())+ .respond_with(response)+ .mount(&server)+ .await;+ server+}++#[tokio::test]+async fn token_check_reports_an_expired_token() {+ let server = github_answering(wiremock::ResponseTemplate::new(401)).await;+ let client = GithubOauthImpl::with_api_base_url(server.uri());++ assert!(client.is_access_token_expired("token").await.unwrap());+}++#[tokio::test]+async fn token_check_treats_an_exhausted_rate_limit_as_throttling() {+ let reset = SystemTime::now()+ .duration_since(UNIX_EPOCH)+ .unwrap()+ .as_secs()+ + 120;+ let server = github_answering(+ wiremock::ResponseTemplate::new(403)+ .insert_header("x-ratelimit-remaining", "0")+ .insert_header("x-ratelimit-reset", reset.to_string().as_str())+ .set_body_string(r#"{"message":"API rate limit exceeded"}"#),+ )+ .await;+ let client = GithubOauthImpl::with_api_base_url(server.uri());++ let error = client.is_access_token_expired("token").await.unwrap_err();++ let Some(UpstreamFailure::RateLimited {+ retry_after: Some(retry_after),+ }) = upstream_failure(error)+ else {+ panic!("expected a rate limit with a delay");+ };+ assert!(+ (110..=120).contains(&retry_after.as_secs()),+ "{retry_after:?}"+ );+}++#[tokio::test]+async fn token_check_treats_a_secondary_rate_limit_as_throttling() {+ let server =+ github_answering(wiremock::ResponseTemplate::new(403).insert_header("retry-after", "30"))+ .await;+ let client = GithubOauthImpl::with_api_base_url(server.uri());++ let error = client.is_access_token_expired("token").await.unwrap_err();++ assert_eq!(+ upstream_failure(error),+ Some(UpstreamFailure::RateLimited {+ retry_after: Some(Duration::from_secs(30)),+ })+ );+}++#[tokio::test]+async fn a_secondary_rate_limit_without_headers_is_throttling() {+ // Primary quota left; its reset is an hour away and says nothing about+ // when the secondary limit lifts.+ let reset = SystemTime::now()+ .duration_since(UNIX_EPOCH)+ .unwrap()+ .as_secs()+ + 3600;+ let secondary = || {+ wiremock::ResponseTemplate::new(403)+ .insert_header("x-ratelimit-remaining", "4999")+ .insert_header("x-ratelimit-reset", reset.to_string().as_str())+ .set_body_string(+ r#"{"message":"You have exceeded a secondary rate limit. Please wait a few minutes before you try again."}"#,+ )+ };+ let throttled = Some(UpstreamFailure::RateLimited { retry_after: None });++ let server = github_answering(secondary()).await;+ let client = GithubOauthImpl::with_api_base_url(server.uri());+ let error = client.is_access_token_expired("token").await.unwrap_err();+ assert_eq!(upstream_failure(error), throttled);++ let error = client+ .merge_pull_request("token", "macro", "app", 7, GithubMergeMethod::Merge)+ .await+ .unwrap_err();+ assert_eq!(upstream_failure(error), throttled);+}++#[tokio::test]+async fn token_check_treats_a_failing_github_as_unavailable() {+ let server = github_answering(wiremock::ResponseTemplate::new(503)).await;+ let client = GithubOauthImpl::with_api_base_url(server.uri());++ let error = client.is_access_token_expired("token").await.unwrap_err();++ assert_eq!(upstream_failure(error), Some(UpstreamFailure::Unavailable));+}++#[tokio::test]+async fn token_check_treats_an_unreachable_github_as_unavailable() {+ let address = TcpListener::bind("127.0.0.1:0")+ .unwrap()+ .local_addr()+ .unwrap();+ let client = GithubOauthImpl::with_api_base_url(format!("http://{address}"));++ let error = client.is_access_token_expired("token").await.unwrap_err();++ assert_eq!(upstream_failure(error), Some(UpstreamFailure::Unavailable));+}++#[tokio::test]+async fn token_check_keeps_a_plain_refusal_ours() {+ let server = github_answering(+ wiremock::ResponseTemplate::new(403).set_body_string(r#"{"message":"Forbidden"}"#),+ )+ .await;+ let client = GithubOauthImpl::with_api_base_url(server.uri());++ let error = client.is_access_token_expired("token").await.unwrap_err();++ assert!(matches!(error, GithubError::Internal(_)));+}++#[tokio::test]+async fn merge_pull_request_does_not_read_a_rate_limit_as_a_refusal() {+ let server = github_answering(+ wiremock::ResponseTemplate::new(403)+ .insert_header("x-ratelimit-remaining", "0")+ .set_body_string(r#"{"message":"API rate limit exceeded"}"#),+ )+ .await;+ let client = GithubOauthImpl::with_api_base_url(server.uri());++ let error = client+ .merge_pull_request("token", "macro", "app", 7, GithubMergeMethod::Merge)+ .await+ .unwrap_err();++ assert_eq!(+ upstream_failure(error),+ Some(UpstreamFailure::RateLimited { retry_after: None })+ );+}+#[tokio::test]async fn get_repository_merge_settings_reads_allowed_methods() {let server = MockServer::start(MockResponse {@@ -490,7 +652,7 @@ async fn set_pull_request_state_reports_the_validation_reason() {}#[tokio::test]-async fn set_pull_request_state_treats_unexpected_status_as_error() {+async fn set_pull_request_state_reports_a_failing_github_as_unavailable() {let server = MockServer::start(MockResponse {status: "502 Bad Gateway",body: "upstream down".to_string(),@@ -502,10 +664,31 @@ async fn set_pull_request_state_treats_unexpected_status_as_error() {.await.unwrap_err();- assert!(error.to_string().contains("502"));+ assert_eq!(upstream_failure(error), Some(UpstreamFailure::Unavailable));server.finish();}+#[tokio::test]+async fn set_pull_request_state_does_not_read_a_rate_limit_as_a_refusal() {+ let server = github_answering(+ wiremock::ResponseTemplate::new(403)+ .insert_header("x-ratelimit-remaining", "0")+ .set_body_string(r#"{"message":"API rate limit exceeded"}"#),+ )+ .await;+ let client = GithubOauthImpl::with_api_base_url(server.uri());++ let error = client+ .set_pull_request_state("token", "macro", "app", 7, GithubPullRequestState::Closed)+ .await+ .unwrap_err();++ assert_eq!(+ upstream_failure(error),+ Some(UpstreamFailure::RateLimited { retry_after: None })+ );+}+#[tokio::test]async fn get_pull_request_mergeability_batches_and_skips_hidden_pull_requests() {let server = MockServer::start(MockResponse {diff --git a/crates/upstream_error/AGENTS.md b/crates/upstream_error/AGENTS.mdnew file mode 100644index 0000000000..7f435d649a--- /dev/null+++ b/crates/upstream_error/AGENTS.md@@ -0,0 +1,63 @@+# upstream_error++## What it is++Shared classification for failures of third-party services (GitHub, FusionAuth,+...) so they reach clients as 502/503 with a message the user can act on,+instead of a 500 that blames our code.++## How it works++An outbound adapter turns a provider's 5xx, 429, unreachable host, or+undecodable body into an `UpstreamError { service, failure }` with+`UpstreamFailure::from_status` / `from_transport`. The domain error carries it+in its own `Upstream` variant. The inbound adapter answers it with+`UpstreamError::into_response`: 503 (plus `Retry-After` when throttled) or 502,+with an `UpstreamErrorResponse { message, code }` body.++## Start here++- `src/lib.rs`: the classification and what counts as an upstream failure.+- `src/response.rs`: status codes, body, and `Retry-After`.++## Scope++- Owns: classifying provider HTTP and transport failures; their HTTP answer.+- Not here: connection state the user can fix (not linked, grant revoked,+ reauthentication). Each domain models it as its own 4xx error.++## Boundaries++- Depends on: `http`; `reqwest` and `axum` behind features of the same name.+- Used by: `crates/fusionauth`, `crates/github`, `services/authentication_service`.++## Recipes++- Classify a new provider: in its outbound adapter, map non-success responses+ through `UpstreamFailure::from_status` (after handling the 4xx the endpoint+ documents) and request failures through `from_transport`; give the domain+ error an `Upstream(UpstreamError)` variant; answer it with `into_response()`+ in the inbound adapter's exhaustive match.++## Checks++- `cargo test -p upstream_error`+- `rules/ast-grep/rust-fusionauth-classify-transport.yml` (run by `just check`)+ fails a FusionAuth `send()`/`json()`/`text()` mapped to anything but+ `FusionAuthClientError::transport`.++## Gotchas++- Domain code may hold `UpstreamError`, but `src/domain/**` must not see axum:+ depend with `default-features = false` there and enable `axum` only in the+ crate or feature that owns the inbound adapter.+- Providers signal throttling differently. GitHub's primary limit is a 403 with+ `x-ratelimit-remaining: 0`, which `from_status` cannot see; the GitHub+ adapter detects it before falling back to `from_status`.+- `service` is user-facing ("GitHub", "Google", "The sign-in service"), not a+ log tag. A crate that calls several providers names each one: `fusionauth`+ reports Google token refresh failures as "Google", not as itself.++## Related++- Skills: `cloud-storage-hexagonal-architecture` · Docs: `docs/development/rust.md`diff --git a/crates/upstream_error/CLAUDE.md b/crates/upstream_error/CLAUDE.mdnew file mode 120000index 0000000000..47dc3e3d86--- /dev/null+++ b/crates/upstream_error/CLAUDE.md@@ -0,0 +1 @@+AGENTS.md\ No newline at end of filediff --git a/crates/upstream_error/Cargo.toml b/crates/upstream_error/Cargo.tomlnew file mode 100644index 0000000000..9a52c98a69--- /dev/null+++ b/crates/upstream_error/Cargo.toml@@ -0,0 +1,26 @@+[package]+edition = "2024"+name = "upstream_error"+publish = false+version = "0.1.0"++[features]+default = ["axum", "reqwest"]+axum = ["dep:axum", "dep:serde", "dep:utoipa"]+reqwest = ["dep:reqwest"]++[dependencies]+axum = { workspace = true, optional = true }+http = { workspace = true }+reqwest = { workspace = true, optional = true }+serde = { workspace = true, optional = true }+thiserror = { workspace = true }+utoipa = { workspace = true, optional = true }+workspace-hack = { version = "0.1", path = "../workspace-hack" }++[dev-dependencies]+http-body-util = { workspace = true }+reqwest = { workspace = true }+serde_json = { workspace = true }+tokio = { workspace = true }+wiremock = { workspace = true }diff --git a/crates/upstream_error/src/lib.rs b/crates/upstream_error/src/lib.rsnew file mode 100644index 0000000000..d7ee6e71fa--- /dev/null+++ b/crates/upstream_error/src/lib.rs@@ -0,0 +1,91 @@+#![deny(missing_docs)]+//! Failures of third-party services, classified once and answered once.+//!+//! An outbound adapter that calls a provider (GitHub, FusionAuth, ...) turns+//! the provider's outage, throttling, or unusable answer into an+//! [`UpstreamError`] instead of a generic internal error. With the `axum`+//! feature, inbound adapters answer it with its `IntoResponse`: 503 when+//! retrying later can help, 502 when the provider answered with something we+//! could not use. Never 500, which says the bug is ours.+//!+//! Connection state (not linked, grant revoked, token expired) is not an+//! upstream failure. The user can fix it, so each domain models it as its own+//! error and answers with a 4xx that says what to do.++use std::time::Duration;++#[cfg(feature = "axum")]+mod response;+#[cfg(feature = "axum")]+pub use response::{UpstreamErrorCode, UpstreamErrorResponse};++#[cfg(all(test, feature = "axum", feature = "reqwest"))]+mod test;++/// Why a call to a third-party service failed, when the failure is the+/// provider's rather than the caller's or ours.+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]+pub enum UpstreamFailure {+ /// The provider could not be reached, timed out, or answered with a 5xx.+ #[error("is unavailable")]+ Unavailable,+ /// The provider is throttling our requests.+ #[error("is rate limiting requests")]+ RateLimited {+ /// How long the provider asked us to wait, when it said.+ retry_after: Option<Duration>,+ },+ /// The provider answered with something we could not interpret.+ #[error("returned an unexpected response")]+ InvalidResponse,+}++impl UpstreamFailure {+ /// The failure a provider's HTTP status means. `None` when the status is+ /// not the provider's failure: success, and every 4xx except 429, which+ /// the caller has to interpret for its own endpoint.+ pub fn from_status(status: http::StatusCode, headers: &http::HeaderMap) -> Option<Self> {+ if status == http::StatusCode::TOO_MANY_REQUESTS {+ return Some(Self::RateLimited {+ retry_after: retry_after(headers),+ });+ }+ status.is_server_error().then_some(Self::Unavailable)+ }++ /// The failure a request that got no usable answer means: a body that+ /// could not be decoded is [`Self::InvalidResponse`]; a connection,+ /// timeout, or interrupted body is [`Self::Unavailable`].+ #[cfg(feature = "reqwest")]+ pub fn from_transport(error: &reqwest::Error) -> Self {+ if error.is_decode() {+ Self::InvalidResponse+ } else {+ Self::Unavailable+ }+ }+}++/// The `Retry-After` header in its delay-seconds form. The HTTP-date form is+/// ignored: the providers we call send seconds.+pub fn retry_after(headers: &http::HeaderMap) -> Option<Duration> {+ let seconds = headers+ .get(http::header::RETRY_AFTER)?+ .to_str()+ .ok()?+ .trim()+ .parse()+ .ok()?;+ Some(Duration::from_secs(seconds))+}++/// A third-party failure, with the provider named the way users know it.+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]+#[error("{service} {failure}")]+pub struct UpstreamError {+ /// The provider's user-facing name, e.g. `"GitHub"`. It starts the+ /// message clients show, so name what the user recognises.+ pub service: &'static str,+ /// What went wrong.+ pub failure: UpstreamFailure,+}diff --git a/crates/upstream_error/src/response.rs b/crates/upstream_error/src/response.rsnew file mode 100644index 0000000000..a211246d7b--- /dev/null+++ b/crates/upstream_error/src/response.rs@@ -0,0 +1,79 @@+use axum::{+ Json,+ http::{HeaderValue, StatusCode, header::RETRY_AFTER},+ response::{IntoResponse, Response},+};++use crate::{UpstreamError, UpstreamFailure};++/// What kind of upstream failure a 502 or 503 reports, for clients that+/// branch on it.+#[derive(+ Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, utoipa::ToSchema,+)]+#[serde(rename_all = "snake_case")]+pub enum UpstreamErrorCode {+ /// The provider is down or timed out. Retry later.+ UpstreamUnavailable,+ /// The provider is throttling requests. Retry after `Retry-After`.+ UpstreamRateLimited,+ /// The provider answered with something unusable. Retry later.+ UpstreamInvalidResponse,+}++/// The body of a 502 or 503 caused by a third-party service.+#[derive(Debug, serde::Serialize, serde::Deserialize, utoipa::ToSchema)]+pub struct UpstreamErrorResponse {+ /// What failed and what to do about it, written for the user.+ pub message: String,+ /// The kind of failure.+ pub code: UpstreamErrorCode,+}++impl UpstreamError {+ /// 503 when retrying later can help, 502 when the provider's answer was+ /// unusable.+ pub fn status_code(&self) -> StatusCode {+ match self.failure {+ UpstreamFailure::Unavailable | UpstreamFailure::RateLimited { .. } => {+ StatusCode::SERVICE_UNAVAILABLE+ }+ UpstreamFailure::InvalidResponse => StatusCode::BAD_GATEWAY,+ }+ }++ /// The response body: a message the client can show, and its code.+ pub fn body(&self) -> UpstreamErrorResponse {+ let service = self.service;+ let (code, message) = match self.failure {+ UpstreamFailure::Unavailable => (+ UpstreamErrorCode::UpstreamUnavailable,+ format!("{service} is temporarily unavailable. Try again in a few minutes."),+ ),+ UpstreamFailure::RateLimited { .. } => (+ UpstreamErrorCode::UpstreamRateLimited,+ format!("{service} is limiting requests right now. Try again shortly."),+ ),+ UpstreamFailure::InvalidResponse => (+ UpstreamErrorCode::UpstreamInvalidResponse,+ format!("{service} returned an unexpected response. Try again in a few minutes."),+ ),+ };+ UpstreamErrorResponse { message, code }+ }+}++impl IntoResponse for UpstreamError {+ fn into_response(self) -> Response {+ let mut response = (self.status_code(), Json(self.body())).into_response();+ if let UpstreamFailure::RateLimited {+ retry_after: Some(retry_after),+ } = self.failure+ {+ response+ .headers_mut()+ .insert(RETRY_AFTER, HeaderValue::from(retry_after.as_secs()));+ }+ response+ }+}diff --git a/crates/upstream_error/src/test.rs b/crates/upstream_error/src/test.rsnew file mode 100644index 0000000000..1ebafdd0f6--- /dev/null+++ b/crates/upstream_error/src/test.rs@@ -0,0 +1,166 @@+use std::time::Duration;++use http::{HeaderMap, HeaderValue, StatusCode};+use wiremock::{Mock, MockServer, ResponseTemplate, matchers::method};++use super::*;++#[test]+fn server_errors_are_unavailable() {+ for status in [+ StatusCode::INTERNAL_SERVER_ERROR,+ StatusCode::BAD_GATEWAY,+ StatusCode::SERVICE_UNAVAILABLE,+ StatusCode::GATEWAY_TIMEOUT,+ ] {+ assert_eq!(+ UpstreamFailure::from_status(status, &HeaderMap::new()),+ Some(UpstreamFailure::Unavailable),+ "{status}"+ );+ }+}++#[test]+fn too_many_requests_is_rate_limited_with_the_advertised_delay() {+ let mut headers = HeaderMap::new();+ headers.insert(http::header::RETRY_AFTER, HeaderValue::from_static("30"));++ assert_eq!(+ UpstreamFailure::from_status(StatusCode::TOO_MANY_REQUESTS, &headers),+ Some(UpstreamFailure::RateLimited {+ retry_after: Some(Duration::from_secs(30))+ })+ );+}++#[test]+fn too_many_requests_without_a_delay_is_still_rate_limited() {+ assert_eq!(+ UpstreamFailure::from_status(StatusCode::TOO_MANY_REQUESTS, &HeaderMap::new()),+ Some(UpstreamFailure::RateLimited { retry_after: None })+ );+}++#[test]+fn success_and_client_errors_are_not_upstream_failures() {+ for status in [+ StatusCode::OK,+ StatusCode::BAD_REQUEST,+ StatusCode::UNAUTHORIZED,+ StatusCode::FORBIDDEN,+ StatusCode::NOT_FOUND,+ ] {+ assert_eq!(+ UpstreamFailure::from_status(status, &HeaderMap::new()),+ None,+ "{status}"+ );+ }+}++#[test]+fn http_date_retry_after_is_ignored() {+ let mut headers = HeaderMap::new();+ headers.insert(+ http::header::RETRY_AFTER,+ HeaderValue::from_static("Wed, 21 Oct 2015 07:28:00 GMT"),+ );++ assert_eq!(retry_after(&headers), None);+}++#[tokio::test]+async fn an_unreachable_provider_is_unavailable() {+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();+ let url = format!("http://{}", listener.local_addr().unwrap());+ drop(listener);++ let error = reqwest::get(url).await.unwrap_err();++ assert_eq!(+ UpstreamFailure::from_transport(&error),+ UpstreamFailure::Unavailable+ );+}++#[tokio::test]+async fn an_undecodable_body_is_an_invalid_response() {+ let server = MockServer::start().await;+ Mock::given(method("GET"))+ .respond_with(ResponseTemplate::new(200).set_body_string("<html>not json</html>"))+ .mount(&server)+ .await;++ let error = reqwest::get(server.uri())+ .await+ .unwrap()+ .json::<serde_json::Value>()+ .await+ .unwrap_err();++ assert_eq!(+ UpstreamFailure::from_transport(&error),+ UpstreamFailure::InvalidResponse+ );+}++#[tokio::test]+async fn unavailable_answers_503_with_an_actionable_message() {+ let response = axum::response::IntoResponse::into_response(UpstreamError {+ service: "GitHub",+ failure: UpstreamFailure::Unavailable,+ });++ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);+ assert!(response.headers().get(http::header::RETRY_AFTER).is_none());+ assert_eq!(+ body_json(response).await,+ serde_json::json!({+ "message": "GitHub is temporarily unavailable. Try again in a few minutes.",+ "code": "upstream_unavailable",+ })+ );+}++#[tokio::test]+async fn rate_limited_answers_503_with_retry_after() {+ let response = axum::response::IntoResponse::into_response(UpstreamError {+ service: "GitHub",+ failure: UpstreamFailure::RateLimited {+ retry_after: Some(Duration::from_secs(42)),+ },+ });++ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);+ assert_eq!(response.headers()[http::header::RETRY_AFTER], "42");+ assert_eq!(+ body_json(response).await["code"],+ serde_json::json!("upstream_rate_limited")+ );+}++#[tokio::test]+async fn invalid_response_answers_502() {+ let response = axum::response::IntoResponse::into_response(UpstreamError {+ service: "The sign-in service",+ failure: UpstreamFailure::InvalidResponse,+ });++ assert_eq!(response.status(), StatusCode::BAD_GATEWAY);+ assert_eq!(+ body_json(response).await,+ serde_json::json!({+ "message": "The sign-in service returned an unexpected response. Try again in a few minutes.",+ "code": "upstream_invalid_response",+ })+ );+}++async fn body_json(response: axum::response::Response) -> serde_json::Value {+ let bytes = http_body_util::BodyExt::collect(response.into_body())+ .await+ .unwrap()+ .to_bytes();+ serde_json::from_slice(&bytes).unwrap()+}diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/README.md b/packages/sdk-docs/reference/generated/auth/types.gen/README.mdindex 4c7f3ee5f5..8a389de768 100644--- a/packages/sdk-docs/reference/generated/auth/types.gen/README.md+++ b/packages/sdk-docs/reference/generated/auth/types.gen/README.md@@ -619,6 +619,8 @@- [UpdateAiBillingOverageResponses](type-aliases/UpdateAiBillingOverageResponses.md)- [UpdateAutoReloadRequest](type-aliases/UpdateAutoReloadRequest.md)- [UpdateOverageRequest](type-aliases/UpdateOverageRequest.md)+- [UpstreamErrorCode](type-aliases/UpstreamErrorCode.md)+- [UpstreamErrorResponse](type-aliases/UpstreamErrorResponse.md)- [UsageSnapshot](type-aliases/UsageSnapshot.md)- [UserLanguage](type-aliases/UserLanguage.md)- [UserLinkResponse](type-aliases/UserLinkResponse.md)diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/CheckGithubLinkStatusErrors.md b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/CheckGithubLinkStatusErrors.mdindex 7fbd4ab8e0..367a271268 100644--- a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/CheckGithubLinkStatusErrors.md+++ b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/CheckGithubLinkStatusErrors.md@@ -1,6 +1,6 @@# Type Alias: CheckGithubLinkStatusErrors-> **CheckGithubLinkStatusErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); \}+> **CheckGithubLinkStatusErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); `502`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); `503`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); \}## Properties@@ -10,3 +10,5 @@| <a id="property-404"></a> `404` | [`ErrorResponse`](ErrorResponse.md) || <a id="property-428"></a> `428` | [`ErrorResponse`](ErrorResponse.md) || <a id="property-500"></a> `500` | [`ErrorResponse`](ErrorResponse.md) |+| <a id="property-502"></a> `502` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) |+| <a id="property-503"></a> `503` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) |diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/DeleteGithubLinkErrors.md b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/DeleteGithubLinkErrors.mdindex c6636ceda6..a2fc4af759 100644--- a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/DeleteGithubLinkErrors.md+++ b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/DeleteGithubLinkErrors.md@@ -1,6 +1,6 @@# Type Alias: DeleteGithubLinkErrors-> **DeleteGithubLinkErrors** = \{ `400`: [`ErrorResponse`](ErrorResponse.md); `401`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); \}+> **DeleteGithubLinkErrors** = \{ `400`: [`ErrorResponse`](ErrorResponse.md); `401`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); `502`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); `503`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); \}## Properties@@ -9,3 +9,5 @@| <a id="property-400"></a> `400` | [`ErrorResponse`](ErrorResponse.md) || <a id="property-401"></a> `401` | [`ErrorResponse`](ErrorResponse.md) || <a id="property-500"></a> `500` | [`ErrorResponse`](ErrorResponse.md) |+| <a id="property-502"></a> `502` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) |+| <a id="property-503"></a> `503` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) |diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/EnrichGithubPullRequestsErrors.md b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/EnrichGithubPullRequestsErrors.mdindex 722651fb76..d5c3b285b3 100644--- a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/EnrichGithubPullRequestsErrors.md+++ b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/EnrichGithubPullRequestsErrors.md@@ -1,6 +1,6 @@# Type Alias: EnrichGithubPullRequestsErrors-> **EnrichGithubPullRequestsErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); \}+> **EnrichGithubPullRequestsErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); `502`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); `503`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); \}## Properties@@ -10,3 +10,5 @@| <a id="property-404"></a> `404` | [`ErrorResponse`](ErrorResponse.md) || <a id="property-428"></a> `428` | [`ErrorResponse`](ErrorResponse.md) || <a id="property-500"></a> `500` | [`ErrorResponse`](ErrorResponse.md) |+| <a id="property-502"></a> `502` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) |+| <a id="property-503"></a> `503` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) |diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/GetGithubPullRequestMergeabilityErrors.md b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/GetGithubPullRequestMergeabilityErrors.mdindex 4149e2880f..76ae81c6a1 100644--- a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/GetGithubPullRequestMergeabilityErrors.md+++ b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/GetGithubPullRequestMergeabilityErrors.md@@ -1,6 +1,6 @@# Type Alias: GetGithubPullRequestMergeabilityErrors-> **GetGithubPullRequestMergeabilityErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `422`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); \}+> **GetGithubPullRequestMergeabilityErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `422`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); `502`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); `503`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); \}## Properties@@ -11,3 +11,5 @@| <a id="property-422"></a> `422` | [`ErrorResponse`](ErrorResponse.md) | Too many pull requests requested || <a id="property-428"></a> `428` | [`ErrorResponse`](ErrorResponse.md) | - || <a id="property-500"></a> `500` | [`ErrorResponse`](ErrorResponse.md) | - |+| <a id="property-502"></a> `502` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) | - |+| <a id="property-503"></a> `503` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) | - |diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/InitGithubLinkErrors.md b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/InitGithubLinkErrors.mdindex 45f587291a..02b4d5ca6e 100644--- a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/InitGithubLinkErrors.md+++ b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/InitGithubLinkErrors.md@@ -1,6 +1,6 @@# Type Alias: InitGithubLinkErrors-> **InitGithubLinkErrors** = \{ `400`: [`ErrorResponse`](ErrorResponse.md); `401`: [`ErrorResponse`](ErrorResponse.md); `429`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); \}+> **InitGithubLinkErrors** = \{ `400`: [`ErrorResponse`](ErrorResponse.md); `401`: [`ErrorResponse`](ErrorResponse.md); `429`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); `502`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); `503`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); \}## Properties@@ -10,3 +10,5 @@| <a id="property-401"></a> `401` | [`ErrorResponse`](ErrorResponse.md) || <a id="property-429"></a> `429` | [`ErrorResponse`](ErrorResponse.md) || <a id="property-500"></a> `500` | [`ErrorResponse`](ErrorResponse.md) |+| <a id="property-502"></a> `502` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) |+| <a id="property-503"></a> `503` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) |diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/MergeGithubPullRequestErrors.md b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/MergeGithubPullRequestErrors.mdindex 2fb7a471ba..03aa8655d8 100644--- a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/MergeGithubPullRequestErrors.md+++ b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/MergeGithubPullRequestErrors.md@@ -1,6 +1,6 @@# Type Alias: MergeGithubPullRequestErrors-> **MergeGithubPullRequestErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `403`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `409`: [`ErrorResponse`](ErrorResponse.md); `422`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); \}+> **MergeGithubPullRequestErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `403`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `409`: [`ErrorResponse`](ErrorResponse.md); `422`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); `502`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); `503`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); \}## Properties@@ -13,3 +13,5 @@| <a id="property-422"></a> `422` | [`ErrorResponse`](ErrorResponse.md) | - || <a id="property-428"></a> `428` | [`ErrorResponse`](ErrorResponse.md) | - || <a id="property-500"></a> `500` | [`ErrorResponse`](ErrorResponse.md) | - |+| <a id="property-502"></a> `502` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) | - |+| <a id="property-503"></a> `503` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) | - |diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/SetGithubPullRequestDraftErrors.md b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/SetGithubPullRequestDraftErrors.mdindex 53b585ebcd..023e5f7bee 100644--- a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/SetGithubPullRequestDraftErrors.md+++ b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/SetGithubPullRequestDraftErrors.md@@ -1,6 +1,6 @@# Type Alias: SetGithubPullRequestDraftErrors-> **SetGithubPullRequestDraftErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `403`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `422`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); \}+> **SetGithubPullRequestDraftErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `403`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `422`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); `502`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); `503`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); \}## Properties@@ -12,3 +12,5 @@| <a id="property-422"></a> `422` | [`ErrorResponse`](ErrorResponse.md) | GitHub declined the change || <a id="property-428"></a> `428` | [`ErrorResponse`](ErrorResponse.md) | - || <a id="property-500"></a> `500` | [`ErrorResponse`](ErrorResponse.md) | - |+| <a id="property-502"></a> `502` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) | - |+| <a id="property-503"></a> `503` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) | - |diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/SetGithubPullRequestStateErrors.md b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/SetGithubPullRequestStateErrors.mdindex 51494c6153..ae0ea05a1c 100644--- a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/SetGithubPullRequestStateErrors.md+++ b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/SetGithubPullRequestStateErrors.md@@ -1,6 +1,6 @@# Type Alias: SetGithubPullRequestStateErrors-> **SetGithubPullRequestStateErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `403`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `422`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); \}+> **SetGithubPullRequestStateErrors** = \{ `401`: [`ErrorResponse`](ErrorResponse.md); `403`: [`ErrorResponse`](ErrorResponse.md); `404`: [`ErrorResponse`](ErrorResponse.md); `422`: [`ErrorResponse`](ErrorResponse.md); `428`: [`ErrorResponse`](ErrorResponse.md); `500`: [`ErrorResponse`](ErrorResponse.md); `502`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); `503`: [`UpstreamErrorResponse`](UpstreamErrorResponse.md); \}## Properties@@ -12,3 +12,5 @@| <a id="property-422"></a> `422` | [`ErrorResponse`](ErrorResponse.md) | GitHub declined the change || <a id="property-428"></a> `428` | [`ErrorResponse`](ErrorResponse.md) | - || <a id="property-500"></a> `500` | [`ErrorResponse`](ErrorResponse.md) | - |+| <a id="property-502"></a> `502` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) | - |+| <a id="property-503"></a> `503` | [`UpstreamErrorResponse`](UpstreamErrorResponse.md) | - |diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/UpstreamErrorCode.md b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/UpstreamErrorCode.mdnew file mode 100644index 0000000000..c60a7cd545--- /dev/null+++ b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/UpstreamErrorCode.md@@ -0,0 +1,6 @@+# Type Alias: UpstreamErrorCode++> **UpstreamErrorCode** = `"upstream_unavailable"` \| `"upstream_rate_limited"` \| `"upstream_invalid_response"`++What kind of upstream failure a 502 or 503 reports, for clients that+branch on it.diff --git a/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/UpstreamErrorResponse.md b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/UpstreamErrorResponse.mdnew file mode 100644index 0000000000..951bf0e509--- /dev/null+++ b/packages/sdk-docs/reference/generated/auth/types.gen/type-aliases/UpstreamErrorResponse.md@@ -0,0 +1,12 @@+# Type Alias: UpstreamErrorResponse++> **UpstreamErrorResponse** = \{ `code`: [`UpstreamErrorCode`](UpstreamErrorCode.md); `message`: `string`; \}++The body of a 502 or 503 caused by a third-party service.++## Properties++| Property | Type | Description |+| ------ | ------ | ------ |+| <a id="property-code"></a> `code` | [`UpstreamErrorCode`](UpstreamErrorCode.md) | The kind of failure. |+| <a id="property-message"></a> `message` | `string` | What failed and what to do about it, written for the user. |diff --git a/packages/sdk/generated/auth/index.ts b/packages/sdk/generated/auth/index.tsindex 0d510d1b7f..c901d279de 100644--- a/packages/sdk/generated/auth/index.ts+++ b/packages/sdk/generated/auth/index.ts@@ -1,4 +1,4 @@// This file is auto-generated by @hey-api/openapi-tsexport { type Options, Sdk } from './sdk.gen';-export type { AbGroup, AgentApiKey, AgentApiKeyProvider, AiBillingErrorBody, AppleLoginData, AppleLoginError, AppleLoginErrors, AppleLoginRequest, AppleLoginResponse, AppleLoginResponses, AutoReloadDefaults, AutoReloadMonthlyBudget, AutoReloadSnapshot, CancelCodexLoginData, CancelCodexLoginResponse, CancelCodexLoginResponses, ChangePlanData, ChangePlanError, ChangePlanErrors, ChangePlanRequest, ChangePlanResponse, ChangePlanResponse2, ChangePlanResponses, CheckGithubLinkStatusData, CheckGithubLinkStatusError, CheckGithubLinkStatusErrors, CheckGithubLinkStatusResponse, CheckGithubLinkStatusResponses, CheckGmailLinkStatusData, CheckGmailLinkStatusError, CheckGmailLinkStatusErrors, CheckGmailLinkStatusResponse, CheckGmailLinkStatusResponses, CheckoutSessionMetadata, CheckoutSessionV2Response, ClientOptions, CodexConfigRequest, CodexConnectionStatus, CodexEnvironment, CodexEnvironmentRepository, CodexLoginPoll, CodexLoginStart, CodexLoginState, ConfigureCodexData, ConfigureCodexResponse, ConfigureCodexResponses, CreateAccountMergeRequest, CreateAiCreditCheckoutData, CreateAiCreditCheckoutError, CreateAiCreditCheckoutErrors, CreateAiCreditCheckoutResponse, CreateAiCreditCheckoutResponses, CreateCheckoutSessionV2Data, CreateCheckoutSessionV2Error, CreateCheckoutSessionV2Errors, CreateCheckoutSessionV2Request, CreateCheckoutSessionV2Response, CreateCheckoutSessionV2Responses, CreatedTeamInviteLinkResponse, CreateGtmInviteLinkData, CreateGtmInviteLinkError, CreateGtmInviteLinkErrors, CreateGtmInviteLinkRequest, CreateGtmInviteLinkResponse, CreateGtmInviteLinkResponses, CreateInProgressLinkData, CreateInProgressLinkError, CreateInProgressLinkErrors, CreateInProgressLinkResponse, CreateInProgressLinkResponse2, CreateInProgressLinkResponses, CreateMergeRequestData, CreateMergeRequestError, CreateMergeRequestErrors, CreateMergeRequestResponse, CreateMergeRequestResponses, CreatePortalSessionData, CreatePortalSessionError, CreatePortalSessionErrors, CreatePortalSessionRequest, CreatePortalSessionResponse, CreatePortalSessionResponses, CreateTeamData, CreateTeamError, CreateTeamErrors, CreateTeamInviteLinkData, CreateTeamInviteLinkError, CreateTeamInviteLinkErrors, CreateTeamInviteLinkResponse, CreateTeamInviteLinkResponses, CreateTeamRequest, CreateTeamResponse, CreateTeamResponses, CreditCheckoutRequestBody, CreditCheckoutResponse, CursorApiKeyStatus, CursorModelOption, CursorModelsResponse, DeleteAgentApiKeyData, DeleteAgentApiKeyError, DeleteAgentApiKeyErrors, DeleteAgentApiKeyResponse, DeleteAgentApiKeyResponses, DeleteCursorApiKeyData, DeleteCursorApiKeyError, DeleteCursorApiKeyErrors, DeleteCursorApiKeyResponse, DeleteCursorApiKeyResponses, DeleteGithubLinkData, DeleteGithubLinkError, DeleteGithubLinkErrors, DeleteGithubLinkResponse, DeleteGithubLinkResponses, DeleteTeamData, DeleteTeamError, DeleteTeamErrors, DeleteTeamInviteHandlerData, DeleteTeamInviteHandlerError, DeleteTeamInviteHandlerErrors, DeleteTeamInviteHandlerResponses, DeleteTeamInviteLinkData, DeleteTeamInviteLinkError, DeleteTeamInviteLinkErrors, DeleteTeamInviteLinkResponse, DeleteTeamInviteLinkResponses, DeleteTeamResponses, DeleteUserData, DeleteUserError, DeleteUserErrors, DeleteUserResponse, DeleteUserResponses, DenyReason, DisconnectCodexData, DisconnectCodexResponse, DisconnectCodexResponses, EmailConnectionProviders, EmailConnectionProvidersData, EmailConnectionProvidersError, EmailConnectionProvidersErrors, EmailConnectionProvidersResponse, EmailConnectionProvidersResponses, EmptyResponse, EnrichedGithubPullRequest, EnrichGithubPullRequestsData, EnrichGithubPullRequestsError, EnrichGithubPullRequestsErrors, EnrichGithubPullRequestsProxyRequest, EnrichGithubPullRequestsResponse, EnrichGithubPullRequestsResponse2, EnrichGithubPullRequestsResponses, ErrorResponse, GenerateEmailLinkData, GenerateEmailLinkError, GenerateEmailLinkErrors, GenerateEmailLinkRequest, GenerateEmailLinkResponse, GenerateEmailLinkResponses, GenericSuccessResponse, GetAgentApiKeyData, GetAgentApiKeyError, GetAgentApiKeyErrors, GetAgentApiKeyResponse, GetAgentApiKeyResponses, GetAiBillingPlansData, GetAiBillingPlansResponse, GetAiBillingPlansResponses, GetAiBillingSummaryData, GetAiBillingSummaryError, GetAiBillingSummaryErrors, GetAiBillingSummaryResponse, GetAiBillingSummaryResponses, GetCodexConnectionData, GetCodexConnectionResponse, GetCodexConnectionResponses, GetCursorApiKeyData, GetCursorApiKeyError, GetCursorApiKeyErrors, GetCursorApiKeyResponse, GetCursorApiKeyResponses, GetGithubPullRequestMergeabilityData, GetGithubPullRequestMergeabilityError, GetGithubPullRequestMergeabilityErrors, GetGithubPullRequestMergeabilityResponse, GetGithubPullRequestMergeabilityResponses, GetGtmInviteOfferData, GetGtmInviteOfferError, GetGtmInviteOfferErrors, GetGtmInviteOfferResponse, GetGtmInviteOfferResponses, GetLegacyUserPermissionsData, GetLegacyUserPermissionsError, GetLegacyUserPermissionsErrors, GetLegacyUserPermissionsResponse, GetLegacyUserPermissionsResponse2, GetLegacyUserPermissionsResponses, GetNamesWithEmailRequestBody, GetPermissionsData, GetPermissionsError, GetPermissionsErrors, GetPermissionsResponse, GetPermissionsResponses, GetProfilePicturesRequestBody, GetReferralCodeData, GetReferralCodeError, GetReferralCodeErrors, GetReferralCodeResponse, GetReferralCodeResponses, GetSubscriptionStatusData, GetSubscriptionStatusError, GetSubscriptionStatusErrors, GetSubscriptionStatusResponse, GetSubscriptionStatusResponses, GetTeamByInviteLinkData, GetTeamByInviteLinkError, GetTeamByInviteLinkErrors, GetTeamByInviteLinkResponse, GetTeamByInviteLinkResponses, GetTeamData, GetTeamError, GetTeamErrors, GetTeamInviteLinkData, GetTeamInviteLinkError, GetTeamInviteLinkErrors, GetTeamInviteLinkResponse, GetTeamInviteLinkResponses, GetTeamInvitesData, GetTeamInvitesError, GetTeamInvitesErrors, GetTeamInvitesResponse, GetTeamInvitesResponses, GetTeamResponse, GetTeamResponses, GetTrialEligibilityData, GetTrialEligibilityError, GetTrialEligibilityErrors, GetTrialEligibilityResponse, GetTrialEligibilityResponses, GetUserInfo, GetUserInfoData, GetUserInfoError, GetUserInfoErrors, GetUserInfoResponse, GetUserInfoResponses, GetUserInvitesData, GetUserInvitesError, GetUserInvitesErrors, GetUserInvitesResponse, GetUserInvitesResponses, GetUserLanguageData, GetUserLanguageError, GetUserLanguageErrors, GetUserLanguageResponse, GetUserLanguageResponses, GetUserLinkExistsData, GetUserLinkExistsError, GetUserLinkExistsErrors, GetUserLinkExistsResponse, GetUserLinkExistsResponses, GetUserNameData, GetUserNameError, GetUserNameErrors, GetUserNameResponse, GetUserNameResponses, GetUserNamesData, GetUserNamesError, GetUserNamesErrors, GetUserNamesResponse, GetUserNamesResponses, GetUserNamesWithEmailData, GetUserNamesWithEmailError, GetUserNamesWithEmailErrors, GetUserNamesWithEmailResponse, GetUserNamesWithEmailResponses, GetUserOrganizationData, GetUserOrganizationError, GetUserOrganizationErrors, GetUserOrganizationResponse, GetUserOrganizationResponses, GetUserPermissionsData, GetUserPermissionsError, GetUserPermissionsErrors, GetUserPermissionsResponse, GetUserPermissionsResponses, GetUserQuotaData, GetUserQuotaError, GetUserQuotaErrors, GetUserQuotaResponse, GetUserQuotaResponses, GetUserTeamsData, GetUserTeamsError, GetUserTeamsErrors, GetUserTeamsResponse, GetUserTeamsResponses, GithubLinkStatusResponse, GithubMergeMethod, GithubPullRequestCheckRun, GithubPullRequestComment, GithubPullRequestLabel, GithubPullRequestMergeability, GithubPullRequestMergeabilityEntry, GithubPullRequestMergeabilityRequest, GithubPullRequestMergeabilityResponse, GithubPullRequestNumber, GithubPullRequestRef, GithubPullRequestReview, GithubPullRequestReviewState, GithubPullRequestState, GithubPullRequestStatus, GithubPullRequestUser, GmailLinkStatusResponse, GtmInviteLink, GtmInviteLinkList, GtmInviteLinkStatus, GtmInviteOffer, GtmInviteOfferStatus, HealthHandlerData, HealthHandlerResponse, HealthHandlerResponses, InitGithubLinkData, InitGithubLinkError, InitGithubLinkErrors, InitGithubLinkResponse, InitGithubLinkResponse2, InitGithubLinkResponses, InitGmailLinkData, InitGmailLinkError, InitGmailLinkErrors, InitGmailLinkResponse, InitGmailLinkResponse2, InitGmailLinkResponses, InitOutlookLinkData, InitOutlookLinkError, InitOutlookLinkErrors, InitOutlookLinkResponse, InitOutlookLinkResponse2, InitOutlookLinkResponses, InviteEntry, InviteToTeamData, InviteToTeamError, InviteToTeamErrors, InviteToTeamRequest, InviteToTeamResponses, JoinTeamByInviteLinkData, JoinTeamByInviteLinkError, JoinTeamByInviteLinkErrors, JoinTeamByInviteLinkResponse, JoinTeamByInviteLinkResponse2, JoinTeamByInviteLinkResponses, JoinTeamData, JoinTeamError, JoinTeamErrors, JoinTeamResponses, Language, LinkShare, ListCodexEnvironmentsData, ListCodexEnvironmentsResponse, ListCodexEnvironmentsResponses, ListCursorModelsData, ListCursorModelsError, ListCursorModelsErrors, ListCursorModelsResponse, ListCursorModelsResponses, ListGtmInviteLinksData, ListGtmInviteLinksError, ListGtmInviteLinksErrors, ListGtmInviteLinksResponse, ListGtmInviteLinksResponses, LogoutData, LogoutResponse, LogoutResponses, MacroApiTokenData, MacroApiTokenError, MacroApiTokenErrors, MacroApiTokenResponse, MacroApiTokenResponse2, MacroApiTokenResponses, MergeGithubPullRequestData, MergeGithubPullRequestError, MergeGithubPullRequestErrors, MergeGithubPullRequestRequest, MergeGithubPullRequestResponse, MergeGithubPullRequestResponse2, MergeGithubPullRequestResponses, Oauth2CallbackData, Oauth2CallbackError, Oauth2CallbackErrors, Oauth2CallbackResponses, OauthRedirectData, OauthRedirectError, OauthRedirectErrors, OauthRedirectResponses, PasswordlessCallbackData, PasswordlessCallbackError, PasswordlessCallbackErrors, PasswordlessCallbackResponse, PasswordlessCallbackResponses, PasswordlessLoginData, PasswordlessLoginError, PasswordlessLoginErrors, PasswordlessLoginResponse, PasswordlessLoginResponses, PasswordlessRequest, PasswordlessStartedResponse, PasswordLoginData, PasswordLoginError, PasswordLoginErrors, PasswordLoginResponse, PasswordLoginResponses, PasswordRequest, PatchTeamCrmSettingsData, PatchTeamCrmSettingsError, PatchTeamCrmSettingsErrors, PatchTeamCrmSettingsRequest, PatchTeamCrmSettingsResponse, PatchTeamCrmSettingsResponse2, PatchTeamCrmSettingsResponses, PatchTeamData, PatchTeamError, PatchTeamErrors, PatchTeamMemberPlanData, PatchTeamMemberPlanError, PatchTeamMemberPlanErrors, PatchTeamMemberPlanRequest, PatchTeamMemberPlanResponse, PatchTeamMemberPlanResponses, PatchTeamRequest, PatchTeamResponses, PatchTeamUserRole, PatchUserGroupData, PatchUserGroupError, PatchUserGroupErrors, PatchUserGroupRequest, PatchUserGroupResponse, PatchUserGroupResponses, PatchUserOnboardingData, PatchUserOnboardingError, PatchUserOnboardingErrors, PatchUserOnboardingRequest, PatchUserOnboardingResponse, PatchUserOnboardingResponses, PatchUserTutorialData, PatchUserTutorialError, PatchUserTutorialErrors, PatchUserTutorialRequest, PatchUserTutorialResponse, PatchUserTutorialResponses, Permission, PlanCatalogEntry, PlanCatalogResponse, PlanTier, PollCodexLoginData, PollCodexLoginResponse, PollCodexLoginResponses, PostGetNamesRequestBody, PostProfilePicturesData, PostProfilePicturesError, PostProfilePicturesErrors, PostProfilePicturesResponse, PostProfilePicturesResponses, ProfilePictureQueryParams, ProfilePictures, PublicGtmInviteLink, PutAgentApiKeyData, PutAgentApiKeyError, PutAgentApiKeyErrors, PutAgentApiKeyRequest, PutAgentApiKeyResponse, PutAgentApiKeyResponses, PutCursorApiKeyData, PutCursorApiKeyError, PutCursorApiKeyErrors, PutCursorApiKeyRequest, PutCursorApiKeyResponse, PutCursorApiKeyResponses, PutCursorDefaultModelData, PutCursorDefaultModelError, PutCursorDefaultModelErrors, PutCursorDefaultModelRequest, PutCursorDefaultModelResponse, PutCursorDefaultModelResponses, PutProfilePictureData, PutProfilePictureError, PutProfilePictureErrors, PutProfilePictureResponse, PutProfilePictureResponses, PutUserLanguageData, PutUserLanguageError, PutUserLanguageErrors, PutUserLanguageResponse, PutUserLanguageResponses, PutUserNameData, PutUserNameError, PutUserNameErrors, PutUserNameQueryParams, PutUserNameResponse, PutUserNameResponses, RedeemGtmInviteLinkData, RedeemGtmInviteLinkError, RedeemGtmInviteLinkErrors, RedeemGtmInviteLinkRequest, RedeemGtmInviteLinkResponse, RedeemGtmInviteLinkResponses, RefreshData, RefreshError, RefreshErrors, RefreshResponse, RefreshResponses, RejectInvitationData, RejectInvitationError, RejectInvitationErrors, RejectInvitationResponses, RemoveUserFromTeamData, RemoveUserFromTeamError, RemoveUserFromTeamErrors, RemoveUserFromTeamResponses, ResendFusionauthVerifyUserEmailData, ResendFusionauthVerifyUserEmailError, ResendFusionauthVerifyUserEmailErrors, ResendFusionauthVerifyUserEmailRequest, ResendFusionauthVerifyUserEmailResponse, ResendFusionauthVerifyUserEmailResponses, ResolveGtmInviteLinkData, ResolveGtmInviteLinkError, ResolveGtmInviteLinkErrors, ResolveGtmInviteLinkResponse, ResolveGtmInviteLinkResponses, RevokeGtmInviteLinkData, RevokeGtmInviteLinkError, RevokeGtmInviteLinkErrors, RevokeGtmInviteLinkResponse, RevokeGtmInviteLinkResponses, SavedAgentApiKey, ScheduledSeatPlan, SeatPlan, SendInviteBody, SendMobileWelcomeEmailData, SendMobileWelcomeEmailError, SendMobileWelcomeEmailErrors, SendMobileWelcomeEmailRequest, SendMobileWelcomeEmailResponse, SendMobileWelcomeEmailResponse2, SendMobileWelcomeEmailResponses, SendReferralCodeData, SendReferralCodeError, SendReferralCodeErrors, SendReferralCodeResponse, SendReferralCodeResponses, SessionCreationData, SessionCreationError, SessionCreationErrors, SessionCreationResponse, SessionCreationResponses, SessionLoginData, SessionLoginError, SessionLoginErrors, SessionLoginResponse, SessionLoginResponses, SetGithubPullRequestDraftData, SetGithubPullRequestDraftError, SetGithubPullRequestDraftErrors, SetGithubPullRequestDraftRequest, SetGithubPullRequestDraftResponse, SetGithubPullRequestDraftResponse2, SetGithubPullRequestDraftResponses, SetGithubPullRequestStateData, SetGithubPullRequestStateError, SetGithubPullRequestStateErrors, SetGithubPullRequestStateRequest, SetGithubPullRequestStateResponse, SetGithubPullRequestStateResponse2, SetGithubPullRequestStateResponses, SsoLoginData, SsoLoginError, SsoLoginErrors, SsoLoginResponses, SsoRequiredResponse, StartCodexLoginData, StartCodexLoginResponse, StartCodexLoginResponses, StripeSessionResponse, SubscriptionStatus, Team, TeamInviteDetails, TeamInviteLinkResponse, TeamInviteLinkTeam, TeamInviteLinkToken, TeamInviteLinkTokenRequest, TeamInvitesResponse, TeamMember, TeamPlan, TeamRole, TeamWithMembers, ToggleAutoJoinDomainResponse, ToggleNonAdminInvitesResponse, ToggleTeamAutoJoinDomainData, ToggleTeamAutoJoinDomainError, ToggleTeamAutoJoinDomainErrors, ToggleTeamAutoJoinDomainResponse, ToggleTeamAutoJoinDomainResponses, ToggleTeamNonAdminInvitesData, ToggleTeamNonAdminInvitesError, ToggleTeamNonAdminInvitesErrors, ToggleTeamNonAdminInvitesResponse, ToggleTeamNonAdminInvitesResponses, TrialEligibility, UpdateAiBillingAutoReloadData, UpdateAiBillingAutoReloadError, UpdateAiBillingAutoReloadErrors, UpdateAiBillingAutoReloadResponse, UpdateAiBillingAutoReloadResponses, UpdateAiBillingOverageData, UpdateAiBillingOverageError, UpdateAiBillingOverageErrors, UpdateAiBillingOverageResponse, UpdateAiBillingOverageResponses, UpdateAutoReloadRequest, UpdateOverageRequest, UsageSnapshot, UserLanguage, UserLinkResponse, UserName, UserNames, UserOrganizationResponse, UserProfilePicture, UserQuota, UserTokensResponse, VerifyEmailLinkData, VerifyEmailLinkError, VerifyEmailLinkErrors, VerifyEmailLinkResponse, VerifyEmailLinkResponses, VerifyFusionauthUserEmailData, VerifyFusionauthUserEmailError, VerifyFusionauthUserEmailErrors, VerifyFusionauthUserEmailResponse, VerifyFusionauthUserEmailResponses, VerifyMergeRequestData, VerifyMergeRequestError, VerifyMergeRequestErrors, VerifyMergeRequestResponse, VerifyMergeRequestResponses } from './types.gen';+export type { AbGroup, AgentApiKey, AgentApiKeyProvider, AiBillingErrorBody, AppleLoginData, AppleLoginError, AppleLoginErrors, AppleLoginRequest, AppleLoginResponse, AppleLoginResponses, AutoReloadDefaults, AutoReloadMonthlyBudget, AutoReloadSnapshot, CancelCodexLoginData, CancelCodexLoginResponse, CancelCodexLoginResponses, ChangePlanData, ChangePlanError, ChangePlanErrors, ChangePlanRequest, ChangePlanResponse, ChangePlanResponse2, ChangePlanResponses, CheckGithubLinkStatusData, CheckGithubLinkStatusError, CheckGithubLinkStatusErrors, CheckGithubLinkStatusResponse, CheckGithubLinkStatusResponses, CheckGmailLinkStatusData, CheckGmailLinkStatusError, CheckGmailLinkStatusErrors, CheckGmailLinkStatusResponse, CheckGmailLinkStatusResponses, CheckoutSessionMetadata, CheckoutSessionV2Response, ClientOptions, CodexConfigRequest, CodexConnectionStatus, CodexEnvironment, CodexEnvironmentRepository, CodexLoginPoll, CodexLoginStart, CodexLoginState, ConfigureCodexData, ConfigureCodexResponse, ConfigureCodexResponses, CreateAccountMergeRequest, CreateAiCreditCheckoutData, CreateAiCreditCheckoutError, CreateAiCreditCheckoutErrors, CreateAiCreditCheckoutResponse, CreateAiCreditCheckoutResponses, CreateCheckoutSessionV2Data, CreateCheckoutSessionV2Error, CreateCheckoutSessionV2Errors, CreateCheckoutSessionV2Request, CreateCheckoutSessionV2Response, CreateCheckoutSessionV2Responses, CreatedTeamInviteLinkResponse, CreateGtmInviteLinkData, CreateGtmInviteLinkError, CreateGtmInviteLinkErrors, CreateGtmInviteLinkRequest, CreateGtmInviteLinkResponse, CreateGtmInviteLinkResponses, CreateInProgressLinkData, CreateInProgressLinkError, CreateInProgressLinkErrors, CreateInProgressLinkResponse, CreateInProgressLinkResponse2, CreateInProgressLinkResponses, CreateMergeRequestData, CreateMergeRequestError, CreateMergeRequestErrors, CreateMergeRequestResponse, CreateMergeRequestResponses, CreatePortalSessionData, CreatePortalSessionError, CreatePortalSessionErrors, CreatePortalSessionRequest, CreatePortalSessionResponse, CreatePortalSessionResponses, CreateTeamData, CreateTeamError, CreateTeamErrors, CreateTeamInviteLinkData, CreateTeamInviteLinkError, CreateTeamInviteLinkErrors, CreateTeamInviteLinkResponse, CreateTeamInviteLinkResponses, CreateTeamRequest, CreateTeamResponse, CreateTeamResponses, CreditCheckoutRequestBody, CreditCheckoutResponse, CursorApiKeyStatus, CursorModelOption, CursorModelsResponse, DeleteAgentApiKeyData, DeleteAgentApiKeyError, DeleteAgentApiKeyErrors, DeleteAgentApiKeyResponse, DeleteAgentApiKeyResponses, DeleteCursorApiKeyData, DeleteCursorApiKeyError, DeleteCursorApiKeyErrors, DeleteCursorApiKeyResponse, DeleteCursorApiKeyResponses, DeleteGithubLinkData, DeleteGithubLinkError, DeleteGithubLinkErrors, DeleteGithubLinkResponse, DeleteGithubLinkResponses, DeleteTeamData, DeleteTeamError, DeleteTeamErrors, DeleteTeamInviteHandlerData, DeleteTeamInviteHandlerError, DeleteTeamInviteHandlerErrors, DeleteTeamInviteHandlerResponses, DeleteTeamInviteLinkData, DeleteTeamInviteLinkError, DeleteTeamInviteLinkErrors, DeleteTeamInviteLinkResponse, DeleteTeamInviteLinkResponses, DeleteTeamResponses, DeleteUserData, DeleteUserError, DeleteUserErrors, DeleteUserResponse, DeleteUserResponses, DenyReason, DisconnectCodexData, DisconnectCodexResponse, DisconnectCodexResponses, EmailConnectionProviders, EmailConnectionProvidersData, EmailConnectionProvidersError, EmailConnectionProvidersErrors, EmailConnectionProvidersResponse, EmailConnectionProvidersResponses, EmptyResponse, EnrichedGithubPullRequest, EnrichGithubPullRequestsData, EnrichGithubPullRequestsError, EnrichGithubPullRequestsErrors, EnrichGithubPullRequestsProxyRequest, EnrichGithubPullRequestsResponse, EnrichGithubPullRequestsResponse2, EnrichGithubPullRequestsResponses, ErrorResponse, GenerateEmailLinkData, GenerateEmailLinkError, GenerateEmailLinkErrors, GenerateEmailLinkRequest, GenerateEmailLinkResponse, GenerateEmailLinkResponses, GenericSuccessResponse, GetAgentApiKeyData, GetAgentApiKeyError, GetAgentApiKeyErrors, GetAgentApiKeyResponse, GetAgentApiKeyResponses, GetAiBillingPlansData, GetAiBillingPlansResponse, GetAiBillingPlansResponses, GetAiBillingSummaryData, GetAiBillingSummaryError, GetAiBillingSummaryErrors, GetAiBillingSummaryResponse, GetAiBillingSummaryResponses, GetCodexConnectionData, GetCodexConnectionResponse, GetCodexConnectionResponses, GetCursorApiKeyData, GetCursorApiKeyError, GetCursorApiKeyErrors, GetCursorApiKeyResponse, GetCursorApiKeyResponses, GetGithubPullRequestMergeabilityData, GetGithubPullRequestMergeabilityError, GetGithubPullRequestMergeabilityErrors, GetGithubPullRequestMergeabilityResponse, GetGithubPullRequestMergeabilityResponses, GetGtmInviteOfferData, GetGtmInviteOfferError, GetGtmInviteOfferErrors, GetGtmInviteOfferResponse, GetGtmInviteOfferResponses, GetLegacyUserPermissionsData, GetLegacyUserPermissionsError, GetLegacyUserPermissionsErrors, GetLegacyUserPermissionsResponse, GetLegacyUserPermissionsResponse2, GetLegacyUserPermissionsResponses, GetNamesWithEmailRequestBody, GetPermissionsData, GetPermissionsError, GetPermissionsErrors, GetPermissionsResponse, GetPermissionsResponses, GetProfilePicturesRequestBody, GetReferralCodeData, GetReferralCodeError, GetReferralCodeErrors, GetReferralCodeResponse, GetReferralCodeResponses, GetSubscriptionStatusData, GetSubscriptionStatusError, GetSubscriptionStatusErrors, GetSubscriptionStatusResponse, GetSubscriptionStatusResponses, GetTeamByInviteLinkData, GetTeamByInviteLinkError, GetTeamByInviteLinkErrors, GetTeamByInviteLinkResponse, GetTeamByInviteLinkResponses, GetTeamData, GetTeamError, GetTeamErrors, GetTeamInviteLinkData, GetTeamInviteLinkError, GetTeamInviteLinkErrors, GetTeamInviteLinkResponse, GetTeamInviteLinkResponses, GetTeamInvitesData, GetTeamInvitesError, GetTeamInvitesErrors, GetTeamInvitesResponse, GetTeamInvitesResponses, GetTeamResponse, GetTeamResponses, GetTrialEligibilityData, GetTrialEligibilityError, GetTrialEligibilityErrors, GetTrialEligibilityResponse, GetTrialEligibilityResponses, GetUserInfo, GetUserInfoData, GetUserInfoError, GetUserInfoErrors, GetUserInfoResponse, GetUserInfoResponses, GetUserInvitesData, GetUserInvitesError, GetUserInvitesErrors, GetUserInvitesResponse, GetUserInvitesResponses, GetUserLanguageData, GetUserLanguageError, GetUserLanguageErrors, GetUserLanguageResponse, GetUserLanguageResponses, GetUserLinkExistsData, GetUserLinkExistsError, GetUserLinkExistsErrors, GetUserLinkExistsResponse, GetUserLinkExistsResponses, GetUserNameData, GetUserNameError, GetUserNameErrors, GetUserNameResponse, GetUserNameResponses, GetUserNamesData, GetUserNamesError, GetUserNamesErrors, GetUserNamesResponse, GetUserNamesResponses, GetUserNamesWithEmailData, GetUserNamesWithEmailError, GetUserNamesWithEmailErrors, GetUserNamesWithEmailResponse, GetUserNamesWithEmailResponses, GetUserOrganizationData, GetUserOrganizationError, GetUserOrganizationErrors, GetUserOrganizationResponse, GetUserOrganizationResponses, GetUserPermissionsData, GetUserPermissionsError, GetUserPermissionsErrors, GetUserPermissionsResponse, GetUserPermissionsResponses, GetUserQuotaData, GetUserQuotaError, GetUserQuotaErrors, GetUserQuotaResponse, GetUserQuotaResponses, GetUserTeamsData, GetUserTeamsError, GetUserTeamsErrors, GetUserTeamsResponse, GetUserTeamsResponses, GithubLinkStatusResponse, GithubMergeMethod, GithubPullRequestCheckRun, GithubPullRequestComment, GithubPullRequestLabel, GithubPullRequestMergeability, GithubPullRequestMergeabilityEntry, GithubPullRequestMergeabilityRequest, GithubPullRequestMergeabilityResponse, GithubPullRequestNumber, GithubPullRequestRef, GithubPullRequestReview, GithubPullRequestReviewState, GithubPullRequestState, GithubPullRequestStatus, GithubPullRequestUser, GmailLinkStatusResponse, GtmInviteLink, GtmInviteLinkList, GtmInviteLinkStatus, GtmInviteOffer, GtmInviteOfferStatus, HealthHandlerData, HealthHandlerResponse, HealthHandlerResponses, InitGithubLinkData, InitGithubLinkError, InitGithubLinkErrors, InitGithubLinkResponse, InitGithubLinkResponse2, InitGithubLinkResponses, InitGmailLinkData, InitGmailLinkError, InitGmailLinkErrors, InitGmailLinkResponse, InitGmailLinkResponse2, InitGmailLinkResponses, InitOutlookLinkData, InitOutlookLinkError, InitOutlookLinkErrors, InitOutlookLinkResponse, InitOutlookLinkResponse2, InitOutlookLinkResponses, InviteEntry, InviteToTeamData, InviteToTeamError, InviteToTeamErrors, InviteToTeamRequest, InviteToTeamResponses, JoinTeamByInviteLinkData, JoinTeamByInviteLinkError, JoinTeamByInviteLinkErrors, JoinTeamByInviteLinkResponse, JoinTeamByInviteLinkResponse2, JoinTeamByInviteLinkResponses, JoinTeamData, JoinTeamError, JoinTeamErrors, JoinTeamResponses, Language, LinkShare, ListCodexEnvironmentsData, ListCodexEnvironmentsResponse, ListCodexEnvironmentsResponses, ListCursorModelsData, ListCursorModelsError, ListCursorModelsErrors, ListCursorModelsResponse, ListCursorModelsResponses, ListGtmInviteLinksData, ListGtmInviteLinksError, ListGtmInviteLinksErrors, ListGtmInviteLinksResponse, ListGtmInviteLinksResponses, LogoutData, LogoutResponse, LogoutResponses, MacroApiTokenData, MacroApiTokenError, MacroApiTokenErrors, MacroApiTokenResponse, MacroApiTokenResponse2, MacroApiTokenResponses, MergeGithubPullRequestData, MergeGithubPullRequestError, MergeGithubPullRequestErrors, MergeGithubPullRequestRequest, MergeGithubPullRequestResponse, MergeGithubPullRequestResponse2, MergeGithubPullRequestResponses, Oauth2CallbackData, Oauth2CallbackError, Oauth2CallbackErrors, Oauth2CallbackResponses, OauthRedirectData, OauthRedirectError, OauthRedirectErrors, OauthRedirectResponses, PasswordlessCallbackData, PasswordlessCallbackError, PasswordlessCallbackErrors, PasswordlessCallbackResponse, PasswordlessCallbackResponses, PasswordlessLoginData, PasswordlessLoginError, PasswordlessLoginErrors, PasswordlessLoginResponse, PasswordlessLoginResponses, PasswordlessRequest, PasswordlessStartedResponse, PasswordLoginData, PasswordLoginError, PasswordLoginErrors, PasswordLoginResponse, PasswordLoginResponses, PasswordRequest, PatchTeamCrmSettingsData, PatchTeamCrmSettingsError, PatchTeamCrmSettingsErrors, PatchTeamCrmSettingsRequest, PatchTeamCrmSettingsResponse, PatchTeamCrmSettingsResponse2, PatchTeamCrmSettingsResponses, PatchTeamData, PatchTeamError, PatchTeamErrors, PatchTeamMemberPlanData, PatchTeamMemberPlanError, PatchTeamMemberPlanErrors, PatchTeamMemberPlanRequest, PatchTeamMemberPlanResponse, PatchTeamMemberPlanResponses, PatchTeamRequest, PatchTeamResponses, PatchTeamUserRole, PatchUserGroupData, PatchUserGroupError, PatchUserGroupErrors, PatchUserGroupRequest, PatchUserGroupResponse, PatchUserGroupResponses, PatchUserOnboardingData, PatchUserOnboardingError, PatchUserOnboardingErrors, PatchUserOnboardingRequest, PatchUserOnboardingResponse, PatchUserOnboardingResponses, PatchUserTutorialData, PatchUserTutorialError, PatchUserTutorialErrors, PatchUserTutorialRequest, PatchUserTutorialResponse, PatchUserTutorialResponses, Permission, PlanCatalogEntry, PlanCatalogResponse, PlanTier, PollCodexLoginData, PollCodexLoginResponse, PollCodexLoginResponses, PostGetNamesRequestBody, PostProfilePicturesData, PostProfilePicturesError, PostProfilePicturesErrors, PostProfilePicturesResponse, PostProfilePicturesResponses, ProfilePictureQueryParams, ProfilePictures, PublicGtmInviteLink, PutAgentApiKeyData, PutAgentApiKeyError, PutAgentApiKeyErrors, PutAgentApiKeyRequest, PutAgentApiKeyResponse, PutAgentApiKeyResponses, PutCursorApiKeyData, PutCursorApiKeyError, PutCursorApiKeyErrors, PutCursorApiKeyRequest, PutCursorApiKeyResponse, PutCursorApiKeyResponses, PutCursorDefaultModelData, PutCursorDefaultModelError, PutCursorDefaultModelErrors, PutCursorDefaultModelRequest, PutCursorDefaultModelResponse, PutCursorDefaultModelResponses, PutProfilePictureData, PutProfilePictureError, PutProfilePictureErrors, PutProfilePictureResponse, PutProfilePictureResponses, PutUserLanguageData, PutUserLanguageError, PutUserLanguageErrors, PutUserLanguageResponse, PutUserLanguageResponses, PutUserNameData, PutUserNameError, PutUserNameErrors, PutUserNameQueryParams, PutUserNameResponse, PutUserNameResponses, RedeemGtmInviteLinkData, RedeemGtmInviteLinkError, RedeemGtmInviteLinkErrors, RedeemGtmInviteLinkRequest, RedeemGtmInviteLinkResponse, RedeemGtmInviteLinkResponses, RefreshData, RefreshError, RefreshErrors, RefreshResponse, RefreshResponses, RejectInvitationData, RejectInvitationError, RejectInvitationErrors, RejectInvitationResponses, RemoveUserFromTeamData, RemoveUserFromTeamError, RemoveUserFromTeamErrors, RemoveUserFromTeamResponses, ResendFusionauthVerifyUserEmailData, ResendFusionauthVerifyUserEmailError, ResendFusionauthVerifyUserEmailErrors, ResendFusionauthVerifyUserEmailRequest, ResendFusionauthVerifyUserEmailResponse, ResendFusionauthVerifyUserEmailResponses, ResolveGtmInviteLinkData, ResolveGtmInviteLinkError, ResolveGtmInviteLinkErrors, ResolveGtmInviteLinkResponse, ResolveGtmInviteLinkResponses, RevokeGtmInviteLinkData, RevokeGtmInviteLinkError, RevokeGtmInviteLinkErrors, RevokeGtmInviteLinkResponse, RevokeGtmInviteLinkResponses, SavedAgentApiKey, ScheduledSeatPlan, SeatPlan, SendInviteBody, SendMobileWelcomeEmailData, SendMobileWelcomeEmailError, SendMobileWelcomeEmailErrors, SendMobileWelcomeEmailRequest, SendMobileWelcomeEmailResponse, SendMobileWelcomeEmailResponse2, SendMobileWelcomeEmailResponses, SendReferralCodeData, SendReferralCodeError, SendReferralCodeErrors, SendReferralCodeResponse, SendReferralCodeResponses, SessionCreationData, SessionCreationError, SessionCreationErrors, SessionCreationResponse, SessionCreationResponses, SessionLoginData, SessionLoginError, SessionLoginErrors, SessionLoginResponse, SessionLoginResponses, SetGithubPullRequestDraftData, SetGithubPullRequestDraftError, SetGithubPullRequestDraftErrors, SetGithubPullRequestDraftRequest, SetGithubPullRequestDraftResponse, SetGithubPullRequestDraftResponse2, SetGithubPullRequestDraftResponses, SetGithubPullRequestStateData, SetGithubPullRequestStateError, SetGithubPullRequestStateErrors, SetGithubPullRequestStateRequest, SetGithubPullRequestStateResponse, SetGithubPullRequestStateResponse2, SetGithubPullRequestStateResponses, SsoLoginData, SsoLoginError, SsoLoginErrors, SsoLoginResponses, SsoRequiredResponse, StartCodexLoginData, StartCodexLoginResponse, StartCodexLoginResponses, StripeSessionResponse, SubscriptionStatus, Team, TeamInviteDetails, TeamInviteLinkResponse, TeamInviteLinkTeam, TeamInviteLinkToken, TeamInviteLinkTokenRequest, TeamInvitesResponse, TeamMember, TeamPlan, TeamRole, TeamWithMembers, ToggleAutoJoinDomainResponse, ToggleNonAdminInvitesResponse, ToggleTeamAutoJoinDomainData, ToggleTeamAutoJoinDomainError, ToggleTeamAutoJoinDomainErrors, ToggleTeamAutoJoinDomainResponse, ToggleTeamAutoJoinDomainResponses, ToggleTeamNonAdminInvitesData, ToggleTeamNonAdminInvitesError, ToggleTeamNonAdminInvitesErrors, ToggleTeamNonAdminInvitesResponse, ToggleTeamNonAdminInvitesResponses, TrialEligibility, UpdateAiBillingAutoReloadData, UpdateAiBillingAutoReloadError, UpdateAiBillingAutoReloadErrors, UpdateAiBillingAutoReloadResponse, UpdateAiBillingAutoReloadResponses, UpdateAiBillingOverageData, UpdateAiBillingOverageError, UpdateAiBillingOverageErrors, UpdateAiBillingOverageResponse, UpdateAiBillingOverageResponses, UpdateAutoReloadRequest, UpdateOverageRequest, UpstreamErrorCode, UpstreamErrorResponse, UsageSnapshot, UserLanguage, UserLinkResponse, UserName, UserNames, UserOrganizationResponse, UserProfilePicture, UserQuota, UserTokensResponse, VerifyEmailLinkData, VerifyEmailLinkError, VerifyEmailLinkErrors, VerifyEmailLinkResponse, VerifyEmailLinkResponses, VerifyFusionauthUserEmailData, VerifyFusionauthUserEmailError, VerifyFusionauthUserEmailErrors, VerifyFusionauthUserEmailResponse, VerifyFusionauthUserEmailResponses, VerifyMergeRequestData, VerifyMergeRequestError, VerifyMergeRequestErrors, VerifyMergeRequestResponse, VerifyMergeRequestResponses } from './types.gen';diff --git a/packages/sdk/generated/auth/types.gen.ts b/packages/sdk/generated/auth/types.gen.tsindex 1ed646fe0a..688e0cae75 100644--- a/packages/sdk/generated/auth/types.gen.ts+++ b/packages/sdk/generated/auth/types.gen.ts@@ -1914,6 +1914,26 @@ export type UpdateOverageRequest = {limitCents?: number;};+/**+ * What kind of upstream failure a 502 or 503 reports, for clients that+ * branch on it.+ */+export type UpstreamErrorCode = 'upstream_unavailable' | 'upstream_rate_limited' | 'upstream_invalid_response';++/**+ * The body of a 502 or 503 caused by a third-party service.+ */+export type UpstreamErrorResponse = {+ /**+ * The kind of failure.+ */+ code: UpstreamErrorCode;+ /**+ * What failed and what to do about it, written for the user.+ */+ message: string;+};+/*** The payer's current-period position, as shown in Billing settings and used* by the gate.@@ -2625,6 +2645,8 @@ export type SetGithubPullRequestDraftErrors = {422: ErrorResponse;428: ErrorResponse;500: ErrorResponse;+ 502: UpstreamErrorResponse;+ 503: UpstreamErrorResponse;};export type SetGithubPullRequestDraftError = SetGithubPullRequestDraftErrors[keyof SetGithubPullRequestDraftErrors];@@ -2647,6 +2669,8 @@ export type EnrichGithubPullRequestsErrors = {404: ErrorResponse;428: ErrorResponse;500: ErrorResponse;+ 502: UpstreamErrorResponse;+ 503: UpstreamErrorResponse;};export type EnrichGithubPullRequestsError = EnrichGithubPullRequestsErrors[keyof EnrichGithubPullRequestsErrors];@@ -2681,6 +2705,8 @@ export type MergeGithubPullRequestErrors = {422: ErrorResponse;428: ErrorResponse;500: ErrorResponse;+ 502: UpstreamErrorResponse;+ 503: UpstreamErrorResponse;};export type MergeGithubPullRequestError = MergeGithubPullRequestErrors[keyof MergeGithubPullRequestErrors];@@ -2710,6 +2736,8 @@ export type GetGithubPullRequestMergeabilityErrors = {422: ErrorResponse;428: ErrorResponse;500: ErrorResponse;+ 502: UpstreamErrorResponse;+ 503: UpstreamErrorResponse;};export type GetGithubPullRequestMergeabilityError = GetGithubPullRequestMergeabilityErrors[keyof GetGithubPullRequestMergeabilityErrors];@@ -2743,6 +2771,8 @@ export type SetGithubPullRequestStateErrors = {422: ErrorResponse;428: ErrorResponse;500: ErrorResponse;+ 502: UpstreamErrorResponse;+ 503: UpstreamErrorResponse;};export type SetGithubPullRequestStateError = SetGithubPullRequestStateErrors[keyof SetGithubPullRequestStateErrors];@@ -3023,6 +3053,8 @@ export type DeleteGithubLinkErrors = {400: ErrorResponse;401: ErrorResponse;500: ErrorResponse;+ 502: UpstreamErrorResponse;+ 503: UpstreamErrorResponse;};export type DeleteGithubLinkError = DeleteGithubLinkErrors[keyof DeleteGithubLinkErrors];@@ -3050,6 +3082,8 @@ export type InitGithubLinkErrors = {401: ErrorResponse;429: ErrorResponse;500: ErrorResponse;+ 502: UpstreamErrorResponse;+ 503: UpstreamErrorResponse;};export type InitGithubLinkError = InitGithubLinkErrors[keyof InitGithubLinkErrors];@@ -3072,6 +3106,8 @@ export type CheckGithubLinkStatusErrors = {404: ErrorResponse;428: ErrorResponse;500: ErrorResponse;+ 502: UpstreamErrorResponse;+ 503: UpstreamErrorResponse;};export type CheckGithubLinkStatusError = CheckGithubLinkStatusErrors[keyof CheckGithubLinkStatusErrors];diff --git a/packages/sdk/specs/auth.json b/packages/sdk/specs/auth.jsonindex 9ffcbb275d..a846226a1c 100644--- a/packages/sdk/specs/auth.json+++ b/packages/sdk/specs/auth.json@@ -1123,6 +1123,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -1192,6 +1212,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -1291,6 +1331,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -1370,6 +1430,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -1459,6 +1539,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -2155,6 +2255,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}},@@ -2202,6 +2322,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -2261,6 +2401,26 @@}}}+ },+ "502": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }+ },+ "503": {+ "description": "",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/UpstreamErrorResponse"+ }+ }+ }}}}@@ -8316,6 +8476,30 @@}}},+ "UpstreamErrorCode": {+ "type": "string",+ "description": "What kind of upstream failure a 502 or 503 reports, for clients that\nbranch on it.",+ "enum": [+ "upstream_unavailable",+ "upstream_rate_limited",+ "upstream_invalid_response"+ ]+ },+ "UpstreamErrorResponse": {+ "type": "object",+ "description": "The body of a 502 or 503 caused by a third-party service.",+ "required": ["message", "code"],+ "properties": {+ "code": {+ "$ref": "#/components/schemas/UpstreamErrorCode",+ "description": "The kind of failure."+ },+ "message": {+ "type": "string",+ "description": "What failed and what to do about it, written for the user."+ }+ }+ },"UsageSnapshot": {"type": "object","description": "The payer's current-period position, as shown in Billing settings and used\nby the gate.",diff --git a/rules/ast-grep/rust-fusionauth-classify-transport.yml b/rules/ast-grep/rust-fusionauth-classify-transport.ymlnew file mode 100644index 0000000000..991cb3d89d--- /dev/null+++ b/rules/ast-grep/rust-fusionauth-classify-transport.yml@@ -0,0 +1,32 @@+id: rust-fusionauth-classify-transport+language: rust+severity: error+message: Map FusionAuth request failures with `FusionAuthClientError::transport`, not a hand-written error.+note: |-+ A FusionAuth request that gets no usable answer (unreachable, timed out,+ undecodable body) is an upstream failure, answered as 503/502. A+ hand-written `Generic` makes it a 500 that blames our code. Map the+ `send()`, `json()`, and `text()` futures with+ `.map_err(FusionAuthClientError::transport)`, and unexpected statuses with+ `FusionAuthClientError::unexpected_response` (see crates/upstream_error).+files:+ - crates/fusionauth/src/**+ignores:+ - '**/test.rs'+ - '**/test/**'+ # These call Google and Microsoft directly, not FusionAuth. Their failures+ # name the provider (`FusionAuthClientError::provider_transport`) or use the+ # provider's own classified error (`MicrosoftRuntimeError`).+ - 'crates/fusionauth/src/google/oauth.rs'+ - 'crates/fusionauth/src/microsoft/oauth.rs'+ - 'crates/fusionauth/src/microsoft/runtime.rs'+rule:+ any:+ - pattern: $REQUEST.send().await.map_err($MAP)+ - pattern: $RESPONSE.json().await.map_err($MAP)+ - pattern: $RESPONSE.json::<$TYPE>().await.map_err($MAP)+ - pattern: $RESPONSE.text().await.map_err($MAP)+constraints:+ MAP:+ not:+ regex: ^FusionAuthClientError::transport$diff --git a/services/authentication_service/AGENTS.md b/services/authentication_service/AGENTS.mdindex 60c14df9d8..50122fc8d2 100644--- a/services/authentication_service/AGENTS.md+++ b/services/authentication_service/AGENTS.md@@ -83,11 +83,17 @@ inputs cannot select an agent, and ordinary user credentials remain unattributed## Gotchas+- Answer third-party failures as 502/503, not 500: match+ `GithubError::Upstream(error)` before a route's 500 catch-all, and answer a+ `FusionAuthClientError` whose `upstream()` is set with that error's+ `into_response()`. Document 502/503 with `UpstreamErrorResponse` in the+ route's `utoipa::path`.- Fail closed if team cleanup encounters an external chat: external chats require human ownership.- GitHub reconnect preserves the current link until OAuth completes. A differentaccount returns 409; disconnect first to switch accounts. Missing grants report- reauthentication, while infrastructure failures remain server errors.+ reauthentication; GitHub or FusionAuth outages answer 502/503, and other+ failures remain server errors.Deploy grant-refresh support before releasing clients that omit the pre-OAuthdisconnect; older callbacks leave an existing grant unchanged.- FusionAuth reports a link owner that was deleted as `UserDoesNotExist`. Thatdiff --git a/services/authentication_service/Cargo.toml b/services/authentication_service/Cargo.tomlindex a07bdbcadb..c1e40fba6e 100644--- a/services/authentication_service/Cargo.toml+++ b/services/authentication_service/Cargo.toml@@ -177,6 +177,7 @@ tower = { workspace = true }tower-cookies = { version = "0.11.0" }tower-http = { workspace = true }tracing = { workspace = true }+upstream_error = { path = "../../crates/upstream_error" }url = { workspace = true }urlencoding = { workspace = true }user_language = { path = "../../crates/user_language", features = ["inbound", "postgres"] }diff --git a/services/authentication_service/src/api/github_pull_requests.rs b/services/authentication_service/src/api/github_pull_requests.rsindex 11691d7056..7ee3392145 100644--- a/services/authentication_service/src/api/github_pull_requests.rs+++ b/services/authentication_service/src/api/github_pull_requests.rs@@ -17,6 +17,7 @@ use github::domain::{};use macro_authorization::{MacroAuthorizationExtractor, UserOrInternal};use model::response::ErrorResponse;+use upstream_error::UpstreamErrorResponse;use crate::api::context::{ApiContext, AuthorizationService};@@ -36,6 +37,7 @@ impl IntoResponse for EnrichGithubPullRequestsProxyError {StatusCode::PRECONDITION_REQUIRED,"reauthentication required",),+ Self::Github(GithubError::Upstream(error)) => return error.into_response(),Self::Github(error) => {tracing::error!(error=?error, "failed to enrich GitHub pull requests");(StatusCode::INTERNAL_SERVER_ERROR, "internal error")@@ -73,6 +75,7 @@ impl IntoResponse for MergeGithubPullRequestError {Self::Github(GithubError::PullRequestMergeRejected { rejection, message }) => {(github::inbound::merge_rejection_status(rejection), message)}+ Self::Github(GithubError::Upstream(error)) => return error.into_response(),Self::Github(error) => {tracing::error!(error=?error, "failed to merge GitHub pull request");(@@ -116,6 +119,7 @@ impl IntoResponse for UpdateGithubPullRequestError {StatusCode::UNPROCESSABLE_ENTITY,"too many pull requests requested".to_string(),),+ Self::Github(GithubError::Upstream(error)) => return error.into_response(),Self::Github(error) => {tracing::error!(error=?error, "failed to update GitHub pull request");(@@ -156,6 +160,8 @@ pub fn router() -> Router<ApiContext> {(status = 404, body = ErrorResponse),(status = 428, body = ErrorResponse),(status = 500, body = ErrorResponse),+ (status = 502, body = UpstreamErrorResponse),+ (status = 503, body = UpstreamErrorResponse),))]#[tracing::instrument(skip(ctx, authorization, request), fields(user_id = %authorization.authorization.user.macro_user_id), err)]@@ -194,6 +200,8 @@ pub async fn handler((status = 422, body = ErrorResponse),(status = 428, body = ErrorResponse),(status = 500, body = ErrorResponse),+ (status = 502, body = UpstreamErrorResponse),+ (status = 503, body = UpstreamErrorResponse),))]#[tracing::instrument(skip(ctx, authorization, request), fields(user_id = %authorization.authorization.user.macro_user_id), err)]@@ -227,6 +235,8 @@ pub async fn merge_handler((status = 422, description = "GitHub declined the change", body = ErrorResponse),(status = 428, body = ErrorResponse),(status = 500, body = ErrorResponse),+ (status = 502, body = UpstreamErrorResponse),+ (status = 503, body = UpstreamErrorResponse),))]#[tracing::instrument(skip(ctx, authorization, request), fields(user_id = %authorization.authorization.user.macro_user_id), err)]@@ -260,6 +270,8 @@ pub async fn draft_handler((status = 422, description = "GitHub declined the change", body = ErrorResponse),(status = 428, body = ErrorResponse),(status = 500, body = ErrorResponse),+ (status = 502, body = UpstreamErrorResponse),+ (status = 503, body = UpstreamErrorResponse),))]#[tracing::instrument(skip(ctx, authorization, request), fields(user_id = %authorization.authorization.user.macro_user_id), err)]@@ -293,6 +305,8 @@ pub async fn state_handler((status = 422, description = "Too many pull requests requested", body = ErrorResponse),(status = 428, body = ErrorResponse),(status = 500, body = ErrorResponse),+ (status = 502, body = UpstreamErrorResponse),+ (status = 503, body = UpstreamErrorResponse),))]#[tracing::instrument(skip(ctx, authorization, request), fields(user_id = %authorization.authorization.user.macro_user_id), err)]diff --git a/services/authentication_service/src/api/internal/google_access_token.rs b/services/authentication_service/src/api/internal/google_access_token.rsindex 9c672c7cec..f4268d11ca 100644--- a/services/authentication_service/src/api/internal/google_access_token.rs+++ b/services/authentication_service/src/api/internal/google_access_token.rs@@ -45,53 +45,15 @@ async fn get_access_token(let fusionauth_user_id = params.fusionauth_user_id.as_str();let email = params.email.as_str();- // get identity provider idlet idp_id = auth_client.get_identity_provider_id_by_name(identity_provider_name).await- .map_err(|e| {- // No Gmail identity provider configured (e.g. local / --no-doppler- // stacks, or a deployment without the Google integration). This is- // an expected "no grant" outcome, not a server fault: the email- // service treats a 404 as NoGmailGrant and skips inbox init, while- // a 500 would surface as a hard login failure.- if matches!(e, FusionAuthClientError::NoIdentityProviderFound) {- tracing::debug!(error=?e, "no {} identity provider configured", identity_provider_name);- return no_link_response(identity_provider_name);- }- tracing::error!(error=?e, "unable to find idp id for {}", identity_provider_name);- (- StatusCode::INTERNAL_SERVER_ERROR,- Json(ErrorResponse {- message: "unable to find idp".into(),- }),- )- .into_response()- })?;+ .map_err(|e| identity_provider_error_response(e, identity_provider_name))?;- // get refresh token via linklet links = auth_client.get_links(fusionauth_user_id, Some(idp_id.clone())).await- .map_err(|e| {- // The grant's FusionAuth user is gone, so there is no grant.- if matches!(e, FusionAuthClientError::UserDoesNotExist) {- tracing::warn!(- fusionauth_user_id,- "no fusionauth user for {} grant",- identity_provider_name- );- return no_link_response(identity_provider_name);- }- tracing::error!(error=?e, "error fetching links for userid {} and idp id {}", fusionauth_user_id, idp_id.as_str());- (- StatusCode::INTERNAL_SERVER_ERROR,- Json(ErrorResponse {- message: "unable to fetch links".into(),- }),- )- .into_response()- })?;+ .map_err(|e| links_error_response(e, fusionauth_user_id, identity_provider_name))?;// a fusionauth user can have multiple links to the same identity provider with different email// addresses, but can only have one link with a given email@@ -109,26 +71,10 @@ async fn get_access_token(no_link_response(identity_provider_name)})?;- // get access token using refresh tokenlet token_response = auth_client.refresh_google_token(link.token.as_str()).await- .map_err(|e| {- let status_code = match &e {- // A revoked or expired grant is an expected outcome the caller- // handles by asking the user to reauthorize.- FusionAuthClientError::InvalidGrant => {- tracing::warn!(error=?e, "refresh token rejected for userid {}", fusionauth_user_id);- StatusCode::FORBIDDEN- }- _ => {- tracing::error!(error=?e, "error fetching access token for userid {}", fusionauth_user_id);- StatusCode::INTERNAL_SERVER_ERROR- }- };- let message = format!("unable to fetch {} access token", identity_provider_name);- (status_code, Json(ErrorResponse { message: message.into() })).into_response()- })?;+ .map_err(|e| refresh_error_response(e, fusionauth_user_id, identity_provider_name))?;Ok((StatusCode::OK,@@ -149,3 +95,87 @@ fn no_link_response(identity_provider_name: &str) -> Response {).into_response()}++fn identity_provider_error_response(+ error: FusionAuthClientError,+ identity_provider_name: &str,+) -> Response {+ // No Gmail identity provider configured (e.g. local / --no-doppler+ // stacks, or a deployment without the Google integration). This is+ // an expected "no grant" outcome, not a server fault: the email+ // service treats a 404 as NoGmailGrant and skips inbox init, while+ // a 500 would surface as a hard login failure.+ if matches!(error, FusionAuthClientError::NoIdentityProviderFound) {+ tracing::debug!(error=?error, "no {} identity provider configured", identity_provider_name);+ return no_link_response(identity_provider_name);+ }+ if let Some(upstream) = error.upstream() {+ return upstream.into_response();+ }+ tracing::error!(error=?error, "unable to find idp id for {}", identity_provider_name);+ (+ StatusCode::INTERNAL_SERVER_ERROR,+ Json(ErrorResponse {+ message: "unable to find idp".into(),+ }),+ )+ .into_response()+}++fn links_error_response(+ error: FusionAuthClientError,+ fusionauth_user_id: &str,+ identity_provider_name: &str,+) -> Response {+ if let Some(upstream) = error.upstream() {+ return upstream.into_response();+ }+ // The grant's FusionAuth user is gone, so there is no grant.+ if matches!(error, FusionAuthClientError::UserDoesNotExist) {+ tracing::warn!(+ fusionauth_user_id,+ "no fusionauth user for {} grant",+ identity_provider_name+ );+ return no_link_response(identity_provider_name);+ }+ tracing::error!(error=?error, "error fetching {} links for userid {}", identity_provider_name, fusionauth_user_id);+ (+ StatusCode::INTERNAL_SERVER_ERROR,+ Json(ErrorResponse {+ message: "unable to fetch links".into(),+ }),+ )+ .into_response()+}++fn refresh_error_response(+ error: FusionAuthClientError,+ fusionauth_user_id: &str,+ identity_provider_name: &str,+) -> Response {+ if let Some(upstream) = error.upstream() {+ tracing::warn!(error=%error, "google token refresh failed upstream");+ return upstream.into_response();+ }+ let status_code = match &error {+ // A revoked or expired grant is an expected outcome the caller+ // handles by asking the user to reauthorize.+ FusionAuthClientError::InvalidGrant => {+ tracing::warn!(error=?error, "refresh token rejected for userid {}", fusionauth_user_id);+ StatusCode::FORBIDDEN+ }+ _ => {+ tracing::error!(error=?error, "error fetching access token for userid {}", fusionauth_user_id);+ StatusCode::INTERNAL_SERVER_ERROR+ }+ };+ let message = format!("unable to fetch {} access token", identity_provider_name);+ (+ status_code,+ Json(ErrorResponse {+ message: message.into(),+ }),+ )+ .into_response()+}diff --git a/services/authentication_service/src/api/internal/google_access_token/test.rs b/services/authentication_service/src/api/internal/google_access_token/test.rsindex 7313ad9ba8..09a72c808c 100644--- a/services/authentication_service/src/api/internal/google_access_token/test.rs+++ b/services/authentication_service/src/api/internal/google_access_token/test.rs@@ -1,6 +1,76 @@+use axum::http::StatusCode;+use fusionauth::error::{FusionAuthClientError, GenericErrorResponse};+use upstream_error::UpstreamFailure;+use super::*;use crate::api::internal::fusionauth_fixture;+const IDP: &str = "google_gmail";+const USER: &str = "fusionauth-user";++fn google_outage() -> FusionAuthClientError {+ FusionAuthClientError::Upstream {+ service: "Google",+ failure: UpstreamFailure::Unavailable,+ }+}++fn sign_in_outage() -> FusionAuthClientError {+ FusionAuthClientError::Upstream {+ service: "The sign-in service",+ failure: UpstreamFailure::Unavailable,+ }+}++fn generic() -> FusionAuthClientError {+ FusionAuthClientError::Generic(GenericErrorResponse {+ message: "boom".into(),+ })+}++#[test]+fn a_google_outage_during_refresh_is_service_unavailable() {+ let response = refresh_error_response(google_outage(), USER, IDP);++ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);+}++#[test]+fn a_revoked_grant_during_refresh_is_still_forbidden() {+ let response = refresh_error_response(FusionAuthClientError::InvalidGrant, USER, IDP);++ assert_eq!(response.status(), StatusCode::FORBIDDEN);+}++#[test]+fn an_unknown_refresh_failure_is_still_internal() {+ let response = refresh_error_response(generic(), USER, IDP);++ assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);+}++#[test]+fn a_sign_in_outage_while_fetching_links_is_service_unavailable() {+ let response = links_error_response(sign_in_outage(), USER, IDP);++ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);+}++#[test]+fn a_sign_in_outage_while_finding_the_provider_is_service_unavailable() {+ let response = identity_provider_error_response(sign_in_outage(), IDP);++ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);+}++#[test]+fn a_missing_provider_has_no_grant() {+ let response =+ identity_provider_error_response(FusionAuthClientError::NoIdentityProviderFound, IDP);++ assert_eq!(response.status(), StatusCode::NOT_FOUND);+}+#[tokio::test]async fn a_deleted_user_has_no_google_grant() {let (_server, auth_client) = fusionauth_fixture::without_user("deleted-user").await;diff --git a/services/authentication_service/src/api/link/github.rs b/services/authentication_service/src/api/link/github.rsindex a0d2ff6bf1..282f278c1a 100644--- a/services/authentication_service/src/api/link/github.rs+++ b/services/authentication_service/src/api/link/github.rs@@ -13,6 +13,7 @@ use macro_authorization::{MacroAuthorizationExtractor, UserOrInternal};use macro_middleware::tracking::ClientIp;use model::response::{EmptyResponse, ErrorResponse};use serde_utils::urlencode::UrlEncoded;+use upstream_error::{UpstreamError, UpstreamErrorResponse};use url::Url;use crate::{@@ -20,6 +21,9 @@ use crate::{api::context::{ApiContext, AuthorizationService},};+#[cfg(test)]+mod test;+pub const REAUTHENTICATION_REQUIRED_MESSAGE: &str = "ReauthenticationRequired";#[derive(serde::Deserialize, serde::Serialize, Debug, utoipa::ToSchema)]@@ -58,14 +62,21 @@ pub enum InitGithubLinkError {/// The identity provider was not found#[error("identity provider not found")]IdentityProviderNotFound,+ /// The sign-in service could not be reached to look up the provider.+ #[error(transparent)]+ Upstream(UpstreamError),}impl IntoResponse for InitGithubLinkError {fn into_response(self) -> Response {let message = self.to_string();- let status_code: StatusCode = match &self {+ let status_code: StatusCode = match self {InitGithubLinkError::InvalidUserId(_) => StatusCode::BAD_REQUEST,InitGithubLinkError::TooManyInProgressLinks => StatusCode::TOO_MANY_REQUESTS,+ InitGithubLinkError::Upstream(error)+ | InitGithubLinkError::GithubServiceError(GithubError::Upstream(error)) => {+ return error.into_response();+ }InitGithubLinkError::InternalError(_)| InitGithubLinkError::GithubServiceError(_)| InitGithubLinkError::IdentityProviderNotFound => StatusCode::INTERNAL_SERVER_ERROR,@@ -101,7 +112,10 @@ pub enum GithubLinkStatusError {impl IntoResponse for GithubLinkStatusError {fn into_response(self) -> Response {- let Self::Github(error) = &self;+ let Self::Github(error) = self;+ if let GithubError::Upstream(error) = error {+ return error.into_response();+ }tracing::error!(error=?error, "failed to check GitHub link status");error_response(StatusCode::INTERNAL_SERVER_ERROR, "internal error")}@@ -118,6 +132,8 @@ impl IntoResponse for GithubLinkStatusError {(status = 404, body=ErrorResponse),(status = 428, body=ErrorResponse),(status = 500, body=ErrorResponse),+ (status = 502, body=UpstreamErrorResponse),+ (status = 503, body=UpstreamErrorResponse),))]#[tracing::instrument(skip(ctx, ip_context, authorization), fields(client_ip=%ip_context, user_id=%authorization.authorization.user.macro_user_id), err)]@@ -173,6 +189,8 @@ pub(crate) struct InitGithubLinkQueryParams {(status = 429, body=ErrorResponse),(status = 401, body=ErrorResponse),(status = 500, body=ErrorResponse),+ (status = 502, body=UpstreamErrorResponse),+ (status = 503, body=UpstreamErrorResponse),))]#[tracing::instrument(skip(ctx, ip_context, authorization), fields(client_ip=%ip_context, user_id=%authorization.authorization.user.user_context.user_id, fusion_user_id=%authorization.authorization.user.user_context.fusion_user_id), err)]@@ -211,7 +229,13 @@ pub async fn init_github_link_handler(.auth_client.get_identity_provider_id_by_name("github").await- .map_err(|_| InitGithubLinkError::IdentityProviderNotFound)?;+ .map_err(|error| match error.upstream() {+ Some(upstream) => InitGithubLinkError::Upstream(upstream),+ None => {+ tracing::error!(error=?error, "failed to look up the github identity provider");+ InitGithubLinkError::IdentityProviderNotFound+ }+ })?;// Build the signed OAuth state. The callback refuses state it did not sign,// state issued for another provider, and state whose pending link belongs@@ -254,7 +278,10 @@ pub enum DeleteGithubLinkError {impl IntoResponse for DeleteGithubLinkError {fn into_response(self) -> Response {let message = self.to_string();- let status_code: StatusCode = match &self {+ let status_code: StatusCode = match self {+ DeleteGithubLinkError::GithubServiceError(GithubError::Upstream(error)) => {+ return error.into_response();+ }DeleteGithubLinkError::InternalError(_)| DeleteGithubLinkError::GithubServiceError(_) => StatusCode::INTERNAL_SERVER_ERROR,};@@ -279,6 +306,8 @@ impl IntoResponse for DeleteGithubLinkError {(status = 400, body=ErrorResponse),(status = 401, body=ErrorResponse),(status = 500, body=ErrorResponse),+ (status = 502, body=UpstreamErrorResponse),+ (status = 503, body=UpstreamErrorResponse),))]#[tracing::instrument(skip(ctx, ip_context, authorization), fields(client_ip=%ip_context, user_id=%authorization.authorization.user.macro_user_id), err)]diff --git a/services/authentication_service/src/api/link/github/test.rs b/services/authentication_service/src/api/link/github/test.rsnew file mode 100644index 0000000000..75cfe598cb--- /dev/null+++ b/services/authentication_service/src/api/link/github/test.rs@@ -0,0 +1,143 @@+use std::time::Duration;++use axum::{+ http::{StatusCode, header::RETRY_AFTER},+ response::IntoResponse,+};+use github::domain::models::GithubError;+use http_body_util::BodyExt;+use upstream_error::{UpstreamError, UpstreamFailure};+use utoipa::OpenApi;++use super::*;+use crate::api::{+ github_pull_requests::{+ EnrichGithubPullRequestsProxyError, MergeGithubPullRequestError,+ UpdateGithubPullRequestError,+ },+ swagger::ApiDoc,+};++fn github_failing(failure: UpstreamFailure) -> GithubError {+ GithubError::Upstream(UpstreamError {+ service: "GitHub",+ failure,+ })+}++async fn json_body(response: Response) -> serde_json::Value {+ let body = response.into_body().collect().await.unwrap().to_bytes();+ serde_json::from_slice(&body).unwrap()+}++#[tokio::test]+async fn a_github_outage_on_link_status_is_service_unavailable() {+ let response =+ GithubLinkStatusError::Github(github_failing(UpstreamFailure::Unavailable)).into_response();++ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);+ let body = json_body(response).await;+ assert_eq!(body["code"], "upstream_unavailable");+ assert_eq!(+ body["message"],+ "GitHub is temporarily unavailable. Try again in a few minutes."+ );+}++#[test]+fn a_github_rate_limit_on_link_status_says_when_to_retry() {+ let response = GithubLinkStatusError::Github(github_failing(UpstreamFailure::RateLimited {+ retry_after: Some(Duration::from_secs(42)),+ }))+ .into_response();++ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);+ assert_eq!(response.headers()[RETRY_AFTER], "42");+}++#[test]+fn an_unusable_github_answer_on_link_status_is_bad_gateway() {+ let response = GithubLinkStatusError::Github(github_failing(UpstreamFailure::InvalidResponse))+ .into_response();++ assert_eq!(response.status(), StatusCode::BAD_GATEWAY);+}++#[tokio::test]+async fn our_own_failure_on_link_status_is_still_internal() {+ let response = GithubLinkStatusError::Github(GithubError::Internal(anyhow::anyhow!("bug")))+ .into_response();++ assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);+ assert_eq!(json_body(response).await["message"], "internal error");+}++#[tokio::test]+async fn a_sign_in_outage_while_starting_a_link_is_service_unavailable() {+ let response = InitGithubLinkError::Upstream(UpstreamError {+ service: "The sign-in service",+ failure: UpstreamFailure::Unavailable,+ })+ .into_response();++ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);+ let body = json_body(response).await;+ assert_eq!(+ body["message"],+ "The sign-in service is temporarily unavailable. Try again in a few minutes."+ );+}++#[test]+fn a_github_outage_while_deleting_a_link_is_service_unavailable() {+ let response =+ DeleteGithubLinkError::GithubServiceError(github_failing(UpstreamFailure::Unavailable))+ .into_response();++ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);+}++#[test]+fn a_github_outage_on_pull_request_routes_is_service_unavailable() {+ let outage = || github_failing(UpstreamFailure::Unavailable);+ let responses = [+ EnrichGithubPullRequestsProxyError::Github(outage()).into_response(),+ MergeGithubPullRequestError::Github(outage()).into_response(),+ UpdateGithubPullRequestError::Github(outage()).into_response(),+ ];++ for response in responses {+ assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);+ }+}++#[test]+fn github_link_routes_document_upstream_failures() {+ let openapi = serde_json::to_value(ApiDoc::openapi()).unwrap();+ let operations = [+ ("/link/github/status", "get"),+ ("/link/github", "post"),+ ("/link/github", "delete"),+ ("/github_pull_requests/enrich", "post"),+ ("/github_pull_requests/merge", "post"),+ ("/github_pull_requests/draft", "post"),+ ("/github_pull_requests/state", "post"),+ ("/github_pull_requests/mergeability", "post"),+ ];++ for (path, method) in operations {+ let responses = &openapi["paths"][path][method]["responses"];+ for status in ["502", "503"] {+ assert_eq!(+ responses[status]["content"]["application/json"]["schema"]["$ref"].as_str(),+ Some("#/components/schemas/UpstreamErrorResponse"),+ "{method} {path} does not document {status}"+ );+ }+ }+ assert!(+ openapi["components"]["schemas"]+ .get("UpstreamErrorResponse")+ .is_some()+ );+}diff --git a/services/authentication_service/src/api/oauth2/github.rs b/services/authentication_service/src/api/oauth2/github.rsindex 5d0625fd6d..056395b411 100644--- a/services/authentication_service/src/api/oauth2/github.rs+++ b/services/authentication_service/src/api/oauth2/github.rs@@ -47,6 +47,9 @@ impl IntoResponse for GithubLinkError {tracing::error!(error=?error, "internal error occurred while linking github account");(StatusCode::INTERNAL_SERVER_ERROR, "internal error occurred")}+ GithubLinkError::GithubServiceError(GithubError::Upstream(error)) => {+ return error.into_response();+ }GithubLinkError::GithubServiceError(error) => {tracing::error!(error=?error, "github service error occurred while linking github account");(StatusCode::INTERNAL_SERVER_ERROR, "internal error occurred")diff --git a/services/authentication_service/src/api/oauth2/test.rs b/services/authentication_service/src/api/oauth2/test.rsindex 951171baa1..6c9017b926 100644--- a/services/authentication_service/src/api/oauth2/test.rs+++ b/services/authentication_service/src/api/oauth2/test.rs@@ -427,3 +427,22 @@ fn verified_link_state_converts_to_provider_handler_state() {assert_eq!(state.original_url, link.original_url);assert_eq!(state.is_mobile, None);}++#[test]+fn a_github_outage_while_finishing_a_link_is_service_unavailable() {+ use ::github::domain::models::GithubError;+ use axum::response::IntoResponse;+ use upstream_error::{UpstreamError, UpstreamFailure};++ let response =+ super::github::GithubLinkError::GithubServiceError(GithubError::Upstream(UpstreamError {+ service: "GitHub",+ failure: UpstreamFailure::Unavailable,+ }))+ .into_response();++ assert_eq!(+ response.status(),+ axum::http::StatusCode::SERVICE_UNAVAILABLE+ );+}