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?