u
This commit is contained in:
1 parent
01ea998605
commit
d1b1aafc57
9 files changed
+82
-35
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,24 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"base-framework/pkg/router"
|
||||
"base-framework/pkg/utils/response"
|
||||
"log"
|
||||
)
|
||||
|
||||
// Error 全局业务错误处理中间件
|
||||
func Error() router.HandlerFunc {
|
||||
return func(c *router.Context) {
|
||||
// 执行业务逻辑
|
||||
c.Next()
|
||||
|
||||
// 统一处理错误
|
||||
if errs := c.Errors(); len(errs) > 0 {
|
||||
lastErr := errs[len(errs)-1]
|
||||
log.Printf("[ERROR] %v", lastErr)
|
||||
|
||||
response.Error(c).Send()
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"base-framework/pkg/router"
|
||||
"log"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
func Recover() router.HandlerFunc {
|
||||
return func(c *router.Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Printf("[PANIC] %v\n%s", err, debug.Stack())
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user