From 9117c2a4ec320fb8eb817117ceadd462d0194768 Mon Sep 17 00:00:00 2001 From: erio Date: Thu, 5 Mar 2026 19:37:38 +0800 Subject: [PATCH] fix: include upstream error details in usage API error response When FetchUsageWithOptions receives a non-200 response from the Anthropic API (e.g. 429 Rate Limited, 401 Unauthorized), the error was wrapped with fmt.Errorf which infraerrors.FromError cannot recognize, causing a generic "internal error" message with no details. Replace fmt.Errorf with infraerrors.New(500, "UPSTREAM_ERROR", msg) so the upstream error details (status code + body) are included in the 500 response message. The HTTP status remains 500 to avoid interfering with frontend auth routing (e.g. 401 would trigger JWT expiry redirect). --- backend/internal/repository/claude_usage_service.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/internal/repository/claude_usage_service.go b/backend/internal/repository/claude_usage_service.go index f6054828..1264f6bb 100644 --- a/backend/internal/repository/claude_usage_service.go +++ b/backend/internal/repository/claude_usage_service.go @@ -8,6 +8,7 @@ import ( "net/http" "time" + infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors" "github.com/Wei-Shaw/sub2api/internal/pkg/httpclient" "github.com/Wei-Shaw/sub2api/internal/service" ) @@ -95,7 +96,8 @@ func (s *claudeUsageService) FetchUsageWithOptions(ctx context.Context, opts *se if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) - return nil, fmt.Errorf("API returned status %d: %s", resp.StatusCode, string(body)) + msg := fmt.Sprintf("API returned status %d: %s", resp.StatusCode, string(body)) + return nil, infraerrors.New(http.StatusInternalServerError, "UPSTREAM_ERROR", msg) } var usageResp service.ClaudeUsageResponse