Open GoogleCodeExporter opened 8 years ago
It's a good idea to add something like that but what response code to return?
- just 400 ? (bad request)
- or 409 ? (conflict)
- or 412 ? (precondition failed)
- or ...
This is often very application-specific (depending on the validation error) but
one could have generic error handling in the templates for the most obvious
cases.
Do you want to make a proposal how to handle that and create a patch or pull
request?
Thanks for sharing your ideas.
Cheers,
Martin
Original comment by krass...@googlemail.com
on 16 Feb 2012 at 6:54
Hello,
I think minimally 400 Bad Request would be good for validation errors. Googling
around, this seems to be the consensus (the books I've read are a little vague
about this).
I modified my *Resource.groovy classes to do this (see below). It would be nice
if this code were buried in the "Responses" class like "ok" and "created". The
big thing is to get the "errors" from the domain object marshaled back easily.
I am a little new at Grails/Groovy, so I wound up doing the code below. I could
probably put something in "Responses", but the generated resource class I think
needs to make the decision whether its bad or not. I do not think you could
make that decision in "ok"
or "created" - too late.
Thanks,
Kurt
@PUT
@Produces('application/json')
Response updateJson(User dto) {
def errorRender = { updUser -> error updUser.errors as JSON }
saveUser(dto, errorRender)
}
@PUT
@Produces('application/xml')
Response updateXml(User dto) {
def errorRender = { updUser -> error updUser.errors as XML }
saveUser(dto, errorRender)
}
private saveUser(User dto, def errorRender) {
dto.id = id
def updUser = userResourceService.update(dto)
if (updUser.errors.errorCount == 0)
{
ok updUser
}
else
{
errorRender(updUser)
}
}
static Response error(def resource)
{
Response.serverError().entity(resource).build()
}
Original comment by k...@pros.com
on 16 Feb 2012 at 3:32
Original issue reported on code.google.com by
k...@pros.com
on 15 Feb 2012 at 5:33