Skip to content

Commit

Permalink
fix: add translator
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Aug 18, 2023
1 parent fc097fb commit 42d569f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
13 changes: 12 additions & 1 deletion ginp/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"github.com/go-playground/validator/v10"

"github.com/things-go/dyn/genproto/errors"
"github.com/things-go/dyn/transport"
transportHttp "github.com/things-go/dyn/transport/http"
)

var _ transportHttp.Carrier = (*GinCarry)(nil)

type GinCarry struct {
validation *validator.Validate
// translate error
translate transport.ErrorTranslator
}

func NewCarryForGin() *GinCarry {
Expand All @@ -26,6 +29,11 @@ func NewCarryForGin() *GinCarry {
}(),
}
}

func (cy *GinCarry) SetTranslateError(e transport.ErrorTranslator) *GinCarry {
cy.translate = e
return cy
}
func (*GinCarry) WithValueUri(req *http.Request, params gin.Params) *http.Request {
return transportHttp.WithValueUri(req, params)
}
Expand All @@ -41,7 +49,10 @@ func (*GinCarry) BindUri(cg *gin.Context, v any) error {
func (*GinCarry) ErrorBadRequest(cg *gin.Context, err error) {
Abort(cg, errors.ErrBadRequest(err.Error()))
}
func (*GinCarry) Error(cg *gin.Context, err error) {
func (cy *GinCarry) Error(cg *gin.Context, err error) {
if cy.translate != nil {
err = cy.translate.Translate(err)
}
Abort(cg, err)
}
func (*GinCarry) Render(cg *gin.Context, v any) {
Expand Down
15 changes: 13 additions & 2 deletions ginp/ginp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/go-playground/validator/v10"

"github.com/things-go/dyn/genproto/errors"
"github.com/things-go/dyn/transport"
transportHttp "github.com/things-go/dyn/transport/http"
"github.com/things-go/encoding"
)
Expand All @@ -17,6 +18,8 @@ var _ transportHttp.Carrier = (*Carry)(nil)
type Carry struct {
encoding *encoding.Encoding
validation *validator.Validate
// translate error
translate transport.ErrorTranslator
}

type Option func(*Carry)
Expand All @@ -32,7 +35,11 @@ func WithValidation(v *validator.Validate) Option {
cy.validation = v
}
}

func WithTranslateError(t transport.ErrorTranslator) Option {
return func(cy *Carry) {
cy.translate = t
}
}
func NewCarry(opts ...Option) *Carry {
cy := &Carry{
encoding: encoding.New(),
Expand All @@ -47,6 +54,7 @@ func NewCarry(opts ...Option) *Carry {
}
return cy
}

func (cy *Carry) WithValueUri(req *http.Request, params gin.Params) *http.Request {
return transportHttp.WithValueUri(req, params)
}
Expand All @@ -62,7 +70,10 @@ func (cy *Carry) BindUri(c *gin.Context, v any) error {
func (*Carry) ErrorBadRequest(c *gin.Context, err error) {
Abort(c, errors.ErrBadRequest(err.Error()))
}
func (*Carry) Error(c *gin.Context, err error) {
func (cy *Carry) Error(c *gin.Context, err error) {
if cy.translate != nil {
err = cy.translate.Translate(err)
}
Abort(c, err)
}
func (cy *Carry) Render(c *gin.Context, v any) {
Expand Down
5 changes: 5 additions & 0 deletions transport/translator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package transport

type ErrorTranslator interface {
Translate(err error) error
}

0 comments on commit 42d569f

Please sign in to comment.