Browse Source

添加recover中间件

pull/2/head
zhangxin 3 years ago
parent
commit
26c941eb2f
2 changed files with 35 additions and 1 deletions
  1. +34
    -0
      web/middleware/middleware.go
  2. +1
    -1
      web/routes/routes.go

+ 34
- 0
web/middleware/middleware.go View File

@ -1,11 +1,16 @@
package middleware
import (
"fmt"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"lapp_-wy/conf"
"lapp_-wy/utils"
"lapp_-wy/web/middleware/casbins"
"lapp_-wy/web/middleware/glog"
"lapp_-wy/web/middleware/jwts"
"lapp_-wy/web/supports"
"runtime"
"strings"
)
@ -54,3 +59,32 @@ func checkURL(reqPath string) bool {
}
return false
}
/**
* @Description: 错误信息处理
* @Author: zhangxin
* @Date: 2021/03/23
*/
func Recover(ctx iris.Context) {
defer func() {
if err := recover(); err != nil {
if ctx.IsStopped() {
return
}
var stacktrace string
for i := 1; ; i++ {
_, f, l, got := runtime.Caller(i)
if !got {
break
}
stacktrace += fmt.Sprintf("%s:%d\n", f, l)
}
glog.ErrorExtln("recovery","recovery", stacktrace)
// 打印错误日志
supports.Error(ctx, iris.StatusInternalServerError, "操作出错请重试", nil)
return
}
}()
ctx.Next()
}

+ 1
- 1
web/routes/routes.go View File

@ -13,10 +13,10 @@ func Hub(app *iris.Application) {
/* 定义路由 */
main := app.Party("/", cors.Mycors()).AllowMethods(iris.MethodOptions)
//中间件,验证token和权限路径
main.Use(middleware.Recover)
main.Use(middleware.ServeHTTP)
home := main.Party("/")
home.Get("/sysMenu", controllers.DynamicMenu) // 获取动态菜单
// 用户API模块


Loading…
Cancel
Save