Skip to content

Commit

Permalink
feat: map not found error returned by gorm (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Jun 12, 2023
1 parent 29e2d70 commit c76b64b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions api-server/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/pkg/errors"
"github.com/wI2L/fizz"
"github.com/wI2L/fizz/openapi"
"gorm.io/gorm"

"github.com/bentoml/yatai-common/consts"
"github.com/bentoml/yatai-schemas/schemasv1"
Expand Down Expand Up @@ -65,6 +66,19 @@ func injectCurrentOrganization(c *gin.Context) {
c.Next()
}

func errorHook(c *gin.Context, e error) (status int, resp interface{}) {
resp = gin.H{"error": e.Error()}
cause := errors.Cause(e)

if errors.Is(cause, gorm.ErrRecordNotFound) {
status = http.StatusNotFound
} else {
status = http.StatusBadRequest
}

return
}

func NewRouter() (*fizz.Fizz, error) {
tonic.SetRenderHook(func(c *gin.Context, statusCode int, payload interface{}) {
if _, exists := c.Get(WebsocketConnectContextKey); exists {
Expand All @@ -87,6 +101,8 @@ func NewRouter() (*fizz.Fizz, error) {
}
}, "")

tonic.SetErrorHook(errorHook)

engine := gin.New()

store := cookie.NewStore([]byte(config.YataiConfig.Server.SessionSecretKey))
Expand Down

0 comments on commit c76b64b

Please sign in to comment.