修复报错

This commit is contained in:
2025-11-05 23:10:51 +08:00
parent 10b123ab93
commit a1f7c09b2a
12 changed files with 51 additions and 44 deletions

View File

@@ -1,19 +1,21 @@
package management
import (
"context"
"errors"
"fmt"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"github.com/labstack/echo/v4"
)
// mapAndSendError 统一映射服务层错误并发送响应。
// 这个函数将服务层返回的错误转换为控制器层应返回的HTTP状态码和审计信息。
func mapAndSendError(c *PigBatchController, ctx echo.Context, action string, err error, id uint) error {
func mapAndSendError(reqContext context.Context, c *PigBatchController, ctx echo.Context, action string, err error, id uint) error {
if errors.Is(err, service.ErrPigBatchNotFound) ||
errors.Is(err, service.ErrPenNotFound) ||
errors.Is(err, service.ErrPenNotAssociatedWithBatch) {
@@ -26,7 +28,7 @@ func mapAndSendError(c *PigBatchController, ctx echo.Context, action string, err
errors.Is(err, service.ErrPenNotEmpty) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
} else {
c.logger.Errorf("操作[%s]业务逻辑失败: %v", action, err)
logs.GetLogger(reqContext).Errorf("操作[%s]业务逻辑失败: %v", action, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, fmt.Sprintf("操作失败: %v", err), action, err.Error(), id)
}
}
@@ -86,6 +88,7 @@ func extractOperatorAndPrimaryID(
// handleAPIRequest 封装了控制器中处理带有请求体和路径参数的API请求的通用逻辑。
// 它负责请求体绑定、操作员ID获取、服务层调用、错误映射和响应发送。
func handleAPIRequest[Req any](
reqContext context.Context,
c *PigBatchController,
ctx echo.Context,
action string,
@@ -108,7 +111,7 @@ func handleAPIRequest[Req any](
// 3. 执行服务层逻辑
err = serviceExecutor(ctx, operatorID, primaryID, reqDTO)
if err != nil {
return mapAndSendError(c, ctx, action, err, primaryID)
return mapAndSendError(reqContext, c, ctx, action, err, primaryID)
}
// 4. 发送成功响应
@@ -117,6 +120,7 @@ func handleAPIRequest[Req any](
// handleNoBodyAPIRequest 封装了处理不带请求体但有路径参数和操作员ID的API请求的通用逻辑。
func handleNoBodyAPIRequest(
reqContext context.Context,
c *PigBatchController,
ctx echo.Context,
action string,
@@ -133,7 +137,7 @@ func handleNoBodyAPIRequest(
// 2. 执行服务层逻辑
err = serviceExecutor(ctx, operatorID, primaryID)
if err != nil {
return mapAndSendError(c, ctx, action, err, primaryID)
return mapAndSendError(reqContext, c, ctx, action, err, primaryID)
}
// 3. 发送成功响应
@@ -142,6 +146,7 @@ func handleNoBodyAPIRequest(
// handleAPIRequestWithResponse 封装了控制器中处理带有请求体、路径参数并返回响应DTO的API请求的通用逻辑。
func handleAPIRequestWithResponse[Req any, Resp any](
reqContext context.Context,
c *PigBatchController,
ctx echo.Context,
action string,
@@ -164,7 +169,7 @@ func handleAPIRequestWithResponse[Req any, Resp any](
// 3. 执行服务层逻辑
respDTO, err := serviceExecutor(ctx, operatorID, primaryID, reqDTO)
if err != nil {
return mapAndSendError(c, ctx, action, err, primaryID)
return mapAndSendError(reqContext, c, ctx, action, err, primaryID)
}
// 4. 发送成功响应
@@ -173,6 +178,7 @@ func handleAPIRequestWithResponse[Req any, Resp any](
// handleNoBodyAPIRequestWithResponse 封装了处理不带请求体但有路径参数和操作员ID并返回响应DTO的API请求的通用逻辑。
func handleNoBodyAPIRequestWithResponse[Resp any](
reqContext context.Context,
c *PigBatchController,
ctx echo.Context,
action string,
@@ -189,7 +195,7 @@ func handleNoBodyAPIRequestWithResponse[Resp any](
// 2. 执行服务层逻辑
respDTO, err := serviceExecutor(ctx, operatorID, primaryID)
if err != nil {
return mapAndSendError(c, ctx, action, err, primaryID)
return mapAndSendError(reqContext, c, ctx, action, err, primaryID)
}
// 3. 发送成功响应
@@ -198,6 +204,7 @@ func handleNoBodyAPIRequestWithResponse[Resp any](
// handleQueryAPIRequestWithResponse 封装了处理带有查询参数并返回响应DTO的API请求的通用逻辑。
func handleQueryAPIRequestWithResponse[Query any, Resp any](
reqContext context.Context,
c *PigBatchController,
ctx echo.Context,
action string,
@@ -220,7 +227,7 @@ func handleQueryAPIRequestWithResponse[Query any, Resp any](
respDTO, err := serviceExecutor(ctx, operatorID, queryDTO)
if err != nil {
// 对于列表查询通常没有primaryID所以传递0
return mapAndSendError(c, ctx, action, err, 0)
return mapAndSendError(reqContext, c, ctx, action, err, 0)
}
// 4. 发送成功响应