-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Working with BindJSON and error return #815
Comments
@depado the error returned is actually of type ValidationErrors and you can cast it and then for or range over each FieldError in the map to get separate fields. Also for those that may be using v9 of validator the definition is no longer a map https://github.com/go-playground/validator/blob/v9/errors.go#L37 |
Thanks @joeybloggs I'll give that a try 👍 |
I also experience the error that the 'Content-Type' is text/plain instead of 'application/json' when I have a binding error and return a error struct using |
Yes you are correct @toefel18 not all errors that can be returned are so one would have to ensure the cast succeeds: err:= Bind...
ve, ok := err.(validator.ValidationErrors)
if !ok {
// non validation error... handle appropriately.
} |
@joeybloggs that is indeed true, but the problem I am referring to is different if err := c.Bind(&login); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
} Output is JSON, response Content-Type is "text/plain". I thought that is what @depado meant. Should I file another bug request? |
I see @toefel18 although I am not a part of the gin team, I'd say yes it does seem like a separate issue. P.S. may want to check there isn't one open already, for some reason it sounds familiar :) |
@joeybloggs, okay I will look into it later today 👍 |
The Content-Type thing is a known issue. |
@depado Does |
Looks like it does but the main point of this issue was about the parsing of the returned error of the |
@depado you can use a middleware to check for any binding errors. There was a long thread about it somewhere here a while ago. |
I've encountered similar issue, I have global after middleware that creates json response with errors when errors are present on the context and I also set header to json in that middleware, but because framework writes headers when handling bind errors, its impossible to overwrite them later... My issue #840, I also have a workaround bind function i'm using for now, see my issue for it. |
I have the same issue
then sometimes I'm getting |
@mymtw if you want to get Content-Type ref: #1041 (comment) |
@easonlin404 thx, works |
Everybody can see the PR. #1047 |
Hello,
First of all, thanks for your great project, I'm using it a on daily basis and it's just awesome.
TL;DR
I have no clue on how to properly handle response format when validation fails on
BindJSON
and I want to send back the errors as JSON.Context
I'm currently having trouble working with
context.BindJSON
. I have two fields that are required when receiving JSON, let's call themfirst
andsecond
Now when I receive the request, I handle it like this :
So first of all, I don't understand why
c.JSON()
doesn't return the appropriate content-type, I found that whenBindJSON
fails, it writes the header to be plain text. Anyway my endpoint always returns JSON so I just set the content-type on top of the handler, problem solved (I guess).When one field is missing, this endpoint returns something like that :
That's fine. I mean it's not really what I expected but it works.
Problem
When both fields are missing, this is what I get back :
Question
Is there a simple way to handle those errors ? I'd like to get something more user-friendly, or at least a separate field for my errors. Something like :
I'm aware that checking manually the fields should allow me to do that, but the fact is : I feel like the documentation and examples are pretty vague about error handling.
Could someone help me with that ? How do you handle errors generated by
Bind
orBindJSON
?The text was updated successfully, but these errors were encountered: