Skip to content

Commit 9fc8c99

Browse files
committed
[A] 合理的错误处理
1 parent f8e33d5 commit 9fc8c99

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

‎router/router_conf.go‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,18 @@ func midRecover(next echo.HandlerFunc) echo.HandlerFunc {
5555
// HTTPErrorHandler 全局错误捕捉
5656
func HTTPErrorHandler(err error, ctx echo.Context) {
5757
if !ctx.Response().Committed {
58-
if strings.Contains(err.Error(), "404") {
59-
// ctx.NoContent(404)
60-
ctx.HTML(404, html404)
61-
// ctx.Redirect(302, "/404.html")
58+
if he, ok := err.(*echo.HTTPError); ok {
59+
if he.Code == 404 {
60+
if strings.HasPrefix(ctx.Request().URL.Path, "/static") || strings.HasPrefix(ctx.Request().URL.Path, "/dist") {
61+
ctx.NoContent(404)
62+
} else {
63+
ctx.HTML(404, html404)
64+
}
65+
} else {
66+
ctx.JSON(utils.NewErrSvr("系统错误", he.Message))
67+
}
6268
} else {
63-
ctx.JSON(utils.ErrSvr(err.Error()))
69+
ctx.JSON(utils.NewErrSvr(err.Error()))
6470
}
6571
}
6672
}

0 commit comments

Comments
 (0)