Surface failing request IDs in CLI errors

Merged
RO
5mo ago
+231-4

Summary

  • surface failing SDK request ids in CLI error output
  • include request_id in JSON error metadata and render it for human-facing formats
  • make raw basecamp api post/put/delete error handling use the same SDK->CLI conversion path as api get

SDK dependency

  • Requires the SDK change from basecamp/basecamp-sdk#277 (carries X-Request-Id on basecamp.Error), which is now in main — this branch is rebased on top of it and builds against SDK v0.7.4-0.20260629111348-cc8e9772e729.

Problem

The Basecamp API already emits X-Request-Id, and the SDK now preserves that value consistently on basecamp.Error for both generated-service calls and raw client calls.

Before this PR, the CLI still dropped that information:

  • convertSDKError() copied SDK code/message/hint/status fields but discarded the underlying SDK error
  • JSON error envelopes had nowhere to expose the request id
  • styled/plain/markdown error rendering did not show it
  • basecamp api post/put/delete returned raw SDK errors instead of using the same conversion path as api get

That meant users still could not see the failing request id when the CLI hit an API error, even though the SDK had it available.

What changed

  • taught internal/output.AsError() to convert raw *basecamp.Error values into CLI errors while preserving the SDK error as the cause
  • added output.RequestID(err) to recover the SDK request id from the error chain
  • included meta.request_id in JSON error responses when present
  • rendered Request ID: ... in styled, plain-text, and markdown error output
  • updated convertSDKError() to retain the wrapped SDK error as the cause
  • made basecamp api post, put, and delete use convertSDKError() just like api get
  • added tests for request-id JSON metadata, human rendering, and SDK->CLI conversion

Resulting UX

JSON errors now look like:

{
  "ok": false,
  "error": "server error",
  "code": "api_error",
  "meta": {
    "request_id": "req-cli-123"
  }
}

Human-facing error output now includes a dedicated request-id line, e.g.:

Error: server error
Request ID: req-cli-123

Testing

go test ./internal/output -run 'TestWriterErrIncludesRequestIDMeta|TestWriterStyledErrorIncludesRequestID|TestWriterMarkdownErrorIncludesRequestID|TestWriterErr|TestWriterStyledErrorWithHint'
go test ./internal/commands -run 'TestConvertSDKErrorPreservesRequestID|TestParsePath|TestAPIPathArgs'

Summary by cubic

Show failing Basecamp API request IDs in CLI errors for easier debugging and support. Adds the request ID to JSON error metadata and all human-facing formats with sanitization and wrapping.

  • Bug Fixes

    • Handle raw or wrapped *basecamp.Error in output.AsError(); preserve it as the cause and keep request context.
    • Include meta.request_id in JSON error responses using output.RequestID(err).
    • Render “Request ID: …” in styled, plain text, and markdown outputs; sanitize control/ANSI chars, correctly escape markdown (including single backslashes), and wrap long IDs.
    • Route basecamp api post/put/delete through convertSDKError() like api get.
    • Add tests for JSON metadata, human rendering, sanitization, markdown backslash escaping, and SDK→CLI conversion.
  • Dependencies