writeRootStructuredError adds the machine-readable marker line for the refusals ANY command can receive, which no per-command writer can own (BUG-3147). That is the server's 429, and its 413 (BUG-2829): cobra has already printed "Error: " for a person, and the stdio MCP transport lifts this
(w io.Writer, err error)
| 81 | // already printed "Error: <message>" for a person, and the stdio MCP transport |
| 82 | // lifts this line instead of guessing a code from that prose. |
| 83 | func writeRootStructuredError(w io.Writer, err error) { |
| 84 | defer writeOpKeyHint(w, err) |
| 85 | var commitmentErr *cli.APIError |
| 86 | if errors.As(err, &commitmentErr) { |
| 87 | structured := false |
| 88 | switch commitmentErr.Code { |
| 89 | case "ambiguous_reminder", "schedule_exists", "reminder_not_fired", "reminder_managed": |
| 90 | structured = true |
| 91 | case "invalid_body": |
| 92 | structured = cli.IsCommitmentRoute(commitmentErr.RequestPath) |
| 93 | case "reopen_unavailable", "reopen_not_supported", "reopen_conflict", "reopen_dependency", "reopen_deadline_passed", "not_fired", "stale", "schedule_error", "schedule_busy", "hard_deadline", "deadline_owner_required", "commitment_changed", |
| 94 | "schedule_linked", "deadline_approval_required", "link_violation_ack", "link_budget", "link_edit_required", "schedule_link_not_found", |
| 95 | "link_cycle", "link_self", "link_duplicate_incoming", "link_limit", "link_invalid", "gate_duplicate", "commitment_preview_required", |
| 96 | "item_still_open", "not_ended": |
| 97 | structured = true |
| 98 | case "validation_error": |
| 99 | var detail struct { |
| 100 | Field string `json:"field"` |
| 101 | Code string `json:"code"` |
| 102 | } |
| 103 | if json.Unmarshal(commitmentErr.Details, &detail) == nil && detail.Code != "" { |
| 104 | switch detail.Field { |
| 105 | case "resolution_id", "reason", "revision", "role", "to", "cancel_policy", "offset", "parent_role", "child_role", "child", "blocker", "state", "occurrence_at", "until", "actual_done_at", "comment", "end_schedule": |
| 106 | structured = true |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | if structured { |
| 111 | if data, encodeErr := json.Marshal(map[string]any{"error": commitmentErr}); encodeErr == nil { |
| 112 | fmt.Fprintln(w, cli.StructuredErrorMarker+string(data)) |
| 113 | } |
| 114 | return |
| 115 | } |
| 116 | } |
| 117 | if apiErr, ok := cli.IsRateLimited(err); ok { |
| 118 | cli.WriteRateLimitedError(w, apiErr) |
| 119 | return |
| 120 | } |
| 121 | if apiErr, ok := cli.IsTooLarge(err); ok { |
| 122 | cli.WriteTooLargeError(w, apiErr) |
| 123 | return |
| 124 | } |
| 125 | // BUG-3252: comment-delete against a server that predates comment |
| 126 | // tombstones. A root writer keeps any later door that deletes a comment |
| 127 | // from saying server_error. |
| 128 | if apiErr, ok := cli.IsCommentHasReplies(err); ok { |
| 129 | cli.WriteCommentHasRepliesError(w, apiErr) |
| 130 | return |
| 131 | } |
| 132 | // BUG-3252: an edit, reply or reaction addressed to a tombstone. |
| 133 | if apiErr, ok := cli.IsCommentDeleted(err); ok { |
| 134 | cli.WriteCommentDeletedError(w, apiErr) |
| 135 | return |
| 136 | } |
| 137 | // BUG-3496: a claim on an item another holder leases. |
| 138 | if apiErr, ok := cli.IsLeaseHeld(err); ok { |
| 139 | cli.WriteLeaseHeldError(w, apiErr) |
| 140 | return |