Skip to content

Commit

Permalink
fix: drop ErrorBadRequest, use Error
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Feb 1, 2024
1 parent bdcbbd9 commit 0456eca
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/protoc-gen-dyn-gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"google.golang.org/protobuf/compiler/protogen"
)

const version = "v0.0.2"
const version = "v0.1.0"

var args = struct {
ShowVersion bool
Expand Down
2 changes: 1 addition & 1 deletion cmd/protoc-gen-dyn-gin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func executeServiceDesc(g *protogen.GeneratedFile, s *serviceDesc) error {
g.P("var reply *", m.Reply)
g.P()
g.P("if err = shouldBind(&req); err != nil {")
g.P("carrier.ErrorBadRequest(c, err)")
g.P("carrier.Error(c, err)")
g.P("return")
g.P("}")
g.P("reply, err = srv.", m.Name, "(c.Request.Context(), &req)")
Expand Down
4 changes: 2 additions & 2 deletions example/hello/hello.gin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions ginp/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ginp

import (
"context"
stdErrors "errors"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -46,13 +47,13 @@ func (*GinCarry) BindQuery(cg *gin.Context, v any) error {
func (*GinCarry) BindUri(cg *gin.Context, v any) error {
return cg.ShouldBindUri(v)
}
func (*GinCarry) ErrorBadRequest(cg *gin.Context, err error) {
Abort(cg, errors.ErrBadRequest(err.Error()))
}
func (cy *GinCarry) Error(cg *gin.Context, err error) {
if cy.translate != nil {
err = cy.translate.Translate(err)
}
if e := new(validator.ValidationErrors); stdErrors.As(err, e) {
err = errors.ErrBadRequest(err.Error())
}
Abort(cg, err)
}
func (*GinCarry) Render(cg *gin.Context, v any) {
Expand Down
8 changes: 5 additions & 3 deletions ginp/ginp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ginp

import (
"context"
stdErrors "errors"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -67,13 +68,14 @@ func (cy *Carry) BindQuery(c *gin.Context, v any) error {
func (cy *Carry) BindUri(c *gin.Context, v any) error {
return cy.encoding.BindUri(c.Request, v)
}
func (*Carry) ErrorBadRequest(c *gin.Context, err error) {
Abort(c, errors.ErrBadRequest(err.Error()))
}

func (cy *Carry) Error(c *gin.Context, err error) {
if cy.translate != nil {
err = cy.translate.Translate(err)
}
if e := new(validator.ValidationErrors); stdErrors.As(err, e) {
err = errors.ErrBadRequest(err.Error())
}
Abort(c, err)
}
func (cy *Carry) Render(c *gin.Context, v any) {
Expand Down
2 changes: 0 additions & 2 deletions transport/http/carrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ type Carrier interface {
// BindUri binds the passed struct pointer using the uri codec.Marshaler.
// NOTE: before use this, you should set uri params in the request context with RequestWithUri.
BindUri(*gin.Context, any) error
// ErrorBadRequest encode error response.
ErrorBadRequest(*gin.Context, error)
// Error encode error response.
Error(c *gin.Context, err error)
// Render encode response.
Expand Down

0 comments on commit 0456eca

Please sign in to comment.