Skip to content

Commit

Permalink
Add 路由中间件 - 捕获异常
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Sep 10, 2019
1 parent 8dea1c5 commit b3a9091
Show file tree
Hide file tree
Showing 3 changed files with 962 additions and 15 deletions.
28 changes: 14 additions & 14 deletions app/route/middleware/exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ func SetUp() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
subject := fmt.Sprintf("【重要错误】%s 项目出错了!", config.AppName)

body := fmt.Sprintf("<b>ErrorMessage: </b> %s \n", err)
body += fmt.Sprintf("<b>RequestTime: </b> %s \n", util.GetCurrentDate())
body += fmt.Sprintf("<b>RequestURL: </b> %s %s \n", c.Request.Method, c.Request.RequestURI)
body += fmt.Sprintf("<b>RequestProto: </b> %s \n", c.Request.Proto)
body += fmt.Sprintf("<b>RequestReferer: </b> %s \n", c.Request.Referer())
body += fmt.Sprintf("<b>RequestUA: </b> %s \n", c.Request.UserAgent())
body += fmt.Sprintf("<b>RequestClientIp: </b> %s \n", c.ClientIP())
body += fmt.Sprintf("<b>DebugStack: </b> %s \n", string(debug.Stack()))

bodyHtml := ""
for _, v := range strings.Split(body, "\n") {
bodyHtml += v + "<br>"
DebugStack := ""
for _, v := range strings.Split(string(debug.Stack()), "\n") {
DebugStack += v + "<br>"
}
_ = util.SendMail(config.ErrorNotifyUser, subject, bodyHtml)

subject := fmt.Sprintf("【重要错误】%s 项目出错了!", config.AppName)

body := strings.ReplaceAll(MailTemplate, "{ErrorMsg}", fmt.Sprintf("%s", err))
body = strings.ReplaceAll(body, "{RequestTime}", util.GetCurrentDate())
body = strings.ReplaceAll(body, "{RequestURL}", c.Request.Method + " " + c.Request.Host + c.Request.RequestURI)
body = strings.ReplaceAll(body, "{RequestUA}", c.Request.UserAgent())
body = strings.ReplaceAll(body, "{RequestIP}", c.ClientIP())
body = strings.ReplaceAll(body, "{DebugStack}", DebugStack)

_ = util.SendMail(config.ErrorNotifyUser, subject, body)

utilGin := util.Gin{Ctx: c}
utilGin.Response(500, "系统异常,请联系管理员!", nil)
Expand Down
Loading

0 comments on commit b3a9091

Please sign in to comment.