fix: include error message in --json output#1615
Merged
Merged
Conversation
`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
|
Thanks for the contribution! Before we can merge this, we need @spokodev to sign the Salesforce Inc. Contributor License Agreement. |
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
approved these changes
Jun 17, 2026
Contributor
|
@spokodev , the PR looks good. Assuming the tests all pass, we'll be good to merge it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1608.
When a command with
enableJsonFlagerrors,--jsonmode serializes the error viatoErrorJson(), which returned{error: err}.JSON.stringify(inlogJson) then dropsError.prototype.message/.name/.stackbecause they are non-enumerable own properties, so the user-facing message is lost:This affects all three idiomatic patterns:
throw new Error(...),throw new Errors.CLIError(...), andthis.error(...).Fix
In
toErrorJson, when the error is anError, serializemessage/nameexplicitly while spreading enumerable own properties, so existing structural fields (e.g. aCLIError'soclifmetadata) are preserved and the message is surfaced. Non-Errorvalues are passed through unchanged.Tests
Two
--json error renderingtests, red before the change and green after — a plainErrorand aCLIError(asserting the message is present andoclif.exitis still emitted). Fulltest/command+test/errorssuites pass.