Handling 404s like all other errors...

4 views
Skip to first unread message

cult hero

unread,
Nov 5, 2009, 3:04:06 PM11/5/09
to Ramaze
I've been playing with error handling most of the morning and I think
I have a decent idea of how to catch errors via something like this:

Rack::RouteExceptions.route(Exception, MainController.r(:error))

Also, in the event someone finds this on a search, the action can
access the exception that's been raised here by "request.env
['rack.route_exceptions.exception']."

Let me add--something I didn't see in any of the examples--if your
controller DOES NOT have "map" set explicitly, routing the exception
will not work. I don't know if this is a bug or if it's intentional.

The reason I bring this up is because it makes it possible to have a
single page where all my errors go. In general, this is a good thing.
Or, I can route any exception to a particular page, but at the end of
the day it's ONE page.

404s do NOT throw an exceptions (at least, none that I am aware of). I
think I expected this behavior because this is how Merb did it.

On a 404 you're greeted with a very minimalistic: No action found at:
"/<ACTION>"

Obviously, I wanted to override this. After some digging I found
node.rb in Innate which has these comments:

# To use a normal action with template do following:
#
# @example
#
# class Hi
# include Innate::Node
# map '/'
#
# def self.action_missing(path)
# return if path == '/not_found'
# # No normal action, runs on bare metal
# try_resolve('/not_found')
# end
#
# def not_found
# # Normal action
# "Sorry, I do not exist"
# end
# end

And it works... kinda. I either had to do this on a per Class basis or
put it in the main controller and have all my classes inherit the
functionality. The biggest trouble is that it won't get an action from
a particular controller for the action but rather it seems to want to
execute "not_found" on the active controller. That way I can just have
a single "not_found" in my MainController for the entire app.

(I guess in theory I could have self.action_missing raise an exception
but I have some feeling that wouldn't work.)
Reply all
Reply to author
Forward
0 new messages