How to globally handle HTTP errors in Grails (status codes 4xx/5xx)?

妖精的绣舞 提交于 2019-12-12 08:47:59

问题


Is there a way in Grails to catch all the possible HTTP errors before they're being sent to the client browser, as to be able to handle the content being sent to the client? I mean all 4xx and 5xx HTTP status codes, not only the 403, 404 and 500 like other have suggested.

What I'd like to do is to catch all HTTP errors in the first place, without having to specify them all one by one, then in a second step I would filter (e.g. in an error controller) specific error codes that I would like to handle (e.g. 400), but at least the ones that I would not specify would render to a generic error template I defined.

If this can't be done (or shouldn't be done), which HTTP errors codes should really be checked for and handled? I can at least see those codes happening at some point: 400, 401, 403, 404, 405, 500, 501, 503. And also, how should they be handled, using HTTP response codes mappings?

Thanks!


回答1:


haven't actually tried it but maybe a number constraint might work?

"$errorCode" {
     controller = "errors"
     action = "displayError"
     constraints {
          errorCode(matches:/\d{3}/)
     }
}


来源:https://stackoverflow.com/questions/4367594/how-to-globally-handle-http-errors-in-grails-status-codes-4xx-5xx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!