|
3 | 3 | /** |
4 | 4 | * [#29985 codemod] The "Discovery, types, diagnostics, packaged bases, |
5 | 5 | * credential carry-forward" facet of `ObjectStackProtocolImplementation`: the |
6 | | - * bodies of `getProjectId`, `getMetaTypes`, `getPackagedObjectBase`, |
| 6 | + * bodies of `getProjectId`, `getMetaTypes`, |
| 7 | + * `rethrowUnlessMetadataStoreUnprovisioned`, `getPackagedObjectBase`, |
7 | 8 | * `getPackagedDashboardBase`, `getPackagedViewBase`, `getPackagedReportBase`, |
8 | 9 | * `packagedArtifactBase`, `objectRegistrationRefusal`, |
9 | 10 | * `restoreTranslatedObjectEchoes`, `restoreInjectedColumnEchoes`, |
@@ -55,6 +56,8 @@ import { |
55 | 56 | toJsonSchemaSafe, |
56 | 57 | } from './protocol-meta-type-schema.js'; |
57 | 58 | import type { SaveMetaItemRequest } from './protocol-door-requests.js'; |
| 59 | +import { isMissingTableError } from '@objectstack/metadata/errors'; |
| 60 | +import { metadataReadFailureError } from './protocol-client-failures.js'; |
58 | 61 | import type { ProtocolHost } from './protocol-host.js'; |
59 | 62 | import { servedOverrideBaseOf } from './served-override.js'; |
60 | 63 | import type { ObjectStackProtocolImplementation } from './protocol.js'; |
@@ -233,6 +236,83 @@ export class ProtocolDiscoveryAndBasesFacet { |
233 | 236 | return listing; |
234 | 237 | } |
235 | 238 |
|
| 239 | + /** |
| 240 | + * [#5532] Decide what a failed READ against `sys_metadata` means, and |
| 241 | + * rethrow unless it is the ONE benign reason. |
| 242 | + * |
| 243 | + * ## The defect |
| 244 | + * |
| 245 | + * Every overlay read in `getMetaItems`/`getMetaItem` used to `catch {}` into |
| 246 | + * its own empty value — `items` left as-is, `item` left `undefined`, the |
| 247 | + * draft lookup falling through to the active read. That made a metadata |
| 248 | + * store the protocol cannot reach **indistinguishable** from an environment |
| 249 | + * where the item was never customised, and the emptiness then travelled |
| 250 | + * the whole read chain unremarked: |
| 251 | + * |
| 252 | + * - `getMetaItemCached` turned it into `not found` — an outage answered |
| 253 | + * as "that item does not exist", which is the opposite disposition |
| 254 | + * (retry the backend vs. create the item / fix the link); |
| 255 | + * - the `state='draft'` read turned it into `NO_DRAFT` / 404 — an outage |
| 256 | + * answered as "there is no pending edit", which a publish flow reads as |
| 257 | + * "nothing to publish"; |
| 258 | + * - `getMetaItems` turned it into `items: []` — an outage answered as |
| 259 | + * "this environment declares none of these", the exact shape #5108 fixed |
| 260 | + * one layer down in `DatabaseLoader` and #5089 in `listForIndex`. |
| 261 | + * |
| 262 | + * ADR-0110 D3 is the rule: a miss and an outage are different facts with |
| 263 | + * opposite meanings, and a consumer must never read one as the other. |
| 264 | + * |
| 265 | + * ## The one benign reason |
| 266 | + * |
| 267 | + * `sys_metadata` has not been provisioned yet. There are then genuinely no |
| 268 | + * overlay rows, so falling through to the registry / MetadataService IS the |
| 269 | + * truth, and a first boot must not explode. Classification is by error TYPE |
| 270 | + * through {@link isMissingTableError} — the same predicate `DatabaseLoader` |
| 271 | + * (#5108) and this package's own `SysMetadataRepository` (#4867) ask, so a |
| 272 | + * driver quirk is taught to the platform once. Conservative in the same |
| 273 | + * direction: an unrecognised error is NOT benign, because a false "benign" |
| 274 | + * silently mis-answers "does this exist?" while a false "real" costs one |
| 275 | + * 503 the caller can retry. |
| 276 | + * |
| 277 | + * ## [#12536] Not benign is not the same as "the store is down" |
| 278 | + * |
| 279 | + * A non-benign failure is classified once more before it is wrapped, by |
| 280 | + * {@link metadataReadFailureError}: a metadata app's hook may have REFUSED |
| 281 | + * this read and marked its refusal with `userMessage` (commit 79c46da90), and that is |
| 282 | + * an application refusal, not a dependency outage. Handing it to the 503 |
| 283 | + * below destroyed the mark at the producer — the two failures left this |
| 284 | + * guard as the same envelope with the same sentence. The classification |
| 285 | + * splits them; neither category's wording changes. |
| 286 | + * |
| 287 | + * @throws {@link markedApplicationRefusalError} — [#12536] FIRST, when the |
| 288 | + * failure carried a producer's `userMessage` mark (commit 79c46da90): the |
| 289 | + * author's text verbatim, in its own refusal category, with the |
| 290 | + * cause still carried for the operator and nothing else quoted. |
| 291 | + * @throws {@link metadataStoreUnavailableError} — a 503 carrying the driver |
| 292 | + * error as `cause`, for every UNMARKED non-benign failure. Not the |
| 293 | + * driver error itself: unwrapped, it has |
| 294 | + * no status, so the REST boundary would have to guess from the |
| 295 | + * message text — and `mapDataError` guesses `no such table` into |
| 296 | + * `404 OBJECT_NOT_FOUND`, i.e. straight back into a miss. |
| 297 | + * @returns normally ONLY for the benign case, licensing the caller to treat |
| 298 | + * the overlay as absent. |
| 299 | + */ |
| 300 | + static rethrowUnlessMetadataStoreUnprovisioned(error: unknown, readObject: string): void { |
| 301 | + // [commit 4cda78c9b] `readObject` is REQUIRED, deliberately. This helper serves |
| 302 | + // callers that read four different tables (`sys_metadata`, |
| 303 | + // `sys_metadata_audit`, `sys_metadata_commit`, `sys_metadata_history`), |
| 304 | + // so a default would silently answer about the wrong one for three of |
| 305 | + // them — measured, not hypothetical: the first draft of this repair |
| 306 | + // hardcoded `sys_metadata` and turned `diffMetaItem`'s genuinely |
| 307 | + // unprovisioned `sys_metadata_history` into a loud failure. A required |
| 308 | + // parameter makes the compiler ask the question at every new call site. |
| 309 | + if (isMissingTableError(error, readObject)) return; |
| 310 | + // [#12536] CLASSIFY, do not assume. A read can fail because the store |
| 311 | + // is unreachable OR because a metadata app's hook refused it in its |
| 312 | + // own words — see {@link metadataReadFailureError}. |
| 313 | + throw metadataReadFailureError(error); |
| 314 | + } |
| 315 | + |
236 | 316 | /** The body of `ObjectStackProtocolImplementation#getPackagedObjectBase`; the method's docblock there is its contract. */ |
237 | 317 | static getPackagedObjectBase(host: Pick<ProtocolHost, 'engine'>, name: string): unknown { |
238 | 318 | if (typeof name !== 'string' || name === '') return undefined; |
|
0 commit comments