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() } } }