fix: improve OpenAI error body capture and log request body on 400
- Use resp.Body() instead of resp.RawBody() for non-streaming error responses - Fall back to RawBody() for streaming responses - Log the full request body on API errors for debugging
This commit is contained in:
@@ -72,11 +72,14 @@ func (p *OpenAIProvider) ChatCompletion(ctx context.Context, req *models.Unified
|
|||||||
if !resp.IsSuccess() {
|
if !resp.IsSuccess() {
|
||||||
msg := resp.String()
|
msg := resp.String()
|
||||||
if msg == "" {
|
if msg == "" {
|
||||||
if body, err := io.ReadAll(resp.RawBody()); err == nil {
|
if b := resp.Body(); len(b) > 0 {
|
||||||
msg = string(body)
|
msg = string(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Log the request body for debugging
|
||||||
|
reqJSON, _ := json.Marshal(body)
|
||||||
log.Printf("OpenAI API Error (%d): %s", resp.StatusCode(), msg)
|
log.Printf("OpenAI API Error (%d): %s", resp.StatusCode(), msg)
|
||||||
|
log.Printf("OpenAI request body: %s", string(reqJSON))
|
||||||
return nil, fmt.Errorf("OpenAI API error (%d): %s", resp.StatusCode(), msg)
|
return nil, fmt.Errorf("OpenAI API error (%d): %s", resp.StatusCode(), msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,11 +185,18 @@ func (p *OpenAIProvider) ChatCompletionStream(ctx context.Context, req *models.U
|
|||||||
if !resp.IsSuccess() {
|
if !resp.IsSuccess() {
|
||||||
msg := resp.String()
|
msg := resp.String()
|
||||||
if msg == "" {
|
if msg == "" {
|
||||||
if body, err := io.ReadAll(resp.RawBody()); err == nil {
|
if b := resp.Body(); len(b) > 0 {
|
||||||
msg = string(body)
|
msg = string(b)
|
||||||
|
}
|
||||||
|
if msg == "" {
|
||||||
|
if b, err := io.ReadAll(resp.RawBody()); err == nil {
|
||||||
|
msg = string(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
reqJSON, _ := json.Marshal(body)
|
||||||
log.Printf("OpenAI API Error (%d): %s", resp.StatusCode(), msg)
|
log.Printf("OpenAI API Error (%d): %s", resp.StatusCode(), msg)
|
||||||
|
log.Printf("OpenAI request body: %s", string(reqJSON))
|
||||||
return nil, fmt.Errorf("OpenAI API error (%d): %s", resp.StatusCode(), msg)
|
return nil, fmt.Errorf("OpenAI API error (%d): %s", resp.StatusCode(), msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user