@fuzdev/fuz_app 0.163.0 → 0.164.0

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.
Files changed (181) hide show
  1. package/README.md +2 -1
  2. package/dist/actions/action_event_data.d.ts +1 -1
  3. package/dist/actions/action_rpc.d.ts.map +1 -1
  4. package/dist/actions/action_rpc.js +4 -0
  5. package/dist/actions/action_spec.d.ts +3 -3
  6. package/dist/actions/action_spec.d.ts.map +1 -1
  7. package/dist/auth/CLAUDE.md +1 -1
  8. package/dist/auth/account_route_schema.d.ts +3 -27
  9. package/dist/auth/account_route_schema.d.ts.map +1 -1
  10. package/dist/auth/account_route_schema.js +6 -20
  11. package/dist/auth/admin_action_specs.d.ts +56 -56
  12. package/dist/auth/audit_log_route_schema.d.ts +1 -1
  13. package/dist/auth/audit_log_route_schema.d.ts.map +1 -1
  14. package/dist/auth/audit_log_route_schema.js +4 -5
  15. package/dist/auth/audit_log_schema.d.ts +18 -18
  16. package/dist/auth/bootstrap_route_schema.d.ts +1 -30
  17. package/dist/auth/bootstrap_route_schema.d.ts.map +1 -1
  18. package/dist/auth/bootstrap_route_schema.js +3 -8
  19. package/dist/auth/daemon_token.d.ts +4 -4
  20. package/dist/auth/daemon_token.d.ts.map +1 -1
  21. package/dist/auth/daemon_token.js +3 -3
  22. package/dist/auth/daemon_token_middleware.d.ts +1 -1
  23. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  24. package/dist/auth/daemon_token_middleware.js +5 -5
  25. package/dist/auth/middleware.js +4 -4
  26. package/dist/auth/role_grant_offer_action_specs.d.ts +2 -2
  27. package/dist/auth/signup_route_schema.d.ts.map +1 -1
  28. package/dist/auth/signup_route_schema.js +3 -7
  29. package/dist/db/CLAUDE.md +2 -1
  30. package/dist/db/file_fact_url.d.ts +11 -6
  31. package/dist/db/file_fact_url.d.ts.map +1 -1
  32. package/dist/db/file_fact_url.js +5 -5
  33. package/dist/db/schema_ready.d.ts +0 -5
  34. package/dist/db/schema_ready.d.ts.map +1 -1
  35. package/dist/db/schema_ready.js +0 -5
  36. package/dist/http/CLAUDE.md +104 -71
  37. package/dist/http/auth_shape.d.ts +48 -29
  38. package/dist/http/auth_shape.d.ts.map +1 -1
  39. package/dist/http/auth_shape.js +12 -85
  40. package/dist/http/body_limit.d.ts +12 -0
  41. package/dist/http/body_limit.d.ts.map +1 -1
  42. package/dist/http/body_limit.js +15 -0
  43. package/dist/http/body_limit_schema.d.ts +20 -0
  44. package/dist/http/body_limit_schema.d.ts.map +1 -0
  45. package/dist/http/body_limit_schema.js +20 -0
  46. package/dist/http/common_routes.d.ts +1 -1
  47. package/dist/http/common_routes.d.ts.map +1 -1
  48. package/dist/http/common_routes.js +11 -14
  49. package/dist/http/db_routes.d.ts.map +1 -1
  50. package/dist/http/db_routes.js +59 -69
  51. package/dist/http/dev_route_error_check.d.ts +70 -0
  52. package/dist/http/dev_route_error_check.d.ts.map +1 -0
  53. package/dist/http/dev_route_error_check.js +119 -0
  54. package/dist/http/error_schemas.d.ts +365 -189
  55. package/dist/http/error_schemas.d.ts.map +1 -1
  56. package/dist/http/error_schemas.js +256 -149
  57. package/dist/http/health_schema.d.ts +10 -6
  58. package/dist/http/health_schema.d.ts.map +1 -1
  59. package/dist/http/health_schema.js +3 -3
  60. package/dist/http/middleware_spec.d.ts +8 -3
  61. package/dist/http/middleware_spec.d.ts.map +1 -1
  62. package/dist/http/origin.d.ts +12 -43
  63. package/dist/http/origin.d.ts.map +1 -1
  64. package/dist/http/origin.js +10 -6
  65. package/dist/http/route_error_check.d.ts +158 -0
  66. package/dist/http/route_error_check.d.ts.map +1 -0
  67. package/dist/http/route_error_check.js +247 -0
  68. package/dist/http/route_errors.d.ts +80 -0
  69. package/dist/http/route_errors.d.ts.map +1 -0
  70. package/dist/http/route_errors.js +83 -0
  71. package/dist/http/route_spec.d.ts +33 -17
  72. package/dist/http/route_spec.d.ts.map +1 -1
  73. package/dist/http/route_spec.js +48 -62
  74. package/dist/http/schema_helpers.d.ts +0 -33
  75. package/dist/http/schema_helpers.d.ts.map +1 -1
  76. package/dist/http/schema_helpers.js +0 -45
  77. package/dist/http/surface.d.ts +19 -12
  78. package/dist/http/surface.d.ts.map +1 -1
  79. package/dist/http/surface.js +25 -38
  80. package/dist/http/surface_query.d.ts +11 -2
  81. package/dist/http/surface_query.d.ts.map +1 -1
  82. package/dist/http/surface_query.js +7 -1
  83. package/dist/primitive_schemas_fuz.d.ts +17 -3
  84. package/dist/primitive_schemas_fuz.d.ts.map +1 -1
  85. package/dist/primitive_schemas_fuz.js +13 -3
  86. package/dist/rate_limiter.d.ts +13 -0
  87. package/dist/rate_limiter.d.ts.map +1 -1
  88. package/dist/rate_limiter.js +20 -2
  89. package/dist/schema_issues.d.ts +29 -11
  90. package/dist/schema_issues.d.ts.map +1 -1
  91. package/dist/schema_issues.js +11 -10
  92. package/dist/schema_meta.d.ts +8 -1
  93. package/dist/schema_meta.d.ts.map +1 -1
  94. package/dist/schema_meta.js +2 -1
  95. package/dist/server/app_server.d.ts +3 -1
  96. package/dist/server/app_server.d.ts.map +1 -1
  97. package/dist/server/app_server.js +24 -10
  98. package/dist/server/serve_fact_route.d.ts.map +1 -1
  99. package/dist/server/serve_fact_route.js +8 -31
  100. package/dist/server/surface_route.d.ts.map +1 -1
  101. package/dist/server/surface_route.js +5 -6
  102. package/dist/server/x_accel.d.ts +12 -7
  103. package/dist/server/x_accel.d.ts.map +1 -1
  104. package/dist/server/x_accel.js +7 -9
  105. package/dist/testing/CLAUDE.md +33 -39
  106. package/dist/testing/admin_integration.js +1 -1
  107. package/dist/testing/adversarial_404.d.ts +4 -18
  108. package/dist/testing/adversarial_404.d.ts.map +1 -1
  109. package/dist/testing/adversarial_404.js +19 -51
  110. package/dist/testing/adversarial_headers.d.ts +3 -4
  111. package/dist/testing/adversarial_headers.d.ts.map +1 -1
  112. package/dist/testing/adversarial_headers.js +8 -6
  113. package/dist/testing/adversarial_input.d.ts.map +1 -1
  114. package/dist/testing/adversarial_input.js +18 -17
  115. package/dist/testing/assertions.d.ts +33 -11
  116. package/dist/testing/assertions.d.ts.map +1 -1
  117. package/dist/testing/assertions.js +52 -12
  118. package/dist/testing/attack_surface.d.ts +5 -35
  119. package/dist/testing/attack_surface.d.ts.map +1 -1
  120. package/dist/testing/attack_surface.js +29 -91
  121. package/dist/testing/auth_apps.d.ts.map +1 -1
  122. package/dist/testing/auth_apps.js +2 -1
  123. package/dist/testing/cross_backend/action_manifest.d.ts +126 -56
  124. package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
  125. package/dist/testing/cross_backend/action_manifest.js +11 -11
  126. package/dist/testing/cross_backend/action_manifest_parity.d.ts +1 -1
  127. package/dist/testing/cross_backend/action_manifest_parity.d.ts.map +1 -1
  128. package/dist/testing/cross_backend/cell_crud.d.ts.map +1 -1
  129. package/dist/testing/cross_backend/cell_crud.js +2 -1
  130. package/dist/testing/cross_backend/conformance_table.js +1 -1
  131. package/dist/testing/cross_backend/fact_serving.js +3 -3
  132. package/dist/testing/cross_backend/origin_conformance.d.ts +278 -102
  133. package/dist/testing/cross_backend/origin_conformance.d.ts.map +1 -1
  134. package/dist/testing/cross_backend/origin_conformance.js +21 -21
  135. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
  136. package/dist/testing/cross_backend/rust_spine_consumer_surface.js +7 -1
  137. package/dist/testing/cross_backend/setup.d.ts +13 -0
  138. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  139. package/dist/testing/cross_backend/setup.js +24 -7
  140. package/dist/testing/cross_backend/testing_origin_check.d.ts +147 -71
  141. package/dist/testing/cross_backend/testing_origin_check.d.ts.map +1 -1
  142. package/dist/testing/cross_backend/testing_origin_check.js +16 -13
  143. package/dist/testing/cross_backend/testing_reset_actions.d.ts +70 -27
  144. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  145. package/dist/testing/data_exposure.d.ts.map +1 -1
  146. package/dist/testing/data_exposure.js +5 -4
  147. package/dist/testing/error_coverage.d.ts +24 -34
  148. package/dist/testing/error_coverage.d.ts.map +1 -1
  149. package/dist/testing/error_coverage.js +46 -89
  150. package/dist/testing/integration.d.ts.map +1 -1
  151. package/dist/testing/integration.js +5 -13
  152. package/dist/testing/integration_helpers.d.ts +37 -9
  153. package/dist/testing/integration_helpers.d.ts.map +1 -1
  154. package/dist/testing/integration_helpers.js +86 -40
  155. package/dist/testing/middleware.d.ts +3 -5
  156. package/dist/testing/middleware.d.ts.map +1 -1
  157. package/dist/testing/middleware.js +5 -4
  158. package/dist/testing/rate_limiting.d.ts +1 -1
  159. package/dist/testing/rate_limiting.d.ts.map +1 -1
  160. package/dist/testing/rate_limiting.js +5 -4
  161. package/dist/testing/round_trip.d.ts.map +1 -1
  162. package/dist/testing/round_trip.js +5 -3
  163. package/dist/testing/schema_generators.d.ts +10 -3
  164. package/dist/testing/schema_generators.d.ts.map +1 -1
  165. package/dist/testing/schema_generators.js +23 -11
  166. package/dist/testing/stubs.d.ts +10 -1
  167. package/dist/testing/stubs.d.ts.map +1 -1
  168. package/dist/testing/stubs.js +15 -5
  169. package/dist/testing/surface_invariants.d.ts +36 -115
  170. package/dist/testing/surface_invariants.d.ts.map +1 -1
  171. package/dist/testing/surface_invariants.js +106 -269
  172. package/dist/ui/CLAUDE.md +4 -2
  173. package/dist/ui/auth_state.svelte.d.ts.map +1 -1
  174. package/dist/ui/auth_state.svelte.js +2 -8
  175. package/dist/ui/backend_probe.svelte.js +3 -3
  176. package/dist/ui/table_state.svelte.d.ts.map +1 -1
  177. package/dist/ui/table_state.svelte.js +1 -8
  178. package/dist/ui/ui_fetch.d.ts +12 -2
  179. package/dist/ui/ui_fetch.d.ts.map +1 -1
  180. package/dist/ui/ui_fetch.js +22 -4
  181. package/package.json +1 -1
@@ -4,8 +4,8 @@ import './assert_dev_env.js';
4
4
  *
5
5
  * Two categories:
6
6
  * - **Structural** — validate internal consistency (error schema presence,
7
- * descriptions, duplicates, middleware propagation, error schema validity,
8
- * error code consistency). No options needed.
7
+ * descriptions, duplicates, middleware propagation, every error naming a
8
+ * code, error code consistency). No options needed.
9
9
  * - **Policy** — enforce security policy (sensitive routes rate-limited,
10
10
  * no public mutations, mutation method conventions). Configurable with
11
11
  * sensible defaults.
@@ -20,6 +20,7 @@ import { assert } from 'vitest';
20
20
  import { middleware_spec_applies } from '../http/schema_helpers.js';
21
21
  import { filter_protected_routes, filter_role_routes, filter_keeper_routes, filter_credential_gated_routes, filter_routes_with_input, filter_routes_with_params, filter_routes_with_query, filter_public_routes, format_route_key } from '../http/surface_query.js';
22
22
  import { PROTOCOL_ACTION_METHODS } from '../actions/action_codegen.js';
23
+ import { ERROR_INVALID_QUERY_PARAMS, ERROR_INVALID_REQUEST_BODY, ERROR_INVALID_ROUTE_PARAMS, REST_ERROR_CODE_PATTERN, is_rest_error_code } from '../http/error_schemas.js';
23
24
  // --- Structural invariants ---
24
25
  /**
25
26
  * Every protected route has 401 in `error_schemas`.
@@ -47,31 +48,34 @@ export const assert_role_routes_declare_403 = (surface) => {
47
48
  }
48
49
  };
49
50
  /**
50
- * Every route with non-null `input_schema` has 400 in `error_schemas`.
51
+ * Assert each route declares a 400 naming `code` among its branches — the
52
+ * code its input middleware answers when it refuses the slot.
51
53
  */
52
- export const assert_input_routes_declare_400 = (surface) => {
53
- const routes = filter_routes_with_input(surface);
54
+ const assert_routes_declare_400_code = (routes, code, slot) => {
54
55
  for (const route of routes) {
55
- assert.ok(route.error_schemas && '400' in route.error_schemas, `${format_route_key(route)} has input but missing 400 error schema`);
56
+ const declared = route.error_schemas?.['400'];
57
+ assert.ok(declared, `${format_route_key(route)} has ${slot} but missing 400 error schema`);
58
+ assert.ok(extract_error_consts(declared).includes(code), `${format_route_key(route)} has ${slot} but its 400 error schema does not name ${code}`);
56
59
  }
57
60
  };
58
61
  /**
59
- * Every route with non-null `params_schema` has 400 in `error_schemas`.
62
+ * Every route with a body — a non-null `input_schema` on a method other than
63
+ * `GET`, which reads no body — declares a 400 naming `invalid_request_body`.
64
+ */
65
+ export const assert_input_routes_declare_400 = (surface) => {
66
+ assert_routes_declare_400_code(filter_routes_with_input(surface).filter((route) => route.method !== 'GET'), ERROR_INVALID_REQUEST_BODY, 'input');
67
+ };
68
+ /**
69
+ * Every route with non-null `params_schema` declares a 400 naming `invalid_route_params`.
60
70
  */
61
71
  export const assert_params_routes_declare_400 = (surface) => {
62
- const routes = filter_routes_with_params(surface);
63
- for (const route of routes) {
64
- assert.ok(route.error_schemas && '400' in route.error_schemas, `${format_route_key(route)} has params but missing 400 error schema`);
65
- }
72
+ assert_routes_declare_400_code(filter_routes_with_params(surface), ERROR_INVALID_ROUTE_PARAMS, 'params');
66
73
  };
67
74
  /**
68
- * Every route with non-null `query_schema` has 400 in `error_schemas`.
75
+ * Every route with non-null `query_schema` declares a 400 naming `invalid_query_params`.
69
76
  */
70
77
  export const assert_query_routes_declare_400 = (surface) => {
71
- const routes = filter_routes_with_query(surface);
72
- for (const route of routes) {
73
- assert.ok(route.error_schemas && '400' in route.error_schemas, `${format_route_key(route)} has query schema but missing 400 error schema`);
74
- }
78
+ assert_routes_declare_400_code(filter_routes_with_query(surface), ERROR_INVALID_QUERY_PARAMS, 'query schema');
75
79
  };
76
80
  /**
77
81
  * Every route has a non-empty description.
@@ -95,6 +99,11 @@ export const assert_no_duplicate_routes = (surface) => {
95
99
  /**
96
100
  * Every applicable middleware that declares errors must have those status codes
97
101
  * present in the route's `error_schemas`.
102
+ *
103
+ * Holds by construction for a surface `generate_app_surface` builds: each
104
+ * route's error map (`resolve_route_error_map`) already unions every applying
105
+ * layer's definitions. It earns its place only on a surface built another
106
+ * way — a committed snapshot read back, or one written by hand.
98
107
  */
99
108
  export const assert_middleware_errors_propagated = (surface) => {
100
109
  const middleware_with_errors = surface.middleware.filter((m) => m.error_schemas !== null);
@@ -109,55 +118,84 @@ export const assert_middleware_errors_propagated = (surface) => {
109
118
  }
110
119
  };
111
120
  /**
112
- * Every route's declared error schemas must have an `error` field at the top level
113
- * (conforming to the `ApiError` base shape `{error: string}`).
121
+ * Every error a route or middleware layer declares names its code exactly:
122
+ * each body at each status (each branch of an `anyOf` or `oneOf`) is an
123
+ * object whose `error` is a `const` or an `enum` — what a definition built by
124
+ * `rest_error` writes. A bare string `error`, or a body with no `error`, is a
125
+ * contract a client can't key on.
114
126
  *
115
- * Walks union branches (`anyOf` from `z.union`, `oneOf` from
116
- * `z.discriminatedUnion`) so every emit shape inside a merged 400 / 404
117
- * is checked, not just the top-level wrapper.
127
+ * @throws AssertionError naming the route or layer, the status, and the body
128
+ */
129
+ export const assert_error_codes_specific = (surface) => {
130
+ for (const { label, status, branch } of each_error_branch(surface)) {
131
+ const error_prop = get_error_property(branch);
132
+ assert.ok(error_prop && (typeof error_prop.const === 'string' || Array.isArray(error_prop.enum)), `${label} error schema for status ${status} does not name its code — ` +
133
+ `declare it with rest_error: ${JSON.stringify(branch)}`);
134
+ }
135
+ };
136
+ /**
137
+ * Every declared `error` code is a code — `REST_ERROR_CODE_PATTERN`, a
138
+ * lowercase letter then lowercase letters, digits, and `_` — never prose,
139
+ * which belongs in `message`. `rest_error` refuses one off the pattern; this
140
+ * holds a surface built another way (a snapshot, a hand-written layer) to the
141
+ * same rule.
118
142
  *
119
- * Catches typos in error schema definitions and ensures consumers can always
120
- * read `.error` from error responses.
143
+ * @throws AssertionError naming the route or layer, the status, and the code
121
144
  */
122
- export const assert_error_schemas_structurally_valid = (surface) => {
145
+ export const assert_error_codes_well_formed = (surface) => {
146
+ for (const { label, status, branch } of each_error_branch(surface)) {
147
+ const error_prop = get_error_property(branch);
148
+ if (!error_prop)
149
+ continue;
150
+ const codes = error_prop.const !== undefined
151
+ ? [error_prop.const]
152
+ : Array.isArray(error_prop.enum)
153
+ ? error_prop.enum
154
+ : [];
155
+ for (const code of codes) {
156
+ assert.ok(is_rest_error_code(code), `${label} error code ${JSON.stringify(code)} at status ${status} is not a code ` +
157
+ `(${REST_ERROR_CODE_PATTERN.source}) — put prose in \`message\``);
158
+ }
159
+ }
160
+ };
161
+ /** Every body every route and middleware layer declares, one per union branch. */
162
+ const each_error_branch = (surface) => {
163
+ const out = [];
164
+ const visit = (label, status, schema) => {
165
+ const branches = get_union_branches(schema);
166
+ if (branches) {
167
+ for (const branch of branches)
168
+ visit(label, status, branch);
169
+ return;
170
+ }
171
+ out.push({ label, status, branch: schema });
172
+ };
123
173
  for (const route of surface.routes) {
124
174
  if (!route.error_schemas)
125
175
  continue;
126
176
  for (const [status, schema] of Object.entries(route.error_schemas)) {
127
- assert_branch_has_error_property(schema, format_route_key(route), status);
177
+ visit(format_route_key(route), status, schema);
128
178
  }
129
179
  }
130
- };
131
- const assert_branch_has_error_property = (schema, route_key, status) => {
132
- const branches = get_union_branches(schema);
133
- if (branches) {
134
- for (const branch of branches) {
135
- assert_branch_has_error_property(branch, route_key, status);
180
+ for (const mw of surface.middleware) {
181
+ if (!mw.error_schemas)
182
+ continue;
183
+ for (const [status, schema] of Object.entries(mw.error_schemas)) {
184
+ visit(`middleware '${mw.name}'`, status, schema);
136
185
  }
137
- return;
138
- }
139
- if (typeof schema !== 'object' || schema === null)
140
- return;
141
- const s = schema;
142
- if (s.type === 'object' && s.properties && typeof s.properties === 'object') {
143
- const props = s.properties;
144
- assert.ok('error' in props, `${route_key} error schema for status ${status} missing 'error' property`);
145
186
  }
187
+ return out;
146
188
  };
147
189
  /**
148
- * The same `z.literal()` error code should not appear at different HTTP status codes
149
- * across routes.
150
- *
151
- * Extracts `const` values from error schema `error` properties (which correspond to
152
- * `z.literal()` in the Zod source). Walks union branches (`anyOf` from `z.union`,
153
- * `oneOf` from `z.discriminatedUnion`) so literal codes nested inside merged unions
154
- * (e.g. validation 400 + actor-resolution 400) are still tracked. Flags when the
155
- * same literal appears at different status codes — e.g., `ERROR_INVALID_CREDENTIALS`
156
- * at both 401 and 403 would be a bug.
190
+ * One error code at one HTTP status: the same code declared at two statuses
191
+ * across the surface's routes and middleware layers fails — e.g.
192
+ * `invalid_credentials` at both 401 and 403 would be a bug.
157
193
  *
158
- * Only checks `const` values (literal schemas). Generic `z.string()` schemas
159
- * (which produce `{type: 'string'}`) and `z.enum()` schemas are ignored — the
160
- * literal-only narrow keeps the check unambiguous.
194
+ * Reads each body's `error` `const` (what `rest_error` writes), through every
195
+ * branch of an `anyOf` or `oneOf` — the surface writes a status with several
196
+ * definitions as an `anyOf` — and nested unions within them. An `error` with
197
+ * no `const` (an `enum`, a bare string) is not read; `assert_error_codes_specific`
198
+ * refuses the bare string.
161
199
  */
162
200
  export const assert_error_code_status_consistency = (surface) => {
163
201
  // Map from error code literal → Set of status codes where it appears
@@ -194,8 +232,8 @@ export const assert_error_code_status_consistency = (surface) => {
194
232
  /**
195
233
  * Extract the `error` property's JSON Schema from a route error schema.
196
234
  *
197
- * Navigates `schema.properties.error` — the common structure for all
198
- * `ApiError`-shaped schemas. Returns `null` if the schema doesn't conform.
235
+ * Navigates `schema.properties.error` — every error body is an object with
236
+ * an `error` code. Returns `null` if the schema doesn't conform.
199
237
  */
200
238
  const get_error_property = (schema) => {
201
239
  if (typeof schema !== 'object' || schema === null)
@@ -211,11 +249,11 @@ const get_error_property = (schema) => {
211
249
  /**
212
250
  * Read the branch array off a JSON Schema union, if present.
213
251
  *
214
- * Zod 4 emits `anyOf` for `z.union(...)` and `oneOf` for
215
- * `z.discriminatedUnion(...)` via `z.toJSONSchema`; both are union-shaped
216
- * for tightness/code-extraction purposes. Nested unions are NOT flattened
217
- * by `toJSONSchema`, so every caller must recurse through the returned
218
- * branches. Returns the branch array or `null` for non-union schemas.
252
+ * The surface writes a status with several definitions as an `anyOf`, and a
253
+ * `oneOf` from a JSON Schema written another way is read the same; both are
254
+ * union-shaped for code extraction. Nested unions are not flattened, so
255
+ * every caller must recurse through the returned branches. Returns the
256
+ * branch array or `null` for non-union schemas.
219
257
  */
220
258
  const get_union_branches = (schema) => {
221
259
  if (typeof schema !== 'object' || schema === null)
@@ -231,11 +269,11 @@ const get_union_branches = (schema) => {
231
269
  * Extract every `const` value from a JSON Schema error property, walking
232
270
  * union branches.
233
271
  *
234
- * Looks for `schema.properties.error.const` — the JSON Schema representation
235
- * of `z.literal('some_error_code')` — and recurses into `anyOf` / `oneOf`
236
- * branches so literals nested inside `z.union` or `z.discriminatedUnion`
237
- * are still tracked. Returns an empty array for schemas with no literal
238
- * codes (`z.enum`, `z.string`, non-object schemas).
272
+ * Looks for `schema.properties.error.const` — what a definition's
273
+ * `literal(code)` writes — and recurses into `anyOf` / `oneOf` branches, so
274
+ * a code in a status of several definitions is still tracked. Returns an
275
+ * empty array for schemas with no `const` code (an `enum`, a bare string,
276
+ * a non-object schema).
239
277
  */
240
278
  const extract_error_consts = (schema) => {
241
279
  const branches = get_union_branches(schema);
@@ -253,134 +291,6 @@ const extract_error_consts = (schema) => {
253
291
  return [error_prop.const];
254
292
  return [];
255
293
  };
256
- /**
257
- * Check if a JSON Schema error property uses specific error codes (`const` or `enum`),
258
- * not just generic `z.string()` (`{type: 'string'}`).
259
- *
260
- * Returns `true` for `z.literal()` (`{const: '...'}`) and `z.enum()` (`{enum: [...]}`),
261
- * and for union schemas where every branch is specific. Defers to
262
- * `classify_error_specificity` so the union walk stays in one place.
263
- */
264
- const has_specific_error_schema = (schema) => classify_error_specificity(schema) !== 'generic';
265
- /**
266
- * Routes declaring 404 error schemas should use specific `z.literal()` or `z.enum()`
267
- * error codes, not generic `z.string()`.
268
- *
269
- * A generic 404 schema (`ApiError` with `z.string()`) means the error code is
270
- * unconstrained — the handler could return any string, making client error handling
271
- * fragile. Routes with params (`:id`) are the primary 404 producers; their error
272
- * schemas should use specific constants like `ERROR_ACCOUNT_NOT_FOUND`.
273
- *
274
- * Only flags routes that have `params_schema` (param-driven resource lookup) — routes
275
- * declaring 404 for other reasons (e.g., bootstrap not configured) may legitimately
276
- * use generic schemas.
277
- */
278
- export const assert_404_schemas_use_specific_errors = (surface) => {
279
- for (const route of surface.routes) {
280
- if (!route.error_schemas || !('404' in route.error_schemas))
281
- continue;
282
- if (route.params_schema === null)
283
- continue;
284
- assert.ok(has_specific_error_schema(route.error_schemas['404']), `${format_route_key(route)} declares 404 with params but uses generic error schema — ` +
285
- `use a specific z.literal() or z.enum() error code`);
286
- }
287
- };
288
- /**
289
- * Classify the specificity of a JSON Schema error property.
290
- *
291
- * - `'literal'` — `z.literal()` (`{const: '...'}`)
292
- * - `'enum'` — `z.enum()` (`{enum: [...]}`)
293
- * - `'generic'` — `z.string()` or unrecognized
294
- *
295
- * Walks union branches (`anyOf` from `z.union`, `oneOf` from
296
- * `z.discriminatedUnion`) — `derive_error_schemas` emits `anyOf` when it
297
- * merges multiple shapes at one status (e.g. validation 400 +
298
- * actor-resolution 400), and a consumer that explicitly declares a
299
- * discriminated-union error schema emits `oneOf`. Reports the **minimum**
300
- * specificity across branches — a union's contract is only as tight as
301
- * its loosest member.
302
- */
303
- const classify_error_specificity = (schema) => {
304
- const branches = get_union_branches(schema);
305
- if (branches) {
306
- if (branches.length === 0)
307
- return 'generic';
308
- let min = 'literal';
309
- for (const branch of branches) {
310
- const branch_specificity = classify_error_specificity(branch);
311
- if (SPECIFICITY_ORDER[branch_specificity] < SPECIFICITY_ORDER[min]) {
312
- min = branch_specificity;
313
- }
314
- }
315
- return min;
316
- }
317
- const error_prop = get_error_property(schema);
318
- if (!error_prop)
319
- return 'generic';
320
- if (typeof error_prop.const === 'string')
321
- return 'literal';
322
- if (Array.isArray(error_prop.enum))
323
- return 'enum';
324
- return 'generic';
325
- };
326
- /**
327
- * Extract error code values from a JSON Schema error property.
328
- *
329
- * Returns the literal value or enum array, or `null` for generic schemas.
330
- *
331
- * For union schemas (`anyOf` / `oneOf`), collects codes from every branch
332
- * (deduped). If any branch is generic, returns `null` because the union
333
- * admits arbitrary strings on that branch.
334
- */
335
- const extract_error_codes = (schema) => {
336
- const branches = get_union_branches(schema);
337
- if (branches) {
338
- const codes = new Set();
339
- for (const branch of branches) {
340
- const branch_codes = extract_error_codes(branch);
341
- if (branch_codes === null)
342
- return null;
343
- for (const code of branch_codes)
344
- codes.add(code);
345
- }
346
- return [...codes];
347
- }
348
- const error_prop = get_error_property(schema);
349
- if (!error_prop)
350
- return null;
351
- if (typeof error_prop.const === 'string')
352
- return [error_prop.const];
353
- if (Array.isArray(error_prop.enum))
354
- return error_prop.enum.filter((v) => typeof v === 'string');
355
- return null;
356
- };
357
- /**
358
- * Audit error schema tightness across all routes in a surface.
359
- *
360
- * Reports which route x status code combinations use generic `ApiError`
361
- * (`z.string()`) vs specific `z.literal()` or `z.enum()` error codes.
362
- * Use the output to prioritize progressive tightening of error schemas.
363
- *
364
- * @returns audit entries for every route x status combination
365
- */
366
- export const audit_error_schema_tightness = (surface) => {
367
- const entries = [];
368
- for (const route of surface.routes) {
369
- if (!route.error_schemas)
370
- continue;
371
- for (const [status, schema] of Object.entries(route.error_schemas)) {
372
- const specificity = classify_error_specificity(schema);
373
- entries.push({
374
- method: route.method,
375
- route_path: route.path,
376
- status,
377
- specificity,
378
- error_codes: extract_error_codes(schema)
379
- });
380
- }
381
- }
382
- return entries;
383
- };
384
294
  // --- RPC / WS structural invariants ---
385
295
  /**
386
296
  * Every RPC method on every endpoint has a non-empty `description`.
@@ -579,88 +489,15 @@ export const assert_keeper_routes_under_prefix = (surface, prefixes = DEFAULT_KE
579
489
  assert.ok(under_prefix, `${format_route_key(route)} is keeper-protected but not under any expected prefix: ${prefixes.join(', ')}`);
580
490
  }
581
491
  };
582
- // --- Error schema tightness assertion ---
583
- /** Numeric specificity ranking for threshold comparisons. */
584
- const SPECIFICITY_ORDER = {
585
- literal: 2,
586
- enum: 1,
587
- generic: 0
588
- };
589
- /**
590
- * Routes shipped by fuz_app whose error schemas require a tightness exemption.
591
- *
592
- * Currently empty — every fuz_app-shipped route (account login/password/
593
- * bootstrap/signup, db health/tables/:name/tables/:name/rows/:id) was tightened
594
- * in place to `z.enum([...])` / `z.literal(...)` against every emit-site error
595
- * code.
596
- *
597
- * Kept as a forward-compatibility hook: when new stock routes ship with
598
- * heterogeneous error surfaces that need an interim generic schema, add
599
- * them here instead of forcing every consumer to hand-maintain the entry.
600
- *
601
- * Paths assume the standard `/api/account` + `/api/db` prefixes used by every
602
- * fuz_app consumer. Merged into `default_error_schema_tightness.allowlist` so
603
- * consumers calling `assert_error_schema_tightness` directly inherit the
604
- * exemptions; the standard attack-surface suite also prepends these entries
605
- * underneath any consumer-supplied allowlist so project-specific entries are
606
- * additive.
607
- */
608
- export const fuz_app_stock_route_tightness_allowlist = [];
609
- /**
610
- * Baseline error schema tightness applied by
611
- * `describe_standard_attack_surface_tests` when no config is passed.
612
- *
613
- * Uses `min_specificity: 'enum'` (the assertion default) with `ignore_statuses`
614
- * for middleware-derived status codes that are commonly generic (auth middleware
615
- * produces multiple error codes at 401/403, and 429 comes from rate limiters),
616
- * and `allowlist` seeded with `fuz_app_stock_route_tightness_allowlist` so
617
- * fuz_app-shipped routes with heterogeneous generic schemas don't force every
618
- * consumer to hand-maintain an identical allowlist. Consumers can pass a
619
- * narrower config with project-specific `allowlist` entries, or pass `null`
620
- * to skip the assertion entirely.
621
- */
622
- export const default_error_schema_tightness = {
623
- ignore_statuses: [401, 403, 429],
624
- allowlist: [...fuz_app_stock_route_tightness_allowlist]
625
- };
626
- /**
627
- * Assert that all error schemas meet a minimum specificity threshold.
628
- *
629
- * Calls `audit_error_schema_tightness` and fails on any entry below
630
- * the configured threshold. Use `allowlist` and `ignore_statuses` to exclude
631
- * known exceptions during progressive tightening.
632
- *
633
- * @throws AssertionError listing every route × status combination whose error
634
- * schema specificity is below `min_specificity` (default `'enum'`) and is
635
- * not in `allowlist` or `ignore_statuses`.
636
- */
637
- export const assert_error_schema_tightness = (surface, options) => {
638
- const min_specificity = options?.min_specificity ?? 'enum';
639
- const ignore_statuses = new Set(options?.ignore_statuses?.map(String));
640
- const allowlist = new Set(options?.allowlist);
641
- const threshold = SPECIFICITY_ORDER[min_specificity];
642
- const entries = audit_error_schema_tightness(surface);
643
- const failures = [];
644
- for (const entry of entries) {
645
- if (ignore_statuses.has(entry.status))
646
- continue;
647
- const key = `${entry.method} ${entry.route_path}`;
648
- if (allowlist.has(key))
649
- continue;
650
- if (SPECIFICITY_ORDER[entry.specificity] < threshold) {
651
- failures.push(`${key} → ${entry.status} (${entry.specificity})`);
652
- }
653
- }
654
- assert.ok(failures.length === 0, `Error schemas below '${min_specificity}' threshold:\n ${failures.join('\n ')}`);
655
- };
656
492
  // --- Aggregate runners ---
657
493
  /**
658
494
  * Run all structural invariants. Options-free — applies universally.
659
495
  *
660
496
  * Catches schema/surface generation bugs: missing 401/403/400 declarations,
661
497
  * empty descriptions, duplicate routes, middleware-injected error codes
662
- * unpropagated to routes, structurally invalid error schemas, error codes
663
- * appearing at multiple statuses, and generic 404 schemas on param routes.
498
+ * unpropagated to routes (by construction on a generated surface, so a
499
+ * snapshot's check), an error that doesn't name its code or whose code is
500
+ * not one, and error codes appearing at multiple statuses.
664
501
  *
665
502
  * @throws AssertionError on the first invariant violation; the message names
666
503
  * the offending route and the missing/inconsistent field.
@@ -674,9 +511,9 @@ export const assert_surface_invariants = (surface) => {
674
511
  assert_descriptions_present(surface);
675
512
  assert_no_duplicate_routes(surface);
676
513
  assert_middleware_errors_propagated(surface);
677
- assert_error_schemas_structurally_valid(surface);
514
+ assert_error_codes_specific(surface);
515
+ assert_error_codes_well_formed(surface);
678
516
  assert_error_code_status_consistency(surface);
679
- assert_404_schemas_use_specific_errors(surface);
680
517
  };
681
518
  /**
682
519
  * Run all RPC / WS structural invariants. Options-free — applies
package/dist/ui/CLAUDE.md CHANGED
@@ -694,8 +694,10 @@ format_scope, global_label)` helper — `global_label = null` renders no
694
694
  one signal, once aborted, would fail every later probe.
695
695
  - `ui_fetch.ts` — `ui_fetch(input, init?)` wraps `fetch` with
696
696
  `credentials: 'include'` for cookie-based session auth;
697
- `parse_response_error(response, fallback?)` safely extracts
698
- `body.error` even from non-JSON responses (HTML 404 pages, etc.).
697
+ `parse_response_error(response, fallback?)` safely extracts the text to
698
+ show even from non-JSON responses (HTML 404 pages, etc.) through
699
+ `format_error_body(body, fallback)`: the body's `message`, else its
700
+ `error` code. `table_state` and `auth_state` show refusals through it.
699
701
  - `ui_format.ts` — display helpers:
700
702
  - `format_relative_time(timestamp, now?)` — "2m ago", "3h ago",
701
703
  "5d ago", "2mo ago", "1y ago"; "just now" when under a minute;
@@ -1 +1 @@
1
- {"version":3,"file":"auth_state.svelte.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/ui/auth_state.svelte.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAMH,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,oBAAoB,EACzB,KAAK,cAAc,EACnB,MAAM,2BAA2B,CAAC;AAGnC;;;;;;;;GAQG;AACH,eAAO,MAAM,yBAAyB,GAAI,aAAa,OAAO,KAAG,MAShE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB;;;;CAA8B,CAAC;AAE9D,qBAAa,SAAS;IACrB,SAAS,UAAqB;IAC9B,QAAQ,UAAqB;IAC7B,YAAY,EAAE,MAAM,GAAG,IAAI,CAAoB;IAC/C,OAAO,EAAE,cAAc,GAAG,IAAI,CAAoB;IAClD,KAAK,EAAE,gBAAgB,GAAG,IAAI,CAAoB;IAClD,WAAW,EAAE,KAAK,CAAC,oBAAoB,CAAC,CAAkB;IAC1D;;;;OAIG;IACH,QAAQ,CAAC,kBAAkB,EAAE,KAAK,CAAC,oBAAoB,CAAC,CAEtD;IACF;;;;;;;;OAQG;IACH,QAAQ,CAAC,YAAY,EAAE,WAAW,CAAC,MAAM,CAAC,CAExC;IACF,wFAAwF;IACxF,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAA+C;IAEzE,gEAAgE;IAChE,eAAe,UAAqB;IAEpC;;;;;;OAMG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAItC;;;;;;;;OAQG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IA6BpC;;;;;;;OAOG;IACG,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAuCjE;;;;;OAKG;IACG,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAiCpF;;;;;;;OAOG;IACG,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA2ClF;;;;;OAKG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAe7B"}
1
+ {"version":3,"file":"auth_state.svelte.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/ui/auth_state.svelte.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAMH,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,oBAAoB,EACzB,KAAK,cAAc,EACnB,MAAM,2BAA2B,CAAC;AAGnC;;;;;;;;GAQG;AACH,eAAO,MAAM,yBAAyB,GAAI,aAAa,OAAO,KAAG,MAShE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB;;;;CAA8B,CAAC;AAE9D,qBAAa,SAAS;IACrB,SAAS,UAAqB;IAC9B,QAAQ,UAAqB;IAC7B,YAAY,EAAE,MAAM,GAAG,IAAI,CAAoB;IAC/C,OAAO,EAAE,cAAc,GAAG,IAAI,CAAoB;IAClD,KAAK,EAAE,gBAAgB,GAAG,IAAI,CAAoB;IAClD,WAAW,EAAE,KAAK,CAAC,oBAAoB,CAAC,CAAkB;IAC1D;;;;OAIG;IACH,QAAQ,CAAC,kBAAkB,EAAE,KAAK,CAAC,oBAAoB,CAAC,CAEtD;IACF;;;;;;;;OAQG;IACH,QAAQ,CAAC,YAAY,EAAE,WAAW,CAAC,MAAM,CAAC,CAExC;IACF,wFAAwF;IACxF,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAA+C;IAEzE,gEAAgE;IAChE,eAAe,UAAqB;IAEpC;;;;;;OAMG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAItC;;;;;;;;OAQG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IA6BpC;;;;;;;OAOG;IACG,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAuCjE;;;;;OAKG;IACG,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA4BpF;;;;;;;OAOG;IACG,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA2ClF;;;;;OAKG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAe7B"}
@@ -33,7 +33,7 @@
33
33
  */
34
34
  import { to_error_message } from '@fuzdev/fuz_util/error.ts';
35
35
  import { create_context } from '@fuzdev/fuz_ui/context_helpers.ts';
36
- import { ui_fetch } from './ui_fetch.js';
36
+ import { parse_response_error, ui_fetch } from './ui_fetch.js';
37
37
  import { is_role_grant_active } from '../auth/account_schema.js';
38
38
  import { ROLE_ADMIN } from '../auth/role_schema.js';
39
39
  /**
@@ -208,13 +208,7 @@ export class AuthState {
208
208
  await this.check_session();
209
209
  return true;
210
210
  }
211
- try {
212
- const data = await response.json();
213
- this.verify_error = data.error ?? `Error: ${response.status}`;
214
- }
215
- catch {
216
- this.verify_error = `Error: ${response.status}`;
217
- }
211
+ this.verify_error = await parse_response_error(response);
218
212
  return false;
219
213
  }
220
214
  catch (e) {
@@ -12,7 +12,7 @@
12
12
  * @module
13
13
  */
14
14
  import { to_error_message } from '@fuzdev/fuz_util/error.ts';
15
- import { z } from 'zod';
15
+ import { is, loose_object } from '@fuzdev/fuz_schema/schema.ts';
16
16
  import { HEALTH_PATH, HealthResponse } from '../http/health_schema.js';
17
17
  /** The refusal for a `2xx` whose body isn't the health reply. */
18
18
  export const BACKEND_PROBE_NOT_HEALTH = 'not a health response';
@@ -24,7 +24,7 @@ export const BACKEND_PROBE_TIMEOUT_MS = 10_000;
24
24
  * The client's read of the health reply — loose, so a field added to the
25
25
  * route's output never reads as a dead backend. The route spec stays strict.
26
26
  */
27
- const HealthReply = z.looseObject(HealthResponse.shape);
27
+ const HealthReply = loose_object(HealthResponse.def);
28
28
  const is_timeout_error = (err) => err?.name === 'TimeoutError';
29
29
  /**
30
30
  * Probe whether the backend is serving: its health route must answer `2xx`
@@ -80,7 +80,7 @@ export const probe_backend = async (options) => {
80
80
  // a dropped connection mid-body, not a wrong body
81
81
  return { ok: false, error: to_error_message(err, 'network error') };
82
82
  }
83
- if (!HealthReply.safeParse(body).success) {
83
+ if (!is(HealthReply, body)) {
84
84
  return { ok: false, error: BACKEND_PROBE_NOT_HEALTH };
85
85
  }
86
86
  return { ok: true };
@@ -1 +1 @@
1
- {"version":3,"file":"table_state.svelte.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/ui/table_state.svelte.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAIH,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAGnD,OAAO,EAGN,KAAK,UAAU,EACf,MAAM,sBAAsB,CAAC;AAE9B,qBAAa,UAAU;IACtB,QAAQ,CAAC,IAAI,0BAAyB;IAEtC,UAAU,EAAE,MAAM,CAAkB;IACpC,OAAO,EAAE,KAAK,CAAC,UAAU,CAAC,CAAkB;IAC5C,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAkB;IACtD,KAAK,SAAiB;IACtB,MAAM,SAAiB;IACvB,KAAK,SAA2C;IAChD,WAAW,EAAE,MAAM,GAAG,IAAI,CAAoB;IAC9C;;;;;OAKG;IACH,SAAS,UAAqB;IAC9B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAoB;IAC3C,YAAY,EAAE,MAAM,GAAG,IAAI,CAAoB;IAE/C;;;;OAIG;IACH,QAAQ,CAAC,UAAU,UAAyD;IAG5E,QAAQ,CAAC,aAAa,SAAoD;IAC1E,QAAQ,CAAC,WAAW,SAAkE;IACtF,QAAQ,CAAC,QAAQ,UAA6B;IAC9C,QAAQ,CAAC,QAAQ,UAAmD;IAEpE;;;;;;;OAOG;IACG,KAAK,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,SAAI,EAAE,KAAK,SAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAoBvE,OAAO,IAAI,IAAI;IAIf,OAAO,IAAI,IAAI;IAIf;;;;;;;OAOG;IACG,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;CAoChE"}
1
+ {"version":3,"file":"table_state.svelte.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/ui/table_state.svelte.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAIH,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAGnD,OAAO,EAGN,KAAK,UAAU,EACf,MAAM,sBAAsB,CAAC;AAE9B,qBAAa,UAAU;IACtB,QAAQ,CAAC,IAAI,0BAAyB;IAEtC,UAAU,EAAE,MAAM,CAAkB;IACpC,OAAO,EAAE,KAAK,CAAC,UAAU,CAAC,CAAkB;IAC5C,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAkB;IACtD,KAAK,SAAiB;IACtB,MAAM,SAAiB;IACvB,KAAK,SAA2C;IAChD,WAAW,EAAE,MAAM,GAAG,IAAI,CAAoB;IAC9C;;;;;OAKG;IACH,SAAS,UAAqB;IAC9B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAoB;IAC3C,YAAY,EAAE,MAAM,GAAG,IAAI,CAAoB;IAE/C;;;;OAIG;IACH,QAAQ,CAAC,UAAU,UAAyD;IAG5E,QAAQ,CAAC,aAAa,SAAoD;IAC1E,QAAQ,CAAC,WAAW,SAAkE;IACtF,QAAQ,CAAC,QAAQ,UAA6B;IAC9C,QAAQ,CAAC,QAAQ,UAAmD;IAEpE;;;;;;;OAOG;IACG,KAAK,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,SAAI,EAAE,KAAK,SAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAoBvE,OAAO,IAAI,IAAI;IAIf,OAAO,IAAI,IAAI;IAIf;;;;;;;OAOG;IACG,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;CA8BhE"}
@@ -124,14 +124,7 @@ export class TableState {
124
124
  try {
125
125
  const response = await ui_fetch(`/api/db/tables/${this.table_name}/rows/${encodeURIComponent(pk_str)}`, { method: 'DELETE' });
126
126
  if (!response.ok) {
127
- try {
128
- const data = await response.json();
129
- const error_msg = data.error || 'unknown error';
130
- this.delete_error = data.detail ? `${error_msg}: ${data.detail}` : error_msg;
131
- }
132
- catch {
133
- this.delete_error = `Error: ${response.status}`;
134
- }
127
+ this.delete_error = await parse_response_error(response);
135
128
  return false;
136
129
  }
137
130
  // Remove from UI
@@ -12,12 +12,22 @@
12
12
  */
13
13
  export declare const ui_fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
14
14
  /**
15
- * Safely extract an error message from a non-ok response.
15
+ * The text to show for an error body: its `message` when it carries one,
16
+ * else its `error` code — a body's `error` is a machine-readable code, and
17
+ * prose, when the server sends any, is in `message`.
18
+ *
19
+ * @param body - the parsed error body, of any shape
20
+ * @param fallback - the text when the body has neither
21
+ */
22
+ export declare const format_error_body: (body: unknown, fallback: string) => string;
23
+ /**
24
+ * Safely extract an error message from a non-ok response: its `message`,
25
+ * else its `error` code (`format_error_body`).
16
26
  *
17
27
  * Handles responses with non-JSON bodies (e.g. HTML 404 pages)
18
28
  * that would throw on `response.json()`.
19
29
  *
20
- * @param fallback - message when no `.error` field is found
30
+ * @param fallback - message when the body has no `message` or `error`
21
31
  */
22
32
  export declare const parse_response_error: (response: Response, fallback?: string) => Promise<string>;
23
33
  //# sourceMappingURL=ui_fetch.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ui_fetch.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/ui/ui_fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,WAAW,GAAG,GAAG,EAAE,OAAO,WAAW,KAAG,OAAO,CAAC,QAAQ,CACtC,CAAC;AAEnD;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,GAChC,UAAU,QAAQ,EAClB,WAAW,MAAM,KACf,OAAO,CAAC,MAAM,CAQhB,CAAC"}
1
+ {"version":3,"file":"ui_fetch.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/ui/ui_fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,WAAW,GAAG,GAAG,EAAE,OAAO,WAAW,KAAG,OAAO,CAAC,QAAQ,CACtC,CAAC;AAEnD;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,GAAI,MAAM,OAAO,EAAE,UAAU,MAAM,KAAG,MAMnE,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,oBAAoB,GAChC,UAAU,QAAQ,EAClB,WAAW,MAAM,KACf,OAAO,CAAC,MAAM,CAOhB,CAAC"}
@@ -12,18 +12,36 @@
12
12
  */
13
13
  export const ui_fetch = (input, init) => fetch(input, { ...init, credentials: 'include' });
14
14
  /**
15
- * Safely extract an error message from a non-ok response.
15
+ * The text to show for an error body: its `message` when it carries one,
16
+ * else its `error` code — a body's `error` is a machine-readable code, and
17
+ * prose, when the server sends any, is in `message`.
18
+ *
19
+ * @param body - the parsed error body, of any shape
20
+ * @param fallback - the text when the body has neither
21
+ */
22
+ export const format_error_body = (body, fallback) => {
23
+ if (body === null || typeof body !== 'object')
24
+ return fallback;
25
+ const { message, error } = body;
26
+ if (typeof message === 'string' && message)
27
+ return message;
28
+ if (typeof error === 'string' && error)
29
+ return error;
30
+ return fallback;
31
+ };
32
+ /**
33
+ * Safely extract an error message from a non-ok response: its `message`,
34
+ * else its `error` code (`format_error_body`).
16
35
  *
17
36
  * Handles responses with non-JSON bodies (e.g. HTML 404 pages)
18
37
  * that would throw on `response.json()`.
19
38
  *
20
- * @param fallback - message when no `.error` field is found
39
+ * @param fallback - message when the body has no `message` or `error`
21
40
  */
22
41
  export const parse_response_error = async (response, fallback) => {
23
42
  const default_message = fallback ?? `Error: ${response.status}`;
24
43
  try {
25
- const data = await response.json();
26
- return typeof data?.error === 'string' ? data.error : default_message;
44
+ return format_error_body(await response.json(), default_message);
27
45
  }
28
46
  catch {
29
47
  return default_message;