@gusnips/react 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -152,7 +152,18 @@ the prop shape _is_ the rule: `problem`, `cause`, `fix`, and `action`. **`fix` a
152
152
  required.** A required prop is the only version of "always offer a way out" that a caller in a
153
153
  hurry cannot skip.
154
154
 
155
- `createErrorDescriber` turns a thrown `ApiError` into the `cause` and `fix` to put in it.
155
+ `createErrorDescriber` turns a thrown `ApiError` into the `cause` and `fix` to put in it — plus
156
+ which control to offer, and the request id to print under it:
157
+
158
+ ```ts
159
+ const { cause, hint, recover, reference } = describeError(error);
160
+ // recover: "retry" | "signin" | "wait" | "none"
161
+ ```
162
+
163
+ `recover` is derived from `shouldRetry` — the same rule react-query retries on — so the button a
164
+ reader sees and the retry that actually happens cannot disagree. Give it the same
165
+ `durableLimitCodes` list you give `queryDefaults`, and a spent quota offers no button instead of
166
+ one that cannot work.
156
167
 
157
168
  ## Also here
158
169
 
@@ -17,11 +17,40 @@ import { ApiError } from "./api-error.ts";
17
17
  * no arms, because the arms are that API's vocabulary. What ships is the scaffolding around it,
18
18
  * which is the part that was written twice and got subtly different both times.
19
19
  */
20
- export interface DescribedError {
20
+ /**
21
+ * Which control to offer beside the words.
22
+ *
23
+ * The fourth adopter had this and the package did not, which left every adopter re-deriving
24
+ * "can a second attempt fix this" from its own switch — beside a `shouldRetry` already deciding
25
+ * exactly that for react-query. Two answers to one question drift, and the drift is visible: a
26
+ * screen offering "try again" for a refusal the query layer refuses to retry, so the button does
27
+ * nothing and the reader presses it twice.
28
+ *
29
+ * So this is DERIVED from `shouldRetry`, not switched on separately. One rule, two consumers.
30
+ */
31
+ export type RecoveryKind =
32
+ /** A second attempt can work. Offer the button. */
33
+ "retry"
34
+ /** Auth said no. Offer a way back to sign-in, never a retry. */
35
+ | "signin"
36
+ /** It stated its own expiry. Offer the button, disabled, with the countdown. */
37
+ | "wait"
38
+ /** Waiting cannot fix it and neither can pressing anything. Offer no control. */
39
+ | "none";
40
+ /** The copy an arm writes. Everything it leaves out, {@link createErrorDescriber} fills in. */
41
+ export interface ErrorCopy {
21
42
  /** What happened, in the reader's words. */
22
43
  cause: string;
23
44
  /** What to do about it. Its absence should be a decision, not an oversight. */
24
45
  hint?: string;
46
+ /** Override the derived kind — a 429 whose code means a spent quota is `"none"`, not `"wait"`. */
47
+ recover?: RecoveryKind;
48
+ /** Override the request id. Rarely wanted; the envelope's is used by default. */
49
+ reference?: string;
50
+ }
51
+ export interface DescribedError extends ErrorCopy {
52
+ /** Always present on the way out, whether an arm set it or the rule derived it. */
53
+ recover: RecoveryKind;
25
54
  }
26
55
  /**
27
56
  * The subset of i18next's `t` this module needs — typed here so `i18next` stays optional.
@@ -54,6 +83,13 @@ export interface ErrorContext {
54
83
  /** Seconds until it clears, already humanized. Null when the envelope did not say. */
55
84
  wait: string | null;
56
85
  waitSecs: number | null;
86
+ /**
87
+ * What the rule decided before this arm ran. Read it to agree, return your own to override.
88
+ *
89
+ * Exposed so an arm can narrow rather than restate: a quota arm that already knows the limit is
90
+ * durable returns `"none"`, and every other arm can leave it alone and stay right by default.
91
+ */
92
+ recover: RecoveryKind;
57
93
  }
58
94
  export interface ErrorDescriberOptions<Prefix extends string, ServerPrefix extends string, ServerName extends string> {
59
95
  t: Translate<`${Prefix}${CopyName}` | `${ServerPrefix}${ServerName}`>;
@@ -91,9 +127,17 @@ export interface ErrorDescriberOptions<Prefix extends string, ServerPrefix exten
91
127
  * code that does not exist is a compile error at the arm rather than a branch that never runs.
92
128
  */
93
129
  codes?: Partial<Record<string, ErrorArm>>;
130
+ /**
131
+ * The codes that mean "this limit does not clear by waiting" — **the same list you hand
132
+ * `queryDefaults`**, because it answers the same question and a second copy is a second
133
+ * chance to disagree with the retry rule. Declare it once in the app and pass it twice.
134
+ */
135
+ durableLimitCodes?: readonly string[];
136
+ /** Longest stated wait still worth a retry rather than a countdown. Matches `queryDefaults`. */
137
+ maxRetryWaitSecs?: number;
94
138
  }
95
- export type ErrorArm = (ctx: ErrorContext) => DescribedError;
139
+ export type ErrorArm = (ctx: ErrorContext) => ErrorCopy;
96
140
  /** Build the describer. */
97
- export declare function createErrorDescriber<Prefix extends string, ServerPrefix extends string, ServerName extends string>({ t, copyPrefix, messageKeyPrefix, knownMessageKeys, codes, }: ErrorDescriberOptions<Prefix, ServerPrefix, ServerName>): (error: unknown) => DescribedError;
141
+ export declare function createErrorDescriber<Prefix extends string, ServerPrefix extends string, ServerName extends string>({ t, copyPrefix, messageKeyPrefix, knownMessageKeys, codes, durableLimitCodes, maxRetryWaitSecs, }: ErrorDescriberOptions<Prefix, ServerPrefix, ServerName>): (error: unknown) => DescribedError;
98
142
  export {};
99
143
  //# sourceMappingURL=describe-error.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"describe-error.d.ts","sourceRoot":"","sources":["../src/describe-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAkB,MAAM,gBAAgB,CAAC;AAE1D;;;;;;;;;;;;;;;;;GAiBG;AAEH,MAAM,WAAW,cAAc;IAC7B,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd,+EAA+E;IAC/E,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;GASG;AACH,MAAM,MAAM,SAAS,CAAC,GAAG,SAAS,MAAM,GAAG,MAAM,IAAI,CACnD,GAAG,EAAE,GAAG,EACR,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC7B,MAAM,CAAC;AAEZ,wFAAwF;AACxF,KAAK,QAAQ,GAAG,aAAa,GAAG,aAAa,GAAG,WAAW,CAAC;AAE5D,iGAAiG;AACjG,KAAK,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,aAAa,GAAG,YAAY,GAAG,WAAW,CAAC;AA8BlF;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,MAAM,SAAS,MAAM,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Ac,CA4F9F"}
@@ -1,4 +1,5 @@
1
1
  import { ApiError, retryAfterSecs } from "./api-error.js";
2
+ import { shouldRetry } from "./query.js";
2
3
  /**
3
4
  * "in 4 minutes" / "in 2 hours" — a wait nobody has to convert from seconds.
4
5
  *
@@ -15,7 +16,7 @@ export function humanizeWait(t, secs, prefix) {
15
16
  return t(`${prefix}waitHours`, { count: Math.round(secs / 3600) });
16
17
  }
17
18
  /** Build the describer. */
18
- export function createErrorDescriber({ t, copyPrefix, messageKeyPrefix, knownMessageKeys, codes = {}, }) {
19
+ export function createErrorDescriber({ t, copyPrefix, messageKeyPrefix, knownMessageKeys, codes = {}, durableLimitCodes = [], maxRetryWaitSecs, }) {
19
20
  const known = new Set(Object.keys(knownMessageKeys));
20
21
  // Sound, and the reason nothing here needs a cast: the set was built from the keys of
21
22
  // `knownMessageKeys`, so membership really does establish the claim. `Object.keys` and not
@@ -34,11 +35,37 @@ export function createErrorDescriber({ t, copyPrefix, messageKeyPrefix, knownMes
34
35
  return (lookup(error.messageKey) ??
35
36
  (error.code === undefined ? null : lookup(`${messageKeyPrefix}${error.code}`)));
36
37
  }
38
+ /**
39
+ * Which control to offer, decided once and never switched on separately.
40
+ *
41
+ * Everything below the last line defers to `shouldRetry` — the rule invariant 8 already owns —
42
+ * so the button a reader sees and the retry react-query performs cannot disagree.
43
+ */
44
+ function recoveryFor(error, waitSecs) {
45
+ // The client raised this one itself, to stop a caller while a dead session was already being
46
+ // handled. Nothing is broken and the person is already on their way to sign-in, so asking
47
+ // them to try again would be the surface contradicting the thing it is reporting.
48
+ if (error.expected)
49
+ return "none";
50
+ // 401 is "we do not know who you are", which no number of attempts answers. 403 is NOT this:
51
+ // the session is fine and it is a different account that would help, so it falls through.
52
+ if (error.status === 401)
53
+ return "signin";
54
+ // It named its own expiry, so there is a real number to count down beside a disabled button.
55
+ if (waitSecs !== null)
56
+ return "wait";
57
+ return shouldRetry(error, durableLimitCodes, maxRetryWaitSecs) ? "retry" : "none";
58
+ }
37
59
  return function describeError(error) {
38
60
  if (!(error instanceof ApiError)) {
39
61
  // Not a response at all — the request never landed. Almost always the network, and almost
40
- // never worth showing a stack trace for.
41
- return { cause: t(`${copyPrefix}network`), hint: t(`${copyPrefix}networkHint`) };
62
+ // never worth showing a stack trace for. `shouldRetry` answers a non-response the same way,
63
+ // so these two agree here without either being told about the other.
64
+ return {
65
+ cause: t(`${copyPrefix}network`),
66
+ hint: t(`${copyPrefix}networkHint`),
67
+ recover: "retry",
68
+ };
42
69
  }
43
70
  const waitSecs = retryAfterSecs(error);
44
71
  const ctx = {
@@ -46,10 +73,8 @@ export function createErrorDescriber({ t, copyPrefix, messageKeyPrefix, knownMes
46
73
  says: serverSentence(error),
47
74
  wait: waitSecs === null ? null : humanizeWait(t, waitSecs, copyPrefix),
48
75
  waitSecs,
76
+ recover: recoveryFor(error, waitSecs),
49
77
  };
50
- const arm = error.code === undefined ? undefined : codes[error.code];
51
- if (arm)
52
- return arm(ctx);
53
78
  // Anything unmapped: the server's own sentence is still the most specific thing we have,
54
79
  // and support can act on it. A 5xx additionally gets "try again shortly", because that one
55
80
  // genuinely does clear on its own — a 4xx does not, and saying so would be a lie that costs
@@ -59,10 +84,22 @@ export function createErrorDescriber({ t, copyPrefix, messageKeyPrefix, knownMes
59
84
  // own `Request failed (502)`, written for a log. A gateway answering with HTML while the API
60
85
  // restarts is exactly when that happens, so it reached screens — until the second migration,
61
86
  // whose three apps each guarded against that one string by hand.
62
- const serverWords = error.code === undefined ? "" : error.message;
87
+ const arm = error.code === undefined ? undefined : codes[error.code];
88
+ const copy = arm
89
+ ? arm(ctx)
90
+ : {
91
+ cause: ctx.says ??
92
+ ((error.code === undefined ? "" : error.message) || t(`${copyPrefix}unexpected`)),
93
+ hint: error.status >= 500 ? t(`${copyPrefix}retrySoon`) : undefined,
94
+ };
95
+ // The arm wins where it spoke, the rule fills the rest. `requestId` is the one thing every
96
+ // error surface asks for and nothing was producing — `ErrorStateProps` has reserved a
97
+ // `reference` slot for it since the contract was written.
98
+ const reference = copy.reference ?? error.requestId;
63
99
  return {
64
- cause: ctx.says ?? (serverWords || t(`${copyPrefix}unexpected`)),
65
- hint: error.status >= 500 ? t(`${copyPrefix}retrySoon`) : undefined,
100
+ ...copy,
101
+ recover: copy.recover ?? ctx.recover,
102
+ ...(reference !== undefined && { reference }),
66
103
  };
67
104
  };
68
105
  }
@@ -1 +1 @@
1
- {"version":3,"file":"describe-error.js","sourceRoot":"","sources":["../src/describe-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AA6E1D;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAC1B,CAAoC,EACpC,IAAY,EACZ,MAAc;IAEd,IAAI,IAAI,GAAG,EAAE;QAAE,OAAO,CAAC,CAAC,GAAG,MAAM,aAAa,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;IAC1F,IAAI,IAAI,GAAG,EAAE,GAAG,EAAE;QAAE,OAAO,CAAC,CAAC,GAAG,MAAM,aAAa,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;IACvF,OAAO,CAAC,CAAC,GAAG,MAAM,WAAW,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC;AACrE,CAAC;AAuDD,2BAA2B;AAC3B,MAAM,UAAU,oBAAoB,CAIlC,EACA,CAAC,EACD,UAAU,EACV,gBAAgB,EAChB,gBAAgB,EAChB,KAAK,GAAG,EAAE,GAC8C;IACxD,MAAM,KAAK,GAAG,IAAI,GAAG,CAAS,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC;IAE7D,sFAAsF;IACtF,2FAA2F;IA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AAC"}
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export { createApiClient, type ApiClient, type ApiClientOptions, type RefreshRes
4
4
  export { createSseParser, readSseStream, type SseFrame } from "./sse.ts";
5
5
  export { queryDefaults, retryDelayMs, shouldRetry, type QueryDefaultsOptions } from "./query.ts";
6
6
  export { ErrorBoundary, type ErrorBoundaryFallbackProps, type ErrorBoundaryProps, } from "./error-boundary.tsx";
7
- export { createErrorDescriber, humanizeWait, type DescribedError, type ErrorArm, type ErrorContext, type ErrorDescriberOptions, type Translate, } from "./describe-error.ts";
7
+ export { createErrorDescriber, humanizeWait, type DescribedError, type ErrorArm, type ErrorContext, type ErrorCopy, type ErrorDescriberOptions, type RecoveryKind, type Translate, } from "./describe-error.ts";
8
8
  export { installPreloadErrorHandler, isChunkLoadError, isPreloadHintFailure, isStaleBuild, RELOAD_GUARD_KEY, reloadOnce, } from "./deploy-recovery.ts";
9
9
  export { PRERENDERED_ROUTE_ATTR, SHELL_ROUTE } from "./prerender-contract.ts";
10
10
  export { applyBrandVars, i18nInitOptions, type I18nInitOptions } from "./i18n.ts";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA6BA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAE7B,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACxE,OAAO,EACL,eAAe,EACf,KAAK,SAAS,EACd,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,GACpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,WAAW,EAAE,KAAK,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEjG,OAAO,EACL,aAAa,EACb,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AsB,CAAC;AAC9B,OAAO,EACL,oBAAoB,EACpB,YAAY,GAQb,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,0BAA0B,EAC1B,gBAAgB,EAChB,oBAAoB,EACpB,YAAY,EACZ,gBAAgB,EAChB,UAAU,GACX,MAAM,sBAAsB,CAAC;AAE9B,iGAAiG;AACjG,kGAAkG;AAClG,6FAA6F;AAC7F,OAAO,EAAE,sBAAsB,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAE9E,OAAO,EAAE,cAAc,EAAE,eAAe,EAAwB,MAAM,WAAW,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gusnips/react",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "The headless runtime under a Vite + React SPA: typed fetch client with single-flight refresh, auth store, query defaults, route guards, error boundary, stale-tab recovery, i18n bootstrap.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -1,6 +1,7 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import { ApiError } from "./api-error.ts";
3
3
  import { createErrorDescriber, humanizeWait } from "./describe-error.ts";
4
+ import { shouldRetry } from "./query.ts";
4
5
 
5
6
  /** A translator that echoes the key, so a test can see exactly which one was asked for. */
6
7
  const t = (key: string, params?: Record<string, unknown>) =>
@@ -94,6 +95,86 @@ describe("createErrorDescriber", () => {
94
95
  });
95
96
  });
96
97
 
98
+ /**
99
+ * The control to offer beside the words. The fourth adopter had this and the package did not, so
100
+ * every adopter re-derived "can a second attempt fix this" from its own switch — beside a
101
+ * `shouldRetry` already answering exactly that for react-query.
102
+ */
103
+ describe("the recovery kind", () => {
104
+ const describeError = createErrorDescriber({
105
+ t,
106
+ copyPrefix: "errors.",
107
+ messageKeyPrefix: "serverErrors.",
108
+ knownMessageKeys: { quotaDay: "" },
109
+ durableLimitCodes: ["QUOTA_EXCEEDED"],
110
+ });
111
+
112
+ it("sends a 401 to sign-in and leaves a 403 alone", () => {
113
+ expect(describeError(new ApiError(401, null)).recover).toBe("signin");
114
+ // 403 is not a dead session: the session is fine and a different account is the fix, so
115
+ // signing someone out would be the wrong cause AND the wrong remedy.
116
+ expect(describeError(new ApiError(403, null)).recover).toBe("none");
117
+ });
118
+
119
+ it("offers a retry for a request that never landed", () => {
120
+ expect(describeError(new TypeError("Failed to fetch")).recover).toBe("retry");
121
+ });
122
+
123
+ it("counts down when the refusal stated its own expiry", () => {
124
+ const error = new ApiError(
125
+ 429,
126
+ { code: "RATE_LIMIT_EXCEEDED", message: "slow down" },
127
+ { retryAfterSecs: 30 },
128
+ );
129
+ expect(describeError(error).recover).toBe("wait");
130
+ });
131
+
132
+ it("offers no control for a limit that buying clears rather than waiting", () => {
133
+ // The same 429 as above. Only the CODE separates a burst limit from a spent quota, which is
134
+ // why the caller names them — from the same list it hands `queryDefaults`.
135
+ const spent = new ApiError(429, { code: "QUOTA_EXCEEDED", message: "spent" });
136
+ expect(describeError(spent).recover).toBe("none");
137
+ });
138
+
139
+ it("never asks anyone to retry the 401 the client raised itself", () => {
140
+ // `expected` is the sign-out path: nothing is broken and the person is already on their way
141
+ // to the door. A retry button here is the surface contradicting what it reports.
142
+ expect(describeError(new ApiError(401, null, { expected: true })).recover).toBe("none");
143
+ });
144
+
145
+ it("agrees with the retry rule instead of deciding the same thing twice", () => {
146
+ // The whole reason this derives rather than switches: if the two ever disagree, a reader
147
+ // presses a button the query layer has already refused to honour.
148
+ const error = new ApiError(503, null);
149
+ expect(shouldRetry(error)).toBe(true);
150
+ expect(describeError(error).recover).toBe("retry");
151
+ });
152
+
153
+ it("carries the request id the error contract reserves a slot for", () => {
154
+ const error = new ApiError(500, null, { requestId: "req_123" });
155
+ expect(describeError(error).reference).toBe("req_123");
156
+ });
157
+
158
+ it("lets an arm narrow the derived kind without restating it", () => {
159
+ const describe503 = createErrorDescriber({
160
+ t,
161
+ copyPrefix: "errors.",
162
+ messageKeyPrefix: "serverErrors.",
163
+ knownMessageKeys: {},
164
+ codes: {
165
+ MAINTENANCE: (ctx) => {
166
+ // The arm reads what the rule decided, so an arm that agrees can stay silent.
167
+ expect(ctx.recover).toBe("retry");
168
+ return { cause: "errors.maintenance", recover: "none" };
169
+ },
170
+ },
171
+ });
172
+ const out = describe503(new ApiError(503, { code: "MAINTENANCE", message: "back soon" }));
173
+ expect(out.recover).toBe("none");
174
+ expect(out.cause).toBe("errors.maintenance");
175
+ });
176
+ });
177
+
97
178
  /**
98
179
  * The second convention in the fleet: an API with no `messageKey` field at all, whose CODE
99
180
  * names the sentence. One repo has 98 of them — `errors.FORECAST_NOT_FOUND` and its siblings —
@@ -1,4 +1,5 @@
1
1
  import { ApiError, retryAfterSecs } from "./api-error.ts";
2
+ import { shouldRetry } from "./query.ts";
2
3
 
3
4
  /**
4
5
  * Turning anything thrown into copy a person can act on.
@@ -19,11 +20,42 @@ import { ApiError, retryAfterSecs } from "./api-error.ts";
19
20
  * which is the part that was written twice and got subtly different both times.
20
21
  */
21
22
 
22
- export interface DescribedError {
23
+ /**
24
+ * Which control to offer beside the words.
25
+ *
26
+ * The fourth adopter had this and the package did not, which left every adopter re-deriving
27
+ * "can a second attempt fix this" from its own switch — beside a `shouldRetry` already deciding
28
+ * exactly that for react-query. Two answers to one question drift, and the drift is visible: a
29
+ * screen offering "try again" for a refusal the query layer refuses to retry, so the button does
30
+ * nothing and the reader presses it twice.
31
+ *
32
+ * So this is DERIVED from `shouldRetry`, not switched on separately. One rule, two consumers.
33
+ */
34
+ export type RecoveryKind =
35
+ /** A second attempt can work. Offer the button. */
36
+ | "retry"
37
+ /** Auth said no. Offer a way back to sign-in, never a retry. */
38
+ | "signin"
39
+ /** It stated its own expiry. Offer the button, disabled, with the countdown. */
40
+ | "wait"
41
+ /** Waiting cannot fix it and neither can pressing anything. Offer no control. */
42
+ | "none";
43
+
44
+ /** The copy an arm writes. Everything it leaves out, {@link createErrorDescriber} fills in. */
45
+ export interface ErrorCopy {
23
46
  /** What happened, in the reader's words. */
24
47
  cause: string;
25
48
  /** What to do about it. Its absence should be a decision, not an oversight. */
26
49
  hint?: string;
50
+ /** Override the derived kind — a 429 whose code means a spent quota is `"none"`, not `"wait"`. */
51
+ recover?: RecoveryKind;
52
+ /** Override the request id. Rarely wanted; the envelope's is used by default. */
53
+ reference?: string;
54
+ }
55
+
56
+ export interface DescribedError extends ErrorCopy {
57
+ /** Always present on the way out, whether an arm set it or the rule derived it. */
58
+ recover: RecoveryKind;
27
59
  }
28
60
 
29
61
  /**
@@ -100,6 +132,13 @@ export interface ErrorContext {
100
132
  /** Seconds until it clears, already humanized. Null when the envelope did not say. */
101
133
  wait: string | null;
102
134
  waitSecs: number | null;
135
+ /**
136
+ * What the rule decided before this arm ran. Read it to agree, return your own to override.
137
+ *
138
+ * Exposed so an arm can narrow rather than restate: a quota arm that already knows the limit is
139
+ * durable returns `"none"`, and every other arm can leave it alone and stay right by default.
140
+ */
141
+ recover: RecoveryKind;
103
142
  }
104
143
 
105
144
  export interface ErrorDescriberOptions<
@@ -142,9 +181,17 @@ export interface ErrorDescriberOptions<
142
181
  * code that does not exist is a compile error at the arm rather than a branch that never runs.
143
182
  */
144
183
  codes?: Partial<Record<string, ErrorArm>>;
184
+ /**
185
+ * The codes that mean "this limit does not clear by waiting" — **the same list you hand
186
+ * `queryDefaults`**, because it answers the same question and a second copy is a second
187
+ * chance to disagree with the retry rule. Declare it once in the app and pass it twice.
188
+ */
189
+ durableLimitCodes?: readonly string[];
190
+ /** Longest stated wait still worth a retry rather than a countdown. Matches `queryDefaults`. */
191
+ maxRetryWaitSecs?: number;
145
192
  }
146
193
 
147
- export type ErrorArm = (ctx: ErrorContext) => DescribedError;
194
+ export type ErrorArm = (ctx: ErrorContext) => ErrorCopy;
148
195
 
149
196
  /** Build the describer. */
150
197
  export function createErrorDescriber<
@@ -157,6 +204,8 @@ export function createErrorDescriber<
157
204
  messageKeyPrefix,
158
205
  knownMessageKeys,
159
206
  codes = {},
207
+ durableLimitCodes = [],
208
+ maxRetryWaitSecs,
160
209
  }: ErrorDescriberOptions<Prefix, ServerPrefix, ServerName>): (error: unknown) => DescribedError {
161
210
  const known = new Set<string>(Object.keys(knownMessageKeys));
162
211
 
@@ -180,11 +229,35 @@ export function createErrorDescriber<
180
229
  );
181
230
  }
182
231
 
232
+ /**
233
+ * Which control to offer, decided once and never switched on separately.
234
+ *
235
+ * Everything below the last line defers to `shouldRetry` — the rule invariant 8 already owns —
236
+ * so the button a reader sees and the retry react-query performs cannot disagree.
237
+ */
238
+ function recoveryFor(error: ApiError, waitSecs: number | null): RecoveryKind {
239
+ // The client raised this one itself, to stop a caller while a dead session was already being
240
+ // handled. Nothing is broken and the person is already on their way to sign-in, so asking
241
+ // them to try again would be the surface contradicting the thing it is reporting.
242
+ if (error.expected) return "none";
243
+ // 401 is "we do not know who you are", which no number of attempts answers. 403 is NOT this:
244
+ // the session is fine and it is a different account that would help, so it falls through.
245
+ if (error.status === 401) return "signin";
246
+ // It named its own expiry, so there is a real number to count down beside a disabled button.
247
+ if (waitSecs !== null) return "wait";
248
+ return shouldRetry(error, durableLimitCodes, maxRetryWaitSecs) ? "retry" : "none";
249
+ }
250
+
183
251
  return function describeError(error: unknown): DescribedError {
184
252
  if (!(error instanceof ApiError)) {
185
253
  // Not a response at all — the request never landed. Almost always the network, and almost
186
- // never worth showing a stack trace for.
187
- return { cause: t(`${copyPrefix}network`), hint: t(`${copyPrefix}networkHint`) };
254
+ // never worth showing a stack trace for. `shouldRetry` answers a non-response the same way,
255
+ // so these two agree here without either being told about the other.
256
+ return {
257
+ cause: t(`${copyPrefix}network`),
258
+ hint: t(`${copyPrefix}networkHint`),
259
+ recover: "retry",
260
+ };
188
261
  }
189
262
 
190
263
  const waitSecs = retryAfterSecs(error);
@@ -193,11 +266,9 @@ export function createErrorDescriber<
193
266
  says: serverSentence(error),
194
267
  wait: waitSecs === null ? null : humanizeWait(t, waitSecs, copyPrefix),
195
268
  waitSecs,
269
+ recover: recoveryFor(error, waitSecs),
196
270
  };
197
271
 
198
- const arm = error.code === undefined ? undefined : codes[error.code];
199
- if (arm) return arm(ctx);
200
-
201
272
  // Anything unmapped: the server's own sentence is still the most specific thing we have,
202
273
  // and support can act on it. A 5xx additionally gets "try again shortly", because that one
203
274
  // genuinely does clear on its own — a 4xx does not, and saying so would be a lie that costs
@@ -207,10 +278,24 @@ export function createErrorDescriber<
207
278
  // own `Request failed (502)`, written for a log. A gateway answering with HTML while the API
208
279
  // restarts is exactly when that happens, so it reached screens — until the second migration,
209
280
  // whose three apps each guarded against that one string by hand.
210
- const serverWords = error.code === undefined ? "" : error.message;
281
+ const arm = error.code === undefined ? undefined : codes[error.code];
282
+ const copy: ErrorCopy = arm
283
+ ? arm(ctx)
284
+ : {
285
+ cause:
286
+ ctx.says ??
287
+ ((error.code === undefined ? "" : error.message) || t(`${copyPrefix}unexpected`)),
288
+ hint: error.status >= 500 ? t(`${copyPrefix}retrySoon`) : undefined,
289
+ };
290
+
291
+ // The arm wins where it spoke, the rule fills the rest. `requestId` is the one thing every
292
+ // error surface asks for and nothing was producing — `ErrorStateProps` has reserved a
293
+ // `reference` slot for it since the contract was written.
294
+ const reference = copy.reference ?? error.requestId;
211
295
  return {
212
- cause: ctx.says ?? (serverWords || t(`${copyPrefix}unexpected`)),
213
- hint: error.status >= 500 ? t(`${copyPrefix}retrySoon`) : undefined,
296
+ ...copy,
297
+ recover: copy.recover ?? ctx.recover,
298
+ ...(reference !== undefined && { reference }),
214
299
  };
215
300
  };
216
301
  }
package/src/index.ts CHANGED
@@ -53,7 +53,9 @@ export {
53
53
  type DescribedError,
54
54
  type ErrorArm,
55
55
  type ErrorContext,
56
+ type ErrorCopy,
56
57
  type ErrorDescriberOptions,
58
+ type RecoveryKind,
57
59
  type Translate,
58
60
  } from "./describe-error.ts";
59
61