feat: 错误处理与 rsp 的返回格式一致

main
熊二 1 year ago
parent 69032be6b2
commit 2aadccd5b8
  1. 7
      internal/init.go
  2. 14
      pkg/rsp/error_handler.go

@ -4,7 +4,6 @@ import (
"errors"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"net/http"
"sorbet/internal/entities"
"sorbet/internal/middleware"
"sorbet/internal/repositories"
@ -57,11 +56,7 @@ func Start() error {
e := echo.New()
e.HideBanner = true
e.HidePort = true
e.HTTPErrorHandler = func(err error, c echo.Context) {
if !c.Response().Committed {
http.Error(c.Response(), err.Error(), 500)
}
}
e.HTTPErrorHandler = rsp.HTTPErrorHandler
e.Logger = util.NewLogger()
e.Use(middleware.Recover())
e.Use(middleware.CORS())

@ -0,0 +1,14 @@
package rsp
import (
"github.com/labstack/echo/v4"
)
// HTTPErrorHandler 实现 echo.HTTPErrorHandler 函数
func HTTPErrorHandler(err error, c echo.Context) {
if c.Response().Committed {
c.Logger().Error(err)
} else if err = Fail(c, err); err != nil {
c.Logger().Error(err)
}
}
Loading…
Cancel
Save