Skip to content

fix: include error message in --json output#1615

Merged
jfeingold35 merged 1 commit into
oclif:mainfrom
spokodev:fix/json-error-message
Jun 17, 2026
Merged

fix: include error message in --json output#1615
jfeingold35 merged 1 commit into
oclif:mainfrom
spokodev:fix/json-error-message

Conversation

@spokodev

Copy link
Copy Markdown
Contributor

Closes #1608.

When a command with enableJsonFlag errors, --json mode serializes the error via toErrorJson(), which returned {error: err}. JSON.stringify (in logJson) then drops Error.prototype.message/.name/.stack because they are non-enumerable own properties, so the user-facing message is lost:

$ mycmd --json          # throw new Error('...')
{ "error": {} }
$ mycmd --json          # throw new Errors.CLIError('...')
{ "error": { "oclif": { "exit": 2 } } }

This affects all three idiomatic patterns: throw new Error(...), throw new Errors.CLIError(...), and this.error(...).

Fix

In toErrorJson, when the error is an Error, serialize message/name explicitly while spreading enumerable own properties, so existing structural fields (e.g. a CLIError's oclif metadata) are preserved and the message is surfaced. Non-Error values are passed through unchanged.

Tests

Two --json error rendering tests, red before the change and green after — a plain Error and a CLIError (asserting the message is present and oclif.exit is still emitted). Full test/command + test/errors suites pass.

`toErrorJson` returned `{error: err}`, and `JSON.stringify` (in `logJson`)
drops `Error`'s non-enumerable `message`/`name`, so `--json` error output
was `{"error": {}}` — or only `{"oclif": {"exit": 2}}` for a `CLIError`.
Serialize the message and name explicitly while preserving enumerable own
properties, so all three error patterns (plain `Error`, `CLIError`,
`this.error`) surface the message under `--json`.

Closes oclif#1608
@salesforce-cla

Copy link
Copy Markdown

Thanks for the contribution! Before we can merge this, we need @spokodev to sign the Salesforce Inc. Contributor License Agreement.

@jfeingold35

Copy link
Copy Markdown
Contributor

@spokodev , thank you for submitting this. I'll get it reviewed and QA'd, and then assuming nothing needs to change, we can merge it and close the associated Issue.

@jfeingold35

Copy link
Copy Markdown
Contributor

@spokodev , the PR looks good. Assuming the tests all pass, we'll be good to merge it.

@jfeingold35 jfeingold35 merged commit bd41207 into oclif:main Jun 17, 2026
84 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--json error rendering drops the error message

2 participants