fix: log response body when parsed upstream error message is empty

When an upstream error response parses as valid JSON but yields no usable
error message (e.g. an aggregator gateway returning {"error":{"message":""}}),
RelayErrorHandler previously produced a bare "bad response status code N"
error with no trace of the original body, making the failure undiagnosable.
Log the body preview in that case, mirroring the existing behavior for
unparseable bodies.
This commit is contained in:
bigsong
2026-07-24 14:10:32 +08:00
committed by GitHub
parent cbd9b30aa4
commit 84a79b6807
+7 -1
View File
@@ -123,7 +123,13 @@ func RelayErrorHandler(ctx context.Context, resp *http.Response, showBodyWhenFai
return
}
}
newApiErr = types.NewOpenAIError(errors.New(errResponse.ToMessage()), types.ErrorCodeBadResponseStatusCode, resp.StatusCode)
message := errResponse.ToMessage()
if message == "" {
// The body parsed as JSON but carried no usable error message; log the
// raw body so the upstream failure remains diagnosable.
logger.LogError(ctx, fmt.Sprintf("bad response status code %d with empty error message, body: %s", resp.StatusCode, responseBodyPreview))
}
newApiErr = types.NewOpenAIError(errors.New(message), types.ErrorCodeBadResponseStatusCode, resp.StatusCode)
if showBodyWhenFail {
newApiErr.Err = buildErrWithBody(newApiErr.Error())
}