Question

I have been searching the web for the issue I'm facing for quite some time now. I found people facing it, but couldn't get an appropriate solution to the problem. The issue is what would be the best way to handle any exception occurred in an template.gsp ? I know I can use a try-catch there so that my controller don't get the exception, but I did find people saying its not a good practice, but failed to answer why. So is it a wrong way and if it is then is there any better solution to deal with this problem ?

Was it helpful?

Solution

The correct way is with a 500 error handler: http://grails.org/doc/latest/guide/theWebLayer.html#mappingToResponseCodes

If you have a lot of logic in your views that could produce a error consider refactoring the code into a tag library which can incorporate better error handling

OTHER TIPS

It depends on what behavior you want for your application with the error.

If a generic error page works for the application - then setup an error page in URL mappings and ignore the error in the controller.

Do you need a nice custom error that is specific for that case - (or possibly display an alternate page? ), if so then you'd need to a try catch (or do some fun with URL mapping and creating an error controller). The objections that come up in Exception handling in Grails controllers arise around the amount of code required for the error handling in the controller vs the rest of the application - with the error handling code being ~40% of each controller method. This causes the code to seem bad/bloated (and apparently violate the CodeNarc "CatchException" rule).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top