fix(http): let the package classify command 401/403/429 and keep re-login first (03-02)
These statuses skipped the package classifier and always became MAYBE_APPLIED, so an expired session ended in "contact support" or the unknown-result screen. The descriptor gains optional classifyStatus, the reference create declares NOT_APPLIED, and AUTH_REQUIRED keeps reauth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
dfb1b9e0ee
commit
2afee67150
@@ -128,6 +128,28 @@ export function classifyProblemEffect<Problem>(
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* D-2. A dispatched command answered 401, 403 or 429 is `MAYBE_APPLIED` unless
|
||||
* the package classifies that status as `NOT_APPLIED`. Any other answer, or a
|
||||
* throw, fails safe to `MAYBE_APPLIED`.
|
||||
*/
|
||||
export function classifyStatusEffect<Problem>(
|
||||
descriptor: CommandEffectDescriptor<Problem> | null,
|
||||
status: 401 | 403 | 429,
|
||||
abandoned: MutationEffectCertainty,
|
||||
): MutationEffectCertainty {
|
||||
if (abandoned !== "MAYBE_APPLIED" || !descriptor?.classifyStatus) {
|
||||
return abandoned;
|
||||
}
|
||||
try {
|
||||
return descriptor.classifyStatus(status) === "NOT_APPLIED"
|
||||
? "NOT_APPLIED"
|
||||
: "MAYBE_APPLIED";
|
||||
} catch {
|
||||
return "MAYBE_APPLIED";
|
||||
}
|
||||
}
|
||||
|
||||
/** §8.10. Certainty to UI intent. The copy itself is owned by the i18n catalog. */
|
||||
export function projectCertaintyToUi(
|
||||
certainty: MutationEffectCertainty,
|
||||
|
||||
@@ -36,6 +36,7 @@ import {
|
||||
import {
|
||||
certaintyForAbandonedAttempt,
|
||||
classifyProblemEffect,
|
||||
classifyStatusEffect,
|
||||
joinMutationEffectCertainty,
|
||||
type MutationEffectCertainty,
|
||||
type PhysicalAttemptState,
|
||||
@@ -1142,25 +1143,24 @@ async function admitResponse<Input, WireOutput, Problem>(
|
||||
}
|
||||
|
||||
const metadata = safeMetadata(response, retryAfterMs);
|
||||
const statusEffect = (classified: 401 | 403 | 429) =>
|
||||
classifyStatusEffect(
|
||||
contract.commandEffect,
|
||||
classified,
|
||||
certaintyForAbandonedAttempt(attemptState, isCommand),
|
||||
);
|
||||
|
||||
if (!contract.acceptedStatuses.includes(status)) {
|
||||
if (status === 401) {
|
||||
await response.body?.cancel().catch(() => {});
|
||||
return settled(
|
||||
unauthenticated(
|
||||
certaintyForAbandonedAttempt(attemptState, isCommand),
|
||||
isCommand,
|
||||
metadata,
|
||||
),
|
||||
unauthenticated(statusEffect(401), isCommand, metadata),
|
||||
"UNAUTHENTICATED",
|
||||
);
|
||||
}
|
||||
if (status === 403) {
|
||||
await response.body?.cancel().catch(() => {});
|
||||
return settled(
|
||||
forbidden(certaintyForAbandonedAttempt(attemptState, isCommand), isCommand),
|
||||
"FORBIDDEN",
|
||||
);
|
||||
return settled(forbidden(statusEffect(403), isCommand), "FORBIDDEN");
|
||||
}
|
||||
if (status === 429) {
|
||||
await response.body?.cancel().catch(() => {});
|
||||
@@ -1168,10 +1168,7 @@ async function admitResponse<Input, WireOutput, Problem>(
|
||||
result: Object.freeze({
|
||||
kind: "RATE_LIMITED" as const,
|
||||
...(retryAfterMs === null ? {} : { retryAfterMs }),
|
||||
effect: normalizeOptionalEffect(
|
||||
certaintyForAbandonedAttempt(attemptState, isCommand),
|
||||
isCommand,
|
||||
),
|
||||
effect: normalizeOptionalEffect(statusEffect(429), isCommand),
|
||||
}),
|
||||
certainty: "RATE_LIMITED",
|
||||
retryHint: true,
|
||||
|
||||
+10
-2
@@ -314,13 +314,21 @@ export function createFailure(
|
||||
userMessageKey: definition.userMessageKey,
|
||||
action:
|
||||
details.effect === "MAYBE_APPLIED"
|
||||
? "contact-support"
|
||||
? unknownEffectAction(definition.action)
|
||||
: details.effect === "APPLIED_CONFIRMED"
|
||||
? "none"
|
||||
: definition.action,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* D-2. An unknown effect sends the user to support, except when the session
|
||||
* itself is the problem: signing in again comes first either way.
|
||||
*/
|
||||
function unknownEffectAction(action: ErrorAction): ErrorAction {
|
||||
return action === "reauth" ? "reauth" : "contact-support";
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds controller-owned mutation effect knowledge without weakening the
|
||||
* fail-safe handling required for an unknown server-side outcome.
|
||||
@@ -333,7 +341,7 @@ export function withFailureEffect(
|
||||
...failure,
|
||||
effect,
|
||||
...(effect === "MAYBE_APPLIED"
|
||||
? { retryable: false as const, action: "contact-support" as const }
|
||||
? { retryable: false as const, action: unknownEffectAction(failure.action) }
|
||||
: effect === "APPLIED_CONFIRMED"
|
||||
? { retryable: false as const, action: "none" as const }
|
||||
: {}),
|
||||
|
||||
@@ -123,6 +123,12 @@ export interface CommandEffectDescriptor<Problem> {
|
||||
classifyProblem(
|
||||
input: Readonly<{ status: number; problem: Problem }>,
|
||||
): CommandEffectClassification;
|
||||
/**
|
||||
* D-2. 401, 403 and 429 are admitted by status before any problem body is
|
||||
* read. Declare `NOT_APPLIED` only when the server contract guarantees the
|
||||
* command was rejected untouched. Without this, they stay `MAYBE_APPLIED`.
|
||||
*/
|
||||
classifyStatus?(status: 401 | 403 | 429): "NOT_APPLIED" | "MAYBE_APPLIED";
|
||||
}
|
||||
|
||||
export type HttpMethod =
|
||||
@@ -470,6 +476,12 @@ function assertHttpContract(
|
||||
if (typeof contract.commandEffect.classifyProblem !== "function") {
|
||||
fail(`${label}: command effect classifier is required`);
|
||||
}
|
||||
if (
|
||||
contract.commandEffect.classifyStatus !== undefined &&
|
||||
typeof contract.commandEffect.classifyStatus !== "function"
|
||||
) {
|
||||
fail(`${label}: command status classifier must be a function`);
|
||||
}
|
||||
if (contract.retrySemantics === "KEYED" && !contract.commandRecovery) {
|
||||
fail(`${label}: KEYED commands require a recovery descriptor`);
|
||||
}
|
||||
|
||||
@@ -44,6 +44,11 @@ const CREATE_EFFECT: CommandEffectDescriptor<ReferenceProblem> = Object.freeze({
|
||||
}
|
||||
return "MAYBE_APPLIED";
|
||||
},
|
||||
/**
|
||||
* D-2. The reference backend rejects an unauthenticated, forbidden or
|
||||
* rate-limited create before touching state.
|
||||
*/
|
||||
classifyStatus: () => "NOT_APPLIED" as const,
|
||||
});
|
||||
|
||||
export const LIST_REFERENCE_RESOURCES_CONTRACT = Object.freeze({
|
||||
|
||||
Reference in New Issue
Block a user