1
votes

I have a custom URLMapping that catch error codes and calls some code (used for monitoring of 5xx/4xx responses):

// Explicit error-handling for monitoring
"401"(controller:"error", action:"fail4x")
..
"501"(controller:"error", action:"fail5x")
...

I also have a filter that does some authentication stuff in the before closure, sets response status to 401 if it fails, and also returns false from the filter.

The problem is that the 401 isn't caught by URLMappings. According to the docs, returning false from a filter "[indicates] that the response has been handled that that all future filters and the action should not execute" - so I'm guessing that Grails just pretty much drops everything, including processing of the URLMapping that would normally catch other 4xx errors?

Is there a way around this so that my 401 from a failed filter is caught by URLMappings (assuming I've interpreted what is happening correctly, and explained it clearly!)

[Using Grails 2.1.1]

Cheers.

2

2 Answers

0
votes

Found a sort-of solution - if failing the filter, do a redirect through the required action:

redirect(controller:"error", action:"fail4x") 
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "blah")

Which forces the trigger of the error controller, bypassing the need for URLMappings to catch it. It seems a bit ugly though, so please feel free to still comment!

0
votes

Had a similar issue with a similar solution. We were catching concurrency issues in our controllers and throwing out an exception that was caught by an after filter. This was working in Grails 1.3.7

After upgrading to Grails 2.1.0, the thrown exceptions instead just generate a 500 error, which gets caught by URLMapping. We ended up adding an action in the error controller that did the same work as the filter, and just redirecting to it when URLMappings sees a 500 with that exception.

Wish there was a cleaner way to handle it, but a dirty working solution beats a clean broken one