I'm trying to serve different responses in Grails when an error 500 happens depending on the format of the request.
I've created an ErrorController and I'm using it in the URLMappings but I'm not getting the right request format:
def handle() {
withFormat {
html {
response.status = 500
render(view:'/errors/serverError')
}
json {
response.setContentType "application/json; charset=utf-8"
response.status = 500
ApiResponse apiResponse = new ApiResponse(
meta: new ApiMeta(
code: 500,
errorType: "Whatever",
msgs: ["${request.exception}"]
)
)
render apiResponse as JSON
}
}
}
The response is always in html. Also tried with 'request.withFormat' with the same results.
What I'm missing here?
I do not have enough info to verify if this is really the reason, but it could look like a MIME type issue. Is json configured correctly as MIME type in your Config.groovy? Does your client accept your MIME type. See this link for reference http://grails.org/doc/2.1.0/guide/single.html#contentNegotiation