1
0

refactor: 后端代码质量优化 - 复用公共库、使用标准库、类型安全错误判断

## 高优先级修复
- stats_service_impl: 使用 strings.SplitN 替代错误的索引分割
- provider_handler: 使用 errors.Is(err, gorm.ErrDuplicatedKey) 替代字符串匹配
- client: 重写 isNetworkError 使用 errors.As/Is 类型安全判断
- proxy_handler: 使用 encoding/json 标准库解析 JSON(extractModelName、isStreamRequest)

## 中优先级修复
- stats_handler: 添加 parseDateParam 辅助函数消除重复日期解析
- pkg/errors: 新增 ErrRequestCreate/Send/ResponseRead 错误类型和 WithCause 方法
- client: 使用结构化错误替代 fmt.Errorf
- ConversionEngine: logger 依赖注入,替换所有 zap.L() 调用

## 低优先级修复
- encoder: 删除 joinStrings,使用 strings.Join
- adapter: 删除 modelInfoRegex 正则,使用 isModelInfoPath 字符串函数

## 文档更新
- README.md: 添加公共库使用指南和编码规范章节
- specs: 同步 delta specs 到 main specs(error-handling、structured-logging、request-validation)

## 归档
- openspec/changes/archive/2026-04-20-refactor-backend-code-quality/
This commit is contained in:
2026-04-20 16:42:48 +08:00
parent bc1ee612d9
commit d92db73937
35 changed files with 1493 additions and 267 deletions

View File

@@ -3,15 +3,18 @@ package provider
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"strings"
"syscall"
"time"
"go.uber.org/zap"
"nex/backend/internal/conversion"
pkgErrors "nex/backend/pkg/errors"
)
// StreamConfig 流式处理配置
@@ -72,7 +75,7 @@ func (c *Client) Send(ctx context.Context, spec conversion.HTTPRequestSpec) (*co
httpReq, err := http.NewRequestWithContext(ctx, spec.Method, spec.URL, bodyReader)
if err != nil {
return nil, fmt.Errorf("创建请求失败: %w", err)
return nil, pkgErrors.ErrRequestCreate.WithCause(err)
}
for k, v := range spec.Headers {
@@ -86,13 +89,13 @@ func (c *Client) Send(ctx context.Context, spec conversion.HTTPRequestSpec) (*co
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("发送请求失败: %w", err)
return nil, pkgErrors.ErrRequestSend.WithCause(err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取响应失败: %w", err)
return nil, pkgErrors.ErrResponseRead.WithCause(err)
}
respHeaders := make(map[string]string)
@@ -120,7 +123,7 @@ func (c *Client) SendStream(ctx context.Context, spec conversion.HTTPRequestSpec
httpReq, err := http.NewRequestWithContext(streamCtx, spec.Method, spec.URL, bodyReader)
if err != nil {
cancel()
return nil, fmt.Errorf("创建请求失败: %w", err)
return nil, pkgErrors.ErrRequestCreate.WithCause(err)
}
for k, v := range spec.Headers {
@@ -130,7 +133,7 @@ func (c *Client) SendStream(ctx context.Context, spec conversion.HTTPRequestSpec
resp, err := c.httpClient.Do(httpReq)
if err != nil {
cancel()
return nil, fmt.Errorf("发送请求失败: %w", err)
return nil, pkgErrors.ErrRequestSend.WithCause(err)
}
if resp.StatusCode != http.StatusOK {
@@ -226,10 +229,46 @@ func isNetworkError(err error) bool {
if err == nil {
return false
}
errStr := err.Error()
return strings.Contains(errStr, "connection reset") ||
strings.Contains(errStr, "broken pipe") ||
strings.Contains(errStr, "network") ||
strings.Contains(errStr, "timeout") ||
strings.Contains(errStr, "EOF")
// 检查标准库定义的网络错误类型
var netErr net.Error
if errors.As(err, &netErr) {
return true
}
// 检查操作错误
var opErr *net.OpError
if errors.As(err, &opErr) {
// 检查具体的系统错误
if opErr.Err != nil {
// 连接重置
if errors.Is(opErr.Err, syscall.ECONNRESET) {
return true
}
// 断管
if errors.Is(opErr.Err, syscall.EPIPE) {
return true
}
// 超时
if errors.Is(opErr.Err, syscall.ETIMEDOUT) {
return true
}
}
return true
}
// 检查上下文错误
if errors.Is(err, context.DeadlineExceeded) {
return true
}
if errors.Is(err, context.Canceled) {
return true
}
// 检查 EOF
if errors.Is(err, io.EOF) {
return true
}
return false
}