From ac5efee020b040c9f022393a566133bd3885c88c Mon Sep 17 00:00:00 2001 From: fabiante <fabiante@users.noreply.github.com> Date: Thu, 7 Sep 2023 16:28:08 +0200 Subject: [PATCH 1/4] Add richzw/gin-error dep --- go.mod | 1 + go.sum | 2 ++ 2 files changed, 3 insertions(+) diff --git a/go.mod b/go.mod index fe595bc..d6d36d8 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/richzw/gin-error v0.0.0-20230424030910-fbf34ee8b800 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.3.0 // indirect diff --git a/go.sum b/go.sum index 1a77fa1..4577740 100644 --- a/go.sum +++ b/go.sum @@ -45,6 +45,8 @@ github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZ github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/richzw/gin-error v0.0.0-20230424030910-fbf34ee8b800 h1:m1i0G772DCb5UtHOocKpQeeYOzXgZFDa0wVGX9c0Dss= +github.com/richzw/gin-error v0.0.0-20230424030910-fbf34ee8b800/go.mod h1:frf9fvAy+kNGKu1y1fFka7fXRc3IJR9OhriBJZDeu7A= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= From 4de42ba2ba2c574194d00df3c95195d559890022 Mon Sep 17 00:00:00 2001 From: fabiante <fabiante@users.noreply.github.com> Date: Thu, 7 Sep 2023 17:14:20 +0200 Subject: [PATCH 2/4] Add temporary fork of gin-err as dep while it has issue with wrapped errs --- go.mod | 4 +++- go.sum | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d6d36d8..80ecdf9 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -25,7 +28,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/richzw/gin-error v0.0.0-20230424030910-fbf34ee8b800 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.3.0 // indirect diff --git a/go.sum b/go.sum index 4577740..2abe5b5 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -45,8 +47,6 @@ github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZ github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/richzw/gin-error v0.0.0-20230424030910-fbf34ee8b800 h1:m1i0G772DCb5UtHOocKpQeeYOzXgZFDa0wVGX9c0Dss= -github.com/richzw/gin-error v0.0.0-20230424030910-fbf34ee8b800/go.mod h1:frf9fvAy+kNGKu1y1fFka7fXRc3IJR9OhriBJZDeu7A= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= From a021107b9eff6558b5031bb2a1598dbf769ae406 Mon Sep 17 00:00:00 2001 From: fabiante <fabiante@users.noreply.github.com> Date: Thu, 7 Sep 2023 17:17:10 +0200 Subject: [PATCH 3/4] Setup error handlers --- api/err.go | 22 ++++++++++++++++++++++ api/server_routes.go | 2 ++ 2 files changed, 24 insertions(+) create mode 100644 api/err.go diff --git a/api/err.go b/api/err.go new file mode 100644 index 0000000..9007b64 --- /dev/null +++ b/api/err.go @@ -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...) +} diff --git a/api/server_routes.go b/api/server_routes.go index a9f7cef..95a6a49 100644 --- a/api/server_routes.go +++ b/api/server_routes.go @@ -5,6 +5,8 @@ import ( ) func SetupRouting(r gin.IRouter, s *Server) { + r.Use(errHandler) + validDomain := validPathVar("domain", regexNamed) validName := validPathVar("name", regexNamed) From b444631d75fa11094feb434dfd39fa93c20ae9b5 Mon Sep 17 00:00:00 2001 From: fabiante <fabiante@users.noreply.github.com> Date: Thu, 7 Sep 2023 17:17:32 +0200 Subject: [PATCH 4/4] Refactor error handling in SavePURL method --- api/server.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/api/server.go b/api/server.go index 2cf4c52..0c4a06e 100644 --- a/api/server.go +++ b/api/server.go @@ -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() } }