MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / writeRootStructuredError

Function writeRootStructuredError

cmd/pad/main.go:83–165  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
83func 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 "start_owner_only", "start_playbook_taken", "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", "attachment_expired":
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", "scope":
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 // A single-id attachment show, delete or restore refused as not_found or
118 // plan_limit_exceeded: the remote MCP transport reports both codes (with
119 // the plan limit's details), and without a marker stdio says server_error.
120 if apiErr, ok := attachmentRefusal(err); ok {
121 if data, encodeErr := json.Marshal(map[string]any{"error": apiErr}); encodeErr == nil {
122 fmt.Fprintln(w, cli.StructuredErrorMarker+string(data))
123 }
124 return
125 }
126 if apiErr, ok := cli.IsRateLimited(err); ok {
127 cli.WriteRateLimitedError(w, apiErr)
128 return
129 }
130 if apiErr, ok := cli.IsTooLarge(err); ok {
131 cli.WriteTooLargeError(w, apiErr)
132 return
133 }
134 // BUG-3252: comment-delete against a server that predates comment
135 // tombstones. A root writer keeps any later door that deletes a comment
136 // from saying server_error.
137 if apiErr, ok := cli.IsCommentHasReplies(err); ok {
138 cli.WriteCommentHasRepliesError(w, apiErr)
139 return
140 }

Calls 15

IsCommitmentRouteFunction · 0.92
IsRateLimitedFunction · 0.92
WriteRateLimitedErrorFunction · 0.92
IsTooLargeFunction · 0.92
WriteTooLargeErrorFunction · 0.92
IsCommentHasRepliesFunction · 0.92
IsCommentDeletedFunction · 0.92
WriteCommentDeletedErrorFunction · 0.92
IsLeaseHeldFunction · 0.92
WriteLeaseHeldErrorFunction · 0.92
IsRoleParkErrorFunction · 0.92