fix(sdk): parse API error responses into typed errors#492
fix(sdk): parse API error responses into typed errors#492
Conversation
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
|
Warning Rate limit exceeded
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 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 configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
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.
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:
Instead of:
Solution
Parse API error responses in
pkg/sdk/client.goand return propererrors.Errortypes:parseAPIError()function that parses JSON error responseserrors.TypeparseAPIError()instead offmt.ErrorfChanges
pkg/sdk/client.go: Add error type mapping andparseAPIError()functionpkg/sdk/compute_test.go: Update test to verify proper error typeVerification
go build ./...passesgo test ./pkg/sdk/...passesFixes #472