Skip to content

Commit 098a08f

Browse files
os-trumpclaude
andauthored
feat(cli): carry the ADR-0112 code and httpStatus in --format json failure envelopes (#13510)
* feat(cli): carry the ADR-0112 code and httpStatus in --format json failure envelopes Every machine-readable failure the CLI emits was `{ success: false, error: error.message }` and nothing else — 48 sites under `packages/cli/src/commands/`. The error reaching those `catch` blocks from `@objectstack/client` is not a bare `Error`: the SDK's `fetch` wrapper attaches `err.code` (the semantic ADR-0112 string, normalized across both server dialects) and `err.httpStatus`. Both were discarded at the CLI boundary, so a script had to substring-match an English sentence that no contract pins. One shared builder (`errorCodeFields` in `utils/format.ts`) plus 48 call-site spreads. Additive: `success` and `error` keep their meaning and spelling, the payload stays flat, and the two keys are ABSENT — not `undefined` — when the thrown error did not carry them. No fallback code is invented for a locally-thrown plain `Error`. Human `table` output is untouched. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TvqBFLRzXdSPcbusDoED9k * test(cli): give the #13347 envelope cases an explicit wall-clock budget Each case drives a real `Command.run` against the real oclif root — ~0.8s per case on an idle box, and measured TIMING OUT at vitest's 5s default when the file ran inside `@objectstack/cli`'s full 220-file suite on a shared container. Wall-clock only: no assertion moves. The neighbouring `delete-reset-carriers.test.ts` reaches the same conclusion the same way. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TvqBFLRzXdSPcbusDoED9k * docs(cli): declare errorCodeFields.code's real value space — pass-through, ADR-0112 for wire failures, Node errno for local I/O Contract-review amendment on #13510: the field doc and changeset called code 'the semantic ADR-0112 code', but the builder is a structural pass-through and os validate/compile/lint throw errno-carrying fs errors inside the same try, so ENOENT/ENOTDIR are genuinely publishable values. Behaviour unchanged (the ruling's literal pass-through shape stands); only the declaration now matches it. The vocabularies are disjoint (errnos are E-prefixed OS names), so ADR-0112 branches cannot false-match an errno. --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent ec4c4d2 commit 098a08f

42 files changed

Lines changed: 763 additions & 56 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
"@objectstack/cli": minor
3+
---
4+
5+
feat(cli): `--format json` failure envelopes carry the ADR-0112 `code` and `httpStatus` (#13347)
6+
7+
Every machine-readable failure this CLI emits was built the same way — 48 sites
8+
under `packages/cli/src/commands/`:
9+
10+
```ts
11+
await emitJson({ success: false, error: error.message });
12+
```
13+
14+
The payload carried the human sentence and nothing else. The error reaching
15+
those `catch` blocks from `@objectstack/client` is not a bare `Error`: the SDK's
16+
`fetch` wrapper attaches `err.code` (the semantic ADR-0112 string, normalized to
17+
the same spelling across the flat `@objectstack/rest` envelope and the wrapped
18+
runtime-dispatcher one) and `err.httpStatus`. Both were discarded at the CLI
19+
boundary, so the one outcome a script most needs to branch on — *someone else
20+
edited it, re-read and retry* vs *you are not allowed* vs *the server is down*
21+
was separable only by substring-matching an English sentence that no contract
22+
pins.
23+
24+
A stale-pin refusal from `os meta delete --if-match` used to read:
25+
26+
```json
27+
{
28+
"success": false,
29+
"error": "[metadata_conflict] view/race_probe has been modified since you loaded it. …"
30+
}
31+
```
32+
33+
and now reads:
34+
35+
```json
36+
{
37+
"success": false,
38+
"error": "[metadata_conflict] view/race_probe has been modified since you loaded it. …",
39+
"code": "METADATA_CONFLICT",
40+
"httpStatus": 409
41+
}
42+
```
43+
44+
Maintainer ruling 2026-08-30 (option **A** of three):
45+
46+
- The payload stays **FLAT**. Nesting into `{ error: { code, message, httpStatus } }`
47+
was considered and declined as breaking.
48+
- `success` and `error` keep their current meaning **and spelling**.
49+
- The two keys are emitted **only when the thrown error carries them**, and are
50+
**absent** — not `undefined` — otherwise. No fallback code is invented for a
51+
locally-thrown plain `Error`: this CLI's own input refusals get no code,
52+
deliberately, because ADR-0112's ledger is the authority on who may mint one.
53+
54+
One value-space note, stated so the declaration matches what ships: `code` is a
55+
**pass-through**, never minted or filtered. On wire failures it is the semantic
56+
ADR-0112 string the SDK attached; on local I/O failures inside the same `try`
57+
(`os validate` reading a `src/docs` that is a file, say) it is the Node errno
58+
(`ENOENT`, `ENOTDIR`, …). The two vocabularies are disjoint — errnos are
59+
`E`-prefixed OS names — so a consumer branching on ADR-0112 codes cannot
60+
false-match an errno, but not every emitted `code` is ledger-owned.
61+
62+
Human (`table`) output is untouched.
63+
64+
**Why `minor` and not `patch`.** Maintainer-set, and it overrides the obvious
65+
reading: this is a shape change to an **already-published error envelope**, and
66+
that makes it minor even though it is purely additive.
67+
68+
**Migration.** Nothing is required — no key is removed, renamed or re-typed, and
69+
every payload this CLI emitted before is still emitted, byte-for-byte, minus the
70+
two new keys. Two things are worth knowing before you rely on the new ones:
71+
72+
- **The envelope is polymorphic, by design.** `code` and `httpStatus` are absent
73+
whenever the failure did not carry them, which a consumer cannot distinguish
74+
from an older CLI. Branch on presence (`if (payload.code === 'METADATA_CONFLICT')`),
75+
never on absence meaning "success" or "unsupported version". This cost was
76+
weighed against breaking every existing consumer, and the non-breaking side won.
77+
- **Stop substring-matching the sentence.** `error` is prose and no contract pins
78+
its wording; the bracketed `[metadata_conflict]` tag some messages carry today
79+
is a property of one producer, not a contract, and a separate card argues for
80+
removing it. Code that reads the sentence to classify a failure should move to
81+
`code` (with `httpStatus` as the coarse fallback).

packages/cli/src/commands/cloud/login.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import * as readline from 'node:readline/promises';
7373
import { stdin as input, stdout as output } from 'node:process';
7474
import { Command, Flags } from '@oclif/core';
7575
import type { CliExitCode } from '../../utils/format.js';
76-
import { printHeader, printKV, printSuccess, printError, emitJson } from '../../utils/format.js';
76+
import { printHeader, printKV, printSuccess, printError, emitJson, errorCodeFields } from '../../utils/format.js';
7777
import { loginWithBrowser, loginWithPassword } from '../../utils/auth-flows.js';
7878
import { DEFAULT_CLOUD_URL, readCloudConfig, writeCloudConfig } from '../../utils/cloud-config.js';
7979

@@ -246,7 +246,7 @@ export default class CloudLogin extends Command {
246246
// written (an expired code, a denied approval, a poll failure), so an
247247
// indented payload here would recreate a two-document stream on the
248248
// path a consumer is least able to recover from.
249-
await emitRecord({ success: false, error: error.message });
249+
await emitRecord({ success: false, error: error.message, ...errorCodeFields(error) });
250250
this.exit(1);
251251
}
252252
printError(error.message || String(error));

packages/cli/src/commands/cloud/logout.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { Command, Flags } from '@oclif/core';
1010
import { ObjectStackClient } from '@objectstack/client';
11-
import { printHeader, printSuccess, printError, emitJson } from '../../utils/format.js';
11+
import { printHeader, printSuccess, printError, emitJson, errorCodeFields } from '../../utils/format.js';
1212
import { deleteCloudConfig, tryReadCloudConfig } from '../../utils/cloud-config.js';
1313

1414
export default class CloudLogout extends Command {
@@ -46,7 +46,7 @@ export default class CloudLogout extends Command {
4646
}
4747
} catch (error: any) {
4848
if (flags.json) {
49-
await emitJson({ success: false, error: error.message });
49+
await emitJson({ success: false, error: error.message, ...errorCodeFields(error) });
5050
this.exit(1);
5151
}
5252
printError(error.message || String(error));

packages/cli/src/commands/cloud/whoami.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { Command, Flags } from '@oclif/core';
10-
import { printHeader, printKV, printSuccess, printError, emitJson, isExitSignal } from '../../utils/format.js';
10+
import { printHeader, printKV, printSuccess, printError, emitJson, isExitSignal, errorCodeFields } from '../../utils/format.js';
1111
import { tryReadCloudConfig } from '../../utils/cloud-config.js';
1212

1313
export default class CloudWhoami extends Command {
@@ -72,7 +72,7 @@ export default class CloudWhoami extends Command {
7272
} catch (error: any) {
7373
if (isExitSignal(error)) throw error;
7474
if (flags.json) {
75-
await emitJson({ success: false, error: error.message });
75+
await emitJson({ success: false, error: error.message, ...errorCodeFields(error) });
7676
this.exit(1);
7777
}
7878
printError(error.message || String(error));

packages/cli/src/commands/compile.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
printMetadataStats,
4040
emitJson,
4141
isExitSignal,
42+
errorCodeFields,
4243
} from '../utils/format.js';
4344
import { checkSpecVersionGap } from '../utils/spec-version.js';
4445

@@ -720,7 +721,7 @@ export default class Compile extends Command {
720721
} catch (error: any) {
721722
if (isExitSignal(error)) throw error;
722723
if (flags.json) {
723-
await emitJson({ success: false, error: error.message, warnings: warningsSoFar(), conversions: conversionNotices }, 0, { compact: true });
724+
await emitJson({ success: false, error: error.message, ...errorCodeFields(error), warnings: warningsSoFar(), conversions: conversionNotices }, 0, { compact: true });
724725
this.exit(1);
725726
}
726727
console.log('');

packages/cli/src/commands/data/create.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { Args, Command, Flags } from '@oclif/core';
4-
import { printError, printSuccess, emitJson } from '../../utils/format.js';
4+
import { printError, printSuccess, emitJson, errorCodeFields } from '../../utils/format.js';
55
import { createApiClient, requireAuth } from '../../utils/api-client.js';
66
import { formatOutput } from '../../utils/output-formatter.js';
77

@@ -100,6 +100,7 @@ export default class DataCreate extends Command {
100100
await emitJson({
101101
success: false,
102102
error: error.message,
103+
...errorCodeFields(error),
103104
});
104105
this.exit(1);
105106
}

packages/cli/src/commands/data/delete.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { Args, Command, Flags } from '@oclif/core';
4-
import { printError, printSuccess, emitJson } from '../../utils/format.js';
4+
import { printError, printSuccess, emitJson, errorCodeFields } from '../../utils/format.js';
55
import { createApiClient, requireAuth } from '../../utils/api-client.js';
66
import { formatOutput } from '../../utils/output-formatter.js';
77

@@ -83,6 +83,7 @@ export default class DataDelete extends Command {
8383
await emitJson({
8484
success: false,
8585
error: error.message,
86+
...errorCodeFields(error),
8687
});
8788
this.exit(1);
8889
}

packages/cli/src/commands/data/get.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { Args, Command, Flags } from '@oclif/core';
4-
import { printError, emitJson } from '../../utils/format.js';
4+
import { printError, emitJson, errorCodeFields } from '../../utils/format.js';
55
import { createApiClient, requireAuth } from '../../utils/api-client.js';
66
import { formatOutput } from '../../utils/output-formatter.js';
77

@@ -74,6 +74,7 @@ export default class DataGet extends Command {
7474
await emitJson({
7575
success: false,
7676
error: error.message,
77+
...errorCodeFields(error),
7778
});
7879
this.exit(1);
7980
}

packages/cli/src/commands/data/query.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { Args, Command, Flags } from '@oclif/core';
4-
import { printError, emitJson } from '../../utils/format.js';
4+
import { printError, emitJson, errorCodeFields } from '../../utils/format.js';
55
import { createApiClient, requireAuth } from '../../utils/api-client.js';
66
import { formatOutput } from '../../utils/output-formatter.js';
77

@@ -117,6 +117,7 @@ export default class DataQuery extends Command {
117117
await emitJson({
118118
success: false,
119119
error: error.message,
120+
...errorCodeFields(error),
120121
});
121122
this.exit(1);
122123
}

packages/cli/src/commands/data/update.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { Args, Command, Flags } from '@oclif/core';
4-
import { printError, printSuccess, emitJson } from '../../utils/format.js';
4+
import { printError, printSuccess, emitJson, errorCodeFields } from '../../utils/format.js';
55
import { createApiClient, requireAuth } from '../../utils/api-client.js';
66
import { formatOutput } from '../../utils/output-formatter.js';
77

@@ -104,6 +104,7 @@ export default class DataUpdate extends Command {
104104
await emitJson({
105105
success: false,
106106
error: error.message,
107+
...errorCodeFields(error),
107108
});
108109
this.exit(1);
109110
}

0 commit comments

Comments
 (0)