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

improving token config and create bindingJSON utils #7

Merged
merged 2 commits into from
Sep 25, 2020
Merged

improving token config and create bindingJSON utils #7

merged 2 commits into from
Sep 25, 2020

Conversation

huf0813
Copy link

@huf0813 huf0813 commented Sep 23, 2020

Hi, i see a lot of insight from @Endrawan and @AFauzulh for better code. Thx guys, i hope there will be more discussions later

Copy link
Contributor

@Endrawan Endrawan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your token improvement @huf0813 . But i still don't understand why you need to create that BindingJSON() method. Maybe you could enlighten me a little?

@@ -31,8 +33,8 @@ type UserTesting struct {

func (cobs *Cobs) Login(c *gin.Context) {
var userCobs UserTesting
if err := c.BindJSON(&userCobs); err != nil {
cobs.Res.CustomResponse(c, "Content-Type", "application/json", "error", "failed when parsing data", http.StatusBadRequest, nil)
if err := cobs.Binding.BindingJSON(c, &userCobs); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use ShouldBindJSON() ?

Comment on lines 15 to 23
func (b *Binding) BindingJSON(c *gin.Context, obj interface{}) error {
if err := c.ShouldBindWith(obj, binding.JSON); err != nil {
if err := c.AbortWithError(http.StatusBadRequest, err).SetType(gin.ErrorTypeBind); err != nil {
return err
}
return err
}
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really necessary? I mean gin already provided ShouldBindJSON() , which exactly works like this

@huf0813
Copy link
Author

huf0813 commented Sep 24, 2020

As you can see on docs. BindJSON() by default will implements two methods, they are ShouldBindWith() and AbortWithError(). If we just implement just ShouldBindJSON() (just implementing ShouldBindWith()) then we will missing something important, that is error handling. That's why i create utils for those.

docs :

  1. Binding Model
  2. BindJSON()
  3. ShouldBindJSON()
  4. ShoulBindWIth()

i'll wrap it out, simply like these :

  1. ShouldBindWith() + AbortWithError() = BindJSON()
  2. ShouldBindWith() + missing error handling = ShouldBindJSON()

@Endrawan
Copy link
Contributor

Endrawan commented Sep 24, 2020

Okay, but the problem is lies in c.AbortWithError() you called in BindingJSON(). When you called c.AbortWithError(), it will force our server to return a header (in your code its StatusBadRequest). So when we want to change the header to the correct one (for example StatusNotFound), it'll return a warning Headers were already written. Wanted to override status code 400 with 404. Not only that, the c.AbortWithError() will force the response to text/plain as you can see in this issue.

So the point of using ShouldBindJSON() is to handle the error by ourselves not by the gin itself. Just try to change your code

if err := cobs.Binding.BindingJSON(c, &userCobs); err != nil {
	cobs.Res.CustomResponse(c, "Content-Type", "application/json", "error", "failed when binding data", http.StatusNotFound, nil)
	return
}

to this

if err := c.ShouldBindJSON(&userCobs); err != nil {
	cobs.Res.CustomResponse(c, "Content-Type", "application/json", "error", err.Error(), http.StatusNotFound, nil)
	return
}

Then you start your bad request (no body, username with int datatype, etc.) with your postman and see the differences.

@huf0813
Copy link
Author

huf0813 commented Sep 24, 2020

Thx, i need to learn more and will delete that later. Don't feel like bothered by me, ok?

@Endrawan
Copy link
Contributor

Yeah, chill man. It's just a discussion nothing wrong with it

Copy link
Contributor

@Endrawan Endrawan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's all good now. Thank you! @huf0813

@Endrawan Endrawan merged commit 426e343 into porosub:master Sep 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants