middleware: handle non-JSON error responses gracefully (#14828)

writeError in both OpenAI and Anthropic middleware writers would return
a raw json.SyntaxError when the error payload wasn't valid JSON (e.g.
"invalid character 'e' looking for beginning of value"). Fall back to
using the raw bytes as the error message instead.

Also use the actual HTTP status code rather than hardcoding 500, so
error types map correctly
This commit is contained in:
Bruce MacDonald
2026-03-13 14:50:49 -07:00
committed by GitHub
parent f3f31a8192
commit abf8e8e9c8
3 changed files with 10 additions and 9 deletions

View File

@@ -34,12 +34,13 @@ func (w *AnthropicWriter) writeError(data []byte) (int, error) {
Error string `json:"error"`
}
if err := json.Unmarshal(data, &errData); err != nil {
return 0, err
// If the error response isn't valid JSON, use the raw bytes as the
// error message rather than surfacing a confusing JSON parse error.
errData.Error = string(data)
}
w.ResponseWriter.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w.ResponseWriter).Encode(anthropic.NewError(w.Status(), errData.Error))
if err != nil {
if err := json.NewEncoder(w.ResponseWriter).Encode(anthropic.NewError(w.Status(), errData.Error)); err != nil {
return 0, err
}

View File

@@ -54,14 +54,14 @@ type EmbedWriter struct {
func (w *BaseWriter) writeError(data []byte) (int, error) {
var serr api.StatusError
err := json.Unmarshal(data, &serr)
if err != nil {
return 0, err
if err := json.Unmarshal(data, &serr); err != nil {
// If the error response isn't valid JSON, use the raw bytes as the
// error message rather than surfacing a confusing JSON parse error.
serr.ErrorMessage = string(data)
}
w.ResponseWriter.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w.ResponseWriter).Encode(openai.NewError(http.StatusInternalServerError, serr.Error()))
if err != nil {
if err := json.NewEncoder(w.ResponseWriter).Encode(openai.NewError(w.ResponseWriter.Status(), serr.Error())); err != nil {
return 0, err
}

View File

@@ -1222,7 +1222,7 @@ func TestRetrieveMiddleware(t *testing.T) {
"code": null,
"message": "model not found",
"param": null,
"type": "api_error"
"type": "invalid_request_error"
}
}`,
},