This project is archived and is in readonly mode.
AbstractController::ActionNotFound should be rescued
-
Andrew White
- State changed from new to duplicate
- Importance changed from to Low
This is essentially a duplicate of #4444 Can no longer rescue_from ActionController::RoutingError. You've almost got the workaround from that ticket but your problem is that the first route is partially matching on the controller. If you add an :action regexp constraint listing the valid actions it should fall through to the second route.
-
Kenn Ejima
Thanks Andrew, patching
render_exceptionworked as you said. In my case:require 'action_dispatch/middleware/show_exceptions' module ActionDispatch class ShowExceptions private def render_exception_with_template(env, exception) if exception.kind_of? AbstractController::ActionNotFound request = Request.new(env) if request.env['action_dispatch.request.path_parameters'][:controller] =~ /^api\/.+/ return Api::BaseController.action(:render_404_as_json).call(env) end end render_exception_without_template(env, exception) end alias_method_chain :render_exception, :template end endI have 20 controllers and 100+ actions under the /api context so listing them all in the :action regexp constraint seemed like a bad idea.
-
Kevin Watt
I'm trying your solution, but don't seem to have access to MyApp::HomeController in this context. Any tips? Is it in any strange namespaces besides the one I define in application.rb?
-
Andrew White
Shouldn't it be in the global namespace? i.e. HomeController and not MyApp::HomeController.
-
Kevin Watt
That fixed it, thanks. When rendering your action you can't re-raise and expect that to get caught like normal, since it's not being wrapped in the begin block that does the rescue_from handling in the first place.
I wish there was a way to have my web server (litespeed, but shouldn't matter) to see that it got a status 404 from the backend handler, then render the standard error page.
Though routing errors still cause a 500 rather than 404 which sucks.