Skip to content

fix(sdk): parse API error responses into typed errors#492

Open
poyrazK wants to merge 2 commits intomainfrom
fix/sdk-error-parsing
Open

fix(sdk): parse API error responses into typed errors#492
poyrazK wants to merge 2 commits intomainfrom
fix/sdk-error-parsing

Conversation

@poyrazK
Copy link
Copy Markdown
Owner

@poyrazK poyrazK commented May 7, 2026

Summary

Fix SDK error parsing so CLI commands show meaningful API error messages instead of generic "An unexpected error occurred".

Problem

When the API returns an error response like {"error": {"type": "BUCKET_NOT_FOUND", "message": "bucket not found"}}, the SDK was returning a plain string "api error: {json...}" instead of parsing the error type and message.

This caused CLI commands to show:

  • "Error: api error: {"error": {"type": "BUCKET_NOT_FOUND"...}"

Instead of:

  • "Error: bucket not found"

Solution

Parse API error responses in pkg/sdk/client.go and return proper errors.Error types:

  • Add parseAPIError() function that parses JSON error responses
  • Map API error type strings (case-insensitive) to errors.Type
  • Update all HTTP methods to use parseAPIError() instead of fmt.Errorf

Changes

  • pkg/sdk/client.go: Add error type mapping and parseAPIError() function
  • pkg/sdk/compute_test.go: Update test to verify proper error type

Verification

  • go build ./... passes
  • go test ./pkg/sdk/... passes
  • CI passes

Fixes #472

When the API returns an error response with JSON like:
{"error": {"type": "BUCKET_NOT_FOUND", "message": "bucket not found"}}

The SDK now parses this and returns a proper errors.Error type instead
of a generic "api error: {json}" string.

This fixes the issue where CLI commands show "An unexpected error occurred"
instead of the actual API error message. Now callers can use errors.Is()
to check for specific error types.

Changes:
- Add parseAPIError() function to client.go
- Update getContext, postContext, deleteWithContext, putWithContext, patchWithContext
- Map API error type strings (case-insensitive) to errors.Type
- Update TestClientAPIError to verify proper error type parsing

Fixes #472
Copilot AI review requested due to automatic review settings May 7, 2026 18:46
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 7, 2026

Warning

Rate limit exceeded

@poyrazK has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 42 minutes and 59 seconds before requesting another review.

To continue reviewing without waiting, purchase usage credits in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: cd7f3eec-74fe-4d95-9445-62f309cf7e93

📥 Commits

Reviewing files that changed from the base of the PR and between 5faabcc and 47c27da.

📒 Files selected for processing (3)
  • pkg/sdk/client.go
  • pkg/sdk/client_test.go
  • pkg/sdk/compute_test.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/sdk-error-parsing

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Add 11 tests verifying:
- parseAPIError correctly parses various error types (BUCKET_NOT_FOUND, NOT_FOUND, INVALID_INPUT, FORBIDDEN)
- Lowercase error types work
- Unknown error types fall back to Internal
- Invalid JSON falls back to Internal
- Missing error field falls back to Internal
- Full HTTP methods (get, post, delete) correctly parse API errors

All tests pass.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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.

CLI storage lifecycle set returns INTERNAL error

2 participants