Skip to content

Commit 747c989

Browse files
committed
modelerrors: remove provider SDK imports from ExtractHTTPStatusCode
Now that all providers (Anthropic, OpenAI, Gemini) wrap their errors in *StatusError via WrapHTTPError, the provider-specific type assertions (anthropic.Error, genai.APIError) in ExtractHTTPStatusCode are redundant. ClassifyModelError already checks *StatusError first. Replace the SDK-specific checks with a *StatusError check, keeping the regex fallback for unwrapped errors. This removes the coupling between the error classification package and provider-specific SDKs. Assisted-By: docker-agent
1 parent f18f5b5 commit 747c989

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

pkg/modelerrors/modelerrors.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import (
1616
"strconv"
1717
"strings"
1818
"time"
19-
20-
"github.com/anthropics/anthropic-sdk-go"
21-
"google.golang.org/genai"
2219
)
2320

2421
// Backoff and retry-after configuration constants.
@@ -161,27 +158,25 @@ func IsContextOverflowError(err error) bool {
161158
// statusCodeRegex matches HTTP status codes in error messages (e.g., "429", "500", ": 429 ")
162159
var statusCodeRegex = regexp.MustCompile(`\b([45]\d{2})\b`)
163160

164-
// ExtractHTTPStatusCode attempts to extract an HTTP status code from the error.
165-
// Checks in order:
166-
// 1. Known provider SDK error types (Anthropic, Gemini)
167-
// 2. Regex parsing of error message (fallback for OpenAI and others)
161+
// ExtractHTTPStatusCode attempts to extract an HTTP status code from the error
162+
// using regex parsing of the error message. This is a fallback for providers
163+
// whose errors are not yet wrapped in *StatusError (the preferred path).
164+
//
165+
// The regex matches 4xx/5xx codes at word boundaries
166+
// (e.g., "429 Too Many Requests", "500 Internal Server Error").
168167
// Returns 0 if no status code found.
169168
func ExtractHTTPStatusCode(err error) int {
170169
if err == nil {
171170
return 0
172171
}
173172

174-
// Check Anthropic SDK error type (public)
175-
if anthropicErr, ok := errors.AsType[*anthropic.Error](err); ok {
176-
return anthropicErr.StatusCode
177-
}
178-
179-
// Check Google Gemini SDK error type (public)
180-
if geminiErr, ok := errors.AsType[*genai.APIError](err); ok {
181-
return geminiErr.Code
173+
// Check for *StatusError first (preferred structured path).
174+
var statusErr *StatusError
175+
if errors.As(err, &statusErr) {
176+
return statusErr.StatusCode
182177
}
183178

184-
// For other providers (OpenAI, etc.), extract from error message using regex
179+
// Fallback: extract from error message using regex.
185180
// OpenAI SDK error format: `POST "/v1/...": 429 Too Many Requests {...}`
186181
matches := statusCodeRegex.FindStringSubmatch(err.Error())
187182
if len(matches) >= 2 {

0 commit comments

Comments
 (0)