Skip to content

Commit 9f0ccfb

Browse files
committed
fix: 修复 main-openai.go 中所有未检查的错误返回值
1 parent dd9b095 commit 9f0ccfb

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

main-openai.go

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ func handleStreamResponse(w http.ResponseWriter, resp *http.Response, model stri
340340

341341
// 转换为 OpenAI 格式
342342
if openaiData, err := convertAnthropicStreamToOpenAI(eventType, data, model); err == nil && openaiData != "" {
343-
fmt.Fprintf(w, "data: %s\n\n", openaiData)
343+
if _, err := fmt.Fprintf(w, "data: %s\n\n", openaiData); err != nil {
344+
log.Printf("错误: 写入流式数据失败: %v", err)
345+
return
346+
}
344347
flusher.Flush()
345348
}
346349
}
@@ -349,7 +352,10 @@ func handleStreamResponse(w http.ResponseWriter, resp *http.Response, model stri
349352
}
350353

351354
// 发送结束标记
352-
fmt.Fprint(w, "data: [DONE]\n\n")
355+
if _, err := fmt.Fprint(w, "data: [DONE]\n\n"); err != nil {
356+
log.Printf("错误: 写入结束标记失败: %v", err)
357+
return
358+
}
353359
flusher.Flush()
354360

355361
if err := scanner.Err(); err != nil {
@@ -392,7 +398,9 @@ func chatCompletionsHandler(w http.ResponseWriter, r *http.Request) {
392398
http.Error(w, `{"error": {"message": "Failed to read request body", "type": "invalid_request_error"}}`, http.StatusBadRequest)
393399
return
394400
}
395-
r.Body.Close()
401+
if err := r.Body.Close(); err != nil {
402+
log.Printf("警告: 关闭请求体失败: %v", err)
403+
}
396404

397405
// 解析OpenAI格式请求
398406
var openaiReq map[string]interface{}
@@ -467,7 +475,11 @@ func chatCompletionsHandler(w http.ResponseWriter, r *http.Request) {
467475
http.Error(w, `{"error": {"message": "Proxy request failed", "type": "server_error"}}`, http.StatusBadGateway)
468476
return
469477
}
470-
defer resp.Body.Close()
478+
defer func() {
479+
if err := resp.Body.Close(); err != nil {
480+
log.Printf("警告: 关闭响应体失败: %v", err)
481+
}
482+
}()
471483

472484
log.Printf("收到响应: 状态码 %d", resp.StatusCode)
473485

@@ -487,9 +499,13 @@ func chatCompletionsHandler(w http.ResponseWriter, r *http.Request) {
487499
"type": "api_error",
488500
},
489501
}
490-
json.NewEncoder(w).Encode(openaiError)
502+
if err := json.NewEncoder(w).Encode(openaiError); err != nil {
503+
log.Printf("错误: 编码错误响应失败: %v", err)
504+
}
491505
} else {
492-
w.Write(respBody)
506+
if _, err := w.Write(respBody); err != nil {
507+
log.Printf("错误: 写入错误响应失败: %v", err)
508+
}
493509
}
494510
return
495511
}
@@ -522,18 +538,22 @@ func chatCompletionsHandler(w http.ResponseWriter, r *http.Request) {
522538

523539
w.Header().Set("Content-Type", "application/json")
524540
w.WriteHeader(http.StatusOK)
525-
json.NewEncoder(w).Encode(openaiResp)
541+
if err := json.NewEncoder(w).Encode(openaiResp); err != nil {
542+
log.Printf("错误: 编码响应失败: %v", err)
543+
}
526544
}
527545
}
528546

529547
// 健康检查端点
530548
func healthHandler(w http.ResponseWriter, r *http.Request) {
531549
w.Header().Set("Content-Type", "application/json")
532-
json.NewEncoder(w).Encode(map[string]interface{}{
550+
if err := json.NewEncoder(w).Encode(map[string]interface{}{
533551
"status": "healthy",
534552
"timestamp": time.Now().UTC().Format(time.RFC3339),
535553
"uptime": time.Since(startTime).Seconds(),
536-
})
554+
}); err != nil {
555+
log.Printf("错误: 编码健康检查响应失败: %v", err)
556+
}
537557
}
538558

539559
// API 文档端点
@@ -543,11 +563,15 @@ func docsHandler(w http.ResponseWriter, r *http.Request) {
543563
if err != nil {
544564
// 如果文件不存在,返回内嵌的文档
545565
w.Header().Set("Content-Type", "text/html; charset=utf-8")
546-
fmt.Fprint(w, getEmbeddedDocs())
566+
if _, err := fmt.Fprint(w, getEmbeddedDocs()); err != nil {
567+
log.Printf("错误: 写入文档失败: %v", err)
568+
}
547569
return
548570
}
549571
w.Header().Set("Content-Type", "text/html; charset=utf-8")
550-
w.Write(htmlContent)
572+
if _, err := w.Write(htmlContent); err != nil {
573+
log.Printf("错误: 写入HTML内容失败: %v", err)
574+
}
551575
}
552576

553577
// 内嵌的 API 文档
@@ -887,19 +911,23 @@ func main() {
887911
docsHandler(recorder, r)
888912
} else if path == "/" {
889913
w.Header().Set("Content-Type", "application/json")
890-
json.NewEncoder(w).Encode(map[string]string{
914+
if err := json.NewEncoder(w).Encode(map[string]string{
891915
"service": "Factory OpenAI-Compatible Proxy",
892916
"version": "1.0",
893-
})
917+
}); err != nil {
918+
log.Printf("错误: 编码根路径响应失败: %v", err)
919+
}
894920
} else {
895921
w.Header().Set("Content-Type", "application/json")
896922
w.WriteHeader(http.StatusNotFound)
897-
json.NewEncoder(w).Encode(map[string]interface{}{
923+
if err := json.NewEncoder(w).Encode(map[string]interface{}{
898924
"error": map[string]string{
899925
"message": "Invalid endpoint. Use /v1/chat/completions or /health",
900926
"type": "invalid_request_error",
901927
},
902-
})
928+
}); err != nil {
929+
log.Printf("错误: 编码404响应失败: %v", err)
930+
}
903931
}
904932

905933
duration := time.Since(start)

0 commit comments

Comments
 (0)