Gin: Custom BindWith error managing

Created on 13 Jul 2016  路  6Comments  路  Source: gin-gonic/gin

Hello @danslimmon, @DennyLoko, @sidbusy, @stxml, @dgsi, @kongfei605, @LeBronoMars and @tsirolnik,

First of all, sorry for delay in answering, I was just a little bit busy with university (final exams).

Regarding all the following issues (#629, #633, #636, https://github.com/danslimmon/gin/pull/1 and #661), here is what I suggest:

  • Because Gin now has the API frozen (doesn't allow breaking changes in functions or behavior), I can't change the behavior of c.BindWith() (#661, @sidbusy), however I like the idea, so keep reading.
  • I'll review and merge the PR #633 with the fix https://github.com/danslimmon/gin/pull/1 (@danslimmon, @DennyLoko).
  • I propose to add two function/aliases called:

    • c.ShouldBindWith() implementing #661, which will allow custom error managment.

    • c.MustBindWith() being an alias of c.BindWith() and soft deprecating the last one (just a message via fmt.Println), for a future change of behavior, by moving c.ShouldBindWith() back to c.BindWith().

Because I'm not a native english speaker, maybe the naming of the two new proposed functions sound strange or completely wrong, so I'm open to suggestion about this, and also about everything else in this message.

Hope I did explained correctly. Tell me your thoughts.

enhancement

Most helpful comment

I'll get back on this tomorrow

All 6 comments

Looks good, before these, I can use binding.FormPost.Bind() instead.

Hello @javierprovecho,

These modifications LGTM. Do you need any help on them ?

I'll get back on this tomorrow

What's the status on this? Really cramping my error style.

@javierprovecho already merged #855 to develop branch so now we can use c.ShouldBindWith() which will allow custom error management.

a little question.

func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
    log.Println("binding called")
    b := make([]byte,req.ContentLength)
    log.Println("data",req.ContentLength)
    log.Println(req.Body)

    n,e := io.Reader(req.Body).Read(b)
    if e != nil{
        log.Println("read err ",e.Error(),n)
        return  e
    }
    log.Println(string(b))

    e = gjson.Unmarshal(b,obj)
    if e != nil{
        log.Println("error unmarlshal json",e.Error())
        return e
    }
    return nil

}

some wrong when invoke n,e := io.Reader(req.Body).Read(b)
the error just return EOF,any idea???

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oryband picture oryband  路  3Comments

iiinsomnia picture iiinsomnia  路  3Comments

xpbliss picture xpbliss  路  3Comments

ghost picture ghost  路  3Comments

ccaza picture ccaza  路  3Comments