refactor(api): unify error handling and split upload/oauth god modules

Replace c.JSON(200, response.Err) and middleware gin.H bypasses with
response.Abort* helpers so errors flow through Gin Error chain and
ErrorHandlerMiddleware for OTel trace correlation.

Split oauth/sources.go into domain-focused files and decompose upload
into handler/filesrv/stats/task/cache/storage/util subpackages with a
root facade preserving existing import paths.
This commit is contained in:
ryan
2026-06-18 10:51:25 +08:00
parent f826807cf1
commit 9af84c8ed6
62 changed files with 2040 additions and 1696 deletions
+2 -25
View File
@@ -7,7 +7,6 @@ package router
import (
"context"
"errors"
"net/http"
"strconv"
"strings"
@@ -106,29 +105,7 @@ func corsMiddleware() gin.HandlerFunc {
}
}
// errorHandlerMiddleware 捕获 c.Errors 并统一格式化为 JSON 返回给客户端,同时将其记录到 Span 异常中
// errorHandlerMiddleware 委托给 response.ErrorHandlerMiddleware,保持路由层单一入口。
func errorHandlerMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
if len(c.Errors) > 0 {
err := c.Errors.Last().Err
span := trace.SpanFromContext(c.Request.Context())
// 1. 如果有活跃的 Span,将错误信息记录到 Trace 中,并把 Span 状态置为 Error
if span.IsRecording() {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
// 2. 将错误转化为统一的 JSON 格式响应给客户端
var apiErr *response.APIError
if errors.As(err, &apiErr) {
c.JSON(apiErr.Code, response.Err(apiErr.Msg))
} else {
// 兜底策略:未知的系统级错误
c.JSON(http.StatusInternalServerError, response.Err("内部系统错误"))
}
}
}
return response.ErrorHandlerMiddleware()
}