Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: map not found error returned by gorm #477

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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