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

Use richzw/gin-error for api error handling #24

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions api/err.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package api

import (
"net/http"

"github.com/fabiante/persurl/app"
"github.com/gin-gonic/gin"
err "github.com/richzw/gin-error"
)

// errHandler is a error handling middleware which maps errors onto
// http status codes.
var errHandler gin.HandlerFunc

func init() {
maps := []*err.ErrorMap{
err.NewErrMap(app.ErrBadRequest).StatusCode(http.StatusBadRequest),
err.NewErrMap(app.ErrNotFound).StatusCode(http.StatusNotFound),
}

errHandler = err.Error(maps...)
}
6 changes: 2 additions & 4 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ func (s *Server) SavePURL(ctx *gin.Context) {
case err == nil:
ctx.Status(http.StatusNoContent)
return
case errors.Is(err, app.ErrBadRequest):
ctx.Status(http.StatusBadRequest)
return
default:
_ = ctx.AbortWithError(http.StatusInternalServerError, err)
_ = ctx.Error(err)
ctx.Abort()
}
}

Expand Down
2 changes: 2 additions & 0 deletions api/server_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
)

func SetupRouting(r gin.IRouter, s *Server) {
r.Use(errHandler)

validDomain := validPathVar("domain", regexNamed)
validName := validPathVar("name", regexNamed)

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ go 1.21.0

require (
github.com/gin-gonic/gin v1.9.1
github.com/richzw/gin-error v0.0.0-20230424030910-fbf34ee8b800
github.com/stretchr/testify v1.8.3
)

replace github.com/richzw/gin-error v0.0.0-20230424030910-fbf34ee8b800 => github.com/fabiante/gin-error v0.0.0-20230907150907-3a6797419248

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fabiante/gin-error v0.0.0-20230907150907-3a6797419248 h1:fAdsvytdv/3+vSR5iKcqInsGTUGGkOgxJ0URJ1hWBQc=
github.com/fabiante/gin-error v0.0.0-20230907150907-3a6797419248/go.mod h1:frf9fvAy+kNGKu1y1fFka7fXRc3IJR9OhriBJZDeu7A=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down