Skip to content

fix(api): map ClickUp API errors to typed, user-friendly errors - #64

Merged
timimsms merged 1 commit into
mainfrom
fix/typed-api-errors
Nov 18, 2026
Merged

timimsms merged 1 commit into
mainfrom
fix/typed-api-errors

Conversation

@timimsms

Copy link
Copy Markdown
Owner

Summary

Part of #27 (part 1 only: typed error mapping in api.Client.handleError. Migrating commands from Run+os.Exit to RunE will be a separate follow-up).

Client.handleError was a passthrough, and internal/errors (UserError, HandleHTTPError) was never called, so users got raw SDK strings. handleError now turns the SDK's *clickup.ErrorResponse, *clickup.RateLimitError and *clickup.AbuseRateLimitError into a single *errors.APIError with:

  • a category sentinel reachable via errors.Is: ErrNotAuthenticated (401), ErrPermissionDenied (403), ErrNotFound (404), ErrRateLimited (429), ErrServer (5xx), ErrInvalidInput (400)
  • resource context taken from the request path (task/abc → task "abc", team/123 → workspace "123")
  • the HTTP status, ClickUp ECODE and err text kept in a trailing parenthetical
  • the original SDK error kept in the unwrap chain, so errors.As(err, &*clickup.ErrorResponse) still works

Already-typed errors are returned unchanged. Network and context errors pass through untouched.

Before / after (cu task view bogus-id, checked live, read-only):

Failed to get task: GET https://api.clickup.com/api/v2/task/bogus-id: 401 Team not authorized OAUTH_027
Failed to get task: task "bogus-id" not found, or not accessible with the current token (HTTP 401 OAUTH_027: Team not authorized)

Other mappings (from the httptest suite):

not authenticated or token invalid — run `cu auth login` (HTTP 401 OAUTH_025: Token invalid)
permission denied for task "abc123" — check your ClickUp access to it (HTTP 403 ACCESS_001: Forbidden)
task "abc123" not found (HTTP 404 ITEM_013: Task not found)
ClickUp rate limit exceeded — wait 30s and try again (HTTP 429 APP_002: Rate limit reached)
ClickUp server error — try again shortly (HTTP 500 SHARD_001: Internal Server Error)
ClickUp rejected the request (HTTP 400 ITEM_102: Status does not exist)

Notes:

  • ClickUp returns 401 OAUTH_027 "Team not authorized" for IDs the token can't see, typos included. Telling the user to run cu auth login would be wrong here, so this case is classified as ErrNotFound. Other 401s (OAUTH_017 missing header, OAUTH_025 invalid token, and so on) map to ErrNotAuthenticated.
  • When its rate-limit state is exhausted, the SDK refuses requests locally with a synthetic 403. These still read as rate limits, not permission errors.
  • Messages stay on one line, so the bulk JSON error field added in feat(output): honour -o json/yaml/csv across every command #52 stays clean.
  • internal/errors: removed the never-called UserError, HandleHTTPError and old APIError/NewAPIError, plus the unused ErrConfigNotFound. IsRetryable and the sentinels used by internal/auth and retry.go stay as they were.
  • Every SDK call in internal/api already went through handleError (16 in client.go, plus customfields.go, status.go, archive.go and teamtasks.go). None were missing.

Tests: internal/api/errors_test.go runs a status-code table through a real Client against httptest. It also covers RateLimitError plus the SDK's synthetic-403 short-circuit, resource context across several endpoints using the production /api/v2/ base path, and the passthrough cases (network, context, already typed).

Checklist

  • ./scripts/ci.sh passes locally (or go test -race ./... at minimum). go test -race ./..., go vet ./... and gosec v2.28.0 are clean. The script's local errcheck step reports existing findings in internal/cmd, none in files this PR touches.
  • Commit messages use conventional prefixes (feat:, fix:, docs:, chore:, ci:, test:)
  • CLI docs regenerated if command help text changed (cu docs markdown --dir docs/site/commands). N/A, no help text changed.
  • Docs updated if user-facing behavior changed. N/A, only error message text changed.

🤖 Generated with Claude Code

Client.handleError was a passthrough, so users saw raw SDK strings such as
"GET https://api.clickup.com/api/v2/task/x: 401 Team not authorized
OAUTH_027". It now converts the SDK's ErrorResponse, RateLimitError and
AbuseRateLimitError into *errors.APIError, which carries a category
sentinel (ErrNotAuthenticated, ErrPermissionDenied, ErrNotFound,
ErrRateLimited, ErrServer, ErrInvalidInput), resource context parsed from
the request path, and the HTTP status/ECODE/ClickUp message in a trailing
parenthetical. The original SDK error stays in the unwrap chain.

ClickUp answers 401 OAUTH_027 "Team not authorized" for IDs the token
cannot see, including typos, so that case reads as not found rather than
telling the user to log in again.

internal/errors drops the never-called UserError and HandleHTTPError and
the unused ErrConfigNotFound in favour of the single APIError type.

Part of #27

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timimsms
timimsms merged commit 891a7ea into main Nov 18, 2026
16 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