Exception Handling in merb.

8 views
Skip to first unread message

dev2

unread,
Sep 7, 2010, 1:39:21 AM9/7/10
to merb
I modified my controllers/exceptions.rb file. I added an action for
my exception, but obviously this isn't enough to make it work. in my
Things controller I "raise FileMissing" and I get the error
"undefined constant Things:FileMissing". What do I need to do to
make this work?
Thanks!

class Exceptions < Merb::Controller

# handle NotFound exceptions (404)
def not_found
render :format => :html
end

# handle NotAcceptable exceptions (406)
def not_acceptable
render :format => :html
end

def file_missing
render :format => :html
end
end

jonah honeyman

unread,
Sep 7, 2010, 2:33:51 AM9/7/10
to me...@googlegroups.com

You need to first create the exception class for merb to route
it properly.

Look at exceptions.rb [1]. Basically, you will want to create
your exception class (in this case FileMissing) and have it
inherit from an existing controller exception.

Starting from line 242 [2] is a list of all the exceptions with
their status codes. You will probably want to raise something in
the 4xx range.

So it would look something like:

class FileMissing < Merb::ControllerExceptions::BadRequest; end

That will send a status code of 400 anytime you raise
FileMissing, along with rendering the action as you have created
it in your exceptions controller.

[1] http://github.com/merb/merb/blob/master/merb-core/lib/merb-core/controller/exceptions.rb#L124
[2] http://github.com/merb/merb/blob/master/merb-core/lib/merb-core/controller/exceptions.rb#L242

> --
> You received this message because you are subscribed to the Google Groups "merb" group.
> To post to this group, send email to me...@googlegroups.com.
> To unsubscribe from this group, send email to merb+uns...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/merb?hl=en.
>

--
-jonah

Reply all
Reply to author
Forward
0 new messages