Exception Handling in Rails 3 -


i handle own exceptions in rails 3. best process handle custom exceptions , routing errors in rails 3?

you can handle custom exceptions follows: in application controller, add follwing:

rescue_from exception, :with => :handle_exception    def not_found     render :template => "shared/not_found", :status => 404   end    private   def handle_exception(exception)     case exception     when cancan::accessdenied       authenticate_user!     when activerecord::recordnotfound       not_found     else       internal_server_error(exception)     end   end    def internal_server_error(exception)     render :template => "shared/internal_server_error", :status => 500   end 

now in method, can raise error or exception as:

def method_name   # raise exception_name, "message on exception"   raise argumenterror, "missing param" end 

you can catch routing errors adding route follows:

match "*any", :to => "application#not_found" 

note: should add above route @ end in route.rb


Comments