Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.
middleware: handle non-JSON error responses gracefully (#14828)
Bruce MacDonaldabf8e8e9c81c
diff --git a/middleware/anthropic.go b/middleware/anthropic.goindex 7a064e142..6e1f66ea8 100644--- a/middleware/anthropic.go+++ b/middleware/anthropic.go@@ -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}diff --git a/middleware/openai.go b/middleware/openai.goindex ca694eb26..8527f00de 100644--- a/middleware/openai.go+++ b/middleware/openai.go@@ -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}diff --git a/middleware/openai_test.go b/middleware/openai_test.goindex 06c7ada56..39fc2c907 100644--- a/middleware/openai_test.go+++ b/middleware/openai_test.go@@ -1222,7 +1222,7 @@ func TestRetrieveMiddleware(t *testing.T) {"code": null,"message": "model not found","param": null,- "type": "api_error"+ "type": "invalid_request_error"}}`,},