Skip to content

Add JSON error responses for --output=json mode#269

Merged
AndreyVMarkelov merged 1 commit into
masterfrom
json-error-responses
Jun 22, 2026
Merged

Add JSON error responses for --output=json mode#269
AndreyVMarkelov merged 1 commit into
masterfrom
json-error-responses

Conversation

@AndreyVMarkelov

@AndreyVMarkelov AndreyVMarkelov commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Render errors as structured JSON ({"ok": false, "error": {"message": "...", "code": "..."}}) to stdout when --output=json is requested
  • Use pre-parse arg scanning (outputJSONRequested) to detect JSON mode for errors that occur before Cobra resolves the command (unknown command/flag)
  • Add stable error codes: structured_output_unsupported, unsupported_output_format, unknown_command, unknown_flag, path_conflict, invalid_arguments, command_failed
  • Switch to SilenceErrors: true + ExecuteC() to route all errors through the new renderer

Test plan

  • go vet ./... clean
  • golangci-lint run ./... clean
  • All tests pass (go test ./... -count=1)
  • Manual: dbxcli missing --output=json returns JSON error with unknown_command code
  • Manual: dbxcli ls --output=json returns JSON error with structured_output_unsupported code
  • Manual: dbxcli ls --output=yaml returns text error to stderr (invalid format, no JSON)
  • Manual: dbxcli ls error still prints text to stderr as before

Render errors as structured JSON to stdout when --output=json is
requested. Uses pre-parse arg scanning to detect JSON mode for errors
that occur before Cobra resolves the command (unknown command/flag).
Adds stable error codes for scripting consumers.
@AndreyVMarkelov AndreyVMarkelov merged commit 317b37d into master Jun 22, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant