Files
workspace/code/go-project/base-farmework/pkg/middleware/error.go
T
2025-08-14 17:28:32 +08:00

29 lines
561 B
Go

package middleware
import (
"base-framework/pkg/router"
"fmt"
"net/http"
)
func Error() router.HandlerFunc {
return func(c *router.Context) {
c.Next() // 执行后续 Handler
errs := c.Errors()
if len(errs) > 0 {
lastErr := errs[len(errs)-1]
// 打印错误信息和堆栈
fmt.Printf("[ERROR] %v\nStack trace:\n%s\n", lastErr.Error(), lastErr.StackTrace())
// 返回前端安全信息
c.JSON(http.StatusInternalServerError, map[string]interface{}{
"code": "FAIL",
"message": lastErr.Error(),
})
c.Abort()
}
}
}