Rails 3 Catch 500 Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring rails exceptions_app developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question rails render 500 json x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; rescue_from rails 4 it only takes a minute: Sign up How to render 500 page in rescue_from up vote 2 down vote favorite 1 I would like to send e-mail when having an exception in my application and render the regular 500 page. rails 4 exceptions_app I could not find how to perform the 500 page render: class ApplicationController < ActionController::Base rescue_from StandardError do send_email_of_error # what goes here? end ... end ruby-on-rails share|improve this question asked Mar 9 '12 at 17:58 mbdev 1,97153148 add a comment| 2 Answers 2 active oldest votes up vote 6 down vote accepted Raising the exception again will likely to what you want: rescue_from StandardError do |exception| send_email_of_error raise exception end You could also call render to render your
Rails 500 Error Page
own page, the docs have an example doing this. But why reinvent the wheel? The exception notifier gem already does this and is customizable and tested. share|improve this answer answered Mar 9 '12 at 18:04 Andrew Marshall 63.9k12134153 that's great. I found the original was not updated for two years. i'll try this version. –mbdev Mar 9 '12 at 18:29 Unfortunately, that turns out to render the error page, but with a status code of 200. –Steve Jorgensen Oct 30 '14 at 22:01 add a comment| up vote 3 down vote This is an approach that maybe fits your needs: class ApplicationController < ActionController::Base rescue_from Exception, :with => :render_500 def render_500(exception) @exception = exception render :template => "shared/500.html", :status => 500 end end share|improve this answer answered Mar 9 '12 at 18:14 awenkhh 2,0061020 2 One should never rescue from Exception. Use StandardError instead as in Andrew Marshall's answer. –dmur Nov 21 '14 at 19:53 @dmur it would be helpful for others if you explain your comment. And the question was how to render a 500 page. That is not covered in Andrew Marshalls answer at all. Wether you rescue from Exception or from StandardError - what I never would do because you don't know anything about the error so let it crash and fix it - is not important here. In my answer I should have written "AnyException"
here for a quick overview of the site Help Center
Rails Raise 500
Detailed answers to any questions you might have Meta rescue_from actioncontroller::routingerror Discuss the workings and policies of this site About Us Learn more about Stack rails catch all exceptions Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question http://stackoverflow.com/questions/9638751/how-to-render-500-page-in-rescue-from x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Dynamic error pages in Rails 3 up vote 37 down vote favorite 21 In Rails 2.3.x, you http://stackoverflow.com/questions/5331008/dynamic-error-pages-in-rails-3 can override render_optional_error_file like so: # ApplicationController.rb protected def render_optional_error_file(status_code) render :template => "errors/500", :status => 500, :layout => 'application' end However, Rails 3 no longer has render_optional_error_file. Instead, you need to override rescue_action_in_public, which you can do like so: # config/initializers/error_page.rb module ActionDispatch class ShowExceptions protected def rescue_action_in_public(exception) status = status_code(exception).to_s template = ActionView::Base.new(["#{Rails.root}/app/views"]) if ["404"].include?(status) file = "/errors/404.html.erb" else file = "/errors/500.html.erb" end body = template.render(:file => file) render(status, body) end end end This works, but does not use the application's layout. However, if you specify the layout path like so: body = template.render(:file => file, :layout => "layouts/application") # line 15 You get an Error during failsafe response: ActionView::Template::Error. Line 4 of application.html.erb:4 is: <%= stylesheet_link_tag "app", "jquery-ui", "jquery.fancybox", :cache => "all" %> Whatever ActionView normally uses to render templates isn't getting loaded. The stack trace is: /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view
wanted to share this particular way http://blog.yangtheman.com/2012/10/11/user-friendly-500-and-404-pages-on-rails-3/ of handling 404 and 500 errors. First, rescue errors and tell what to do in application_controller.rb. if Rails.env.production? unless Rails.application.config.consider_all_requests_local rescue_from Exception, with: http://dev.mensfeld.pl/2010/01/handling-custom-404-and-500-errors-in-rails-2/ :render_500 rescue_from ActionController::RoutingError, with: :render_404 rescue_from ActionController::UnknownController, with: :render_404 rescue_from ActionController::UnknownAction, with: :render_404 rescue_from ActiveRecord::RecordNotFound, with: :render_404 end end In the same application_controller.rb, 500 error then you create the methods specified above. def render_404(exception) @not_found_path = exception.message respond_to do |format| format.html { render template: 'errors/not_found', layout: 'layouts/application', status: 404 } format.all { render nothing: true, status: 404 } end end def render_500(exception) logger.info exception.backtrace.join("\n") respond_to do |format| format.html { render template: rails 3 catch 'errors/internal_server_error', layout: 'layouts/application', status: 500 } format.all { render nothing: true, status: 500} end end Since it's using the application layout, you will see the error message you specify in the layout. The following is my 500 error page, /views/errors/internal_server_error.html.haml. For 404 page, you can also use @not_found_path instance variable in the view as well. #errors-page .thumbnail.errors .errors-500 .caption %h5 Sorry %h1 500 Internal Server Error - # Do not remove this line. It is used for development - # purposes. When an error is found. In development, it - # shows the logs, in production it is nil. .error-msg = @log Cheers, This entry was posted in Ruby on Rails and tagged 404, 500, error handling, Ruby on Rails by Yang Chung. Bookmark the permalink. 2 thoughts on “User-friendly 500 and 404 pages on Ra
we request a non existing resource, we will see: Routing Error No route matches “/dummy_controller/non_existing_action” with {:method=>get} Similar situation occurs, when requesting non existing action but in an existing controller: Unknown action No action responded to smthn_weird. Actions: index, load_adverts, login and logout Even in production when we make a request from 127.0.0.1 (aka localhost), the request is handled as local. When we develope our software, informations like those mentioned above are useful, however sometimes we would like to do something else. There might be a situation when you would like to: redirect all ‘missed' requests into a specified controller (withouth showing 404) test 404 and 500 behavior show extended info even in production mode render different error templates, depending on a module (admin, shopping, etc) do any other weird kind of stuff So, let's overwrite Rails default behavior. Put into config/environment.rb: # Show 404 errors SHOW_404 = true # Show error template (or render extended Rails info) SHOW_EXTENDED_404 = false # Don't show 500 - instead render 404 SHOW_ONLY_404 = false We can use those constants to manipulate Rails "error flow": SHOW_404 = false - don't show 404 - use default controller and action to handle response SHOW_404 = true – render 404 error SHOW_EXTENDED_404 = true – show extended 404 info SHOW_EXTENDED_404 = false – render default 404 template (in any of environments) SHOW_ONLY_404 – don't show 500 errors - handle them like 404 Next, add into config/routes.rb (just before final "end"): # 404 route map.connect "*anything", :controller => "default_controller", :action => "index" if !SHOW_404 This will redirect into default controller any requests which do not fit anywhere else. It is worth mentioning here, that it is often not the desired behavior and the user can feel lost when suddenly he will see main page instead of 404 error. When we use namespaces(modules) and we would like to handle errors differently, depending on a module, we should add: admin.connect "*anything", :controller => "default_controller", :action => "index" if !SHOW_404 admin.connect ':action' , :controller => "module", :action => "index" if SHOW_404 and for each module also: admin.controller_name 'controller_name', :controller => 'controller_name' Now overwrite default error handling methods in controllers/application_controller.rb : def rescue_action_locally(exception) if SHOW_EXTENDED_404 super exception else rescue_action_in_public(exception) end end Second me