Rails 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 rails rescue_from all exceptions more about Stack Overflow the company Business Learn more about hiring developers or posting rails render 500 json ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack rails raise 500 Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to render 500 page in rescue_from up vote 2 down rails 500 error page vote favorite 1 I would like to send e-mail when having an exception in my application and render the regular 500 page. 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
Rails Exceptions_app
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 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| Did you find this question interesting? Try our newsletter Sign up for our newsletter and get our top new questions delivered to your inbox (see an example). Subscribed! Success! Please click the link in the confirmation email to activate your subscription. 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
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings
Rails Raise 404
and policies of this site About Us Learn more about Stack Overflow rails rescue_from example the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation activerecord::activerecorderror Tags Users Badges Ask Question 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; http://stackoverflow.com/questions/9638751/how-to-render-500-page-in-rescue-from it only takes a minute: Sign up How to catch 404 and 500 error in Rails? up vote 0 down vote favorite I am using parse-ruby-client in a Rails web app. According to the documentation at http://www.rubydoc.info/github/adelevie/parse-ruby-client/file/README.md#Logging_In, I can do user = Parse::User.authenticate("cooldude6", "p_n7!-e8") to log a user in. This works as long as the credentials are correct. If they're http://stackoverflow.com/questions/33427586/how-to-catch-404-and-500-error-in-rails not correct, I get an error in the Rails app pointing to that line above. In the logs, I see: I, [2015-10-29T18:41:06.636916 #563] INFO -- Status: 404 Completed 500 Internal Server Error in 625ms My question is: how do I catch this 404 status and 500 internal server error so the page doesn't get rendered and throw this error? Ideally I'd want to redirect back to the sign in page if there is an error. ruby-on-rails share|improve this question asked Oct 30 '15 at 1:49 sharataka 1,777113971 add a comment| 1 Answer 1 active oldest votes up vote 1 down vote accepted You need to rescue Parse::ParseProtocolError user = Parse::User.authenticate(params[:username], params[:password]) # continue normally rescue Parse::ParseProtocolError => e status_code = e.code # handle error and try again share|improve this answer edited Oct 30 '15 at 3:44 answered Oct 30 '15 at 3:29 Antarr Byrd 6,177154997 Thanks! This is working. But do you know how to return the error code that I receive from Parse? That way I can provide the right message to the user. parse.com/docs/dotnet/api/html/&hel
so that error pages work for all types of requests, not justGET. Normally, 404 and 500 error pages are static HTML files that live in the public directory https://mattbrictson.com/dynamic-rails-error-pages of a Rails application. These are boring, minimally-styled pages that don’t get the same treatment as the rest of the app. This tutorial shows you how to move error pages into your Rails app as dynamic views that benefit from application styles, layouts, and viewhelpers. tl;dr – jump to the Rails code and the Capistrano bonustip Why are dynamic error 500 error pages paticularly handy in Rails4? Starting with Rails 4, the production asset pipeline no longer generates filenames without cache-busters. This means that referencing /assets/application.css in your static public/404.html page won’t work in a Rails 4 app! The file will not exist in the production environment. The only way to reliably reference your application stylesheet is to use the stylesheet_link_taghelper. But rails catch 500 error pages are static HTML pages; they can’t use helpers, right? If you want nice-looking error pages in Rails 4, here are youroptions: Option 1: No external styles. Don’t reference your application stylesheet at all. Instead, use simple, static error pages with the necessary minimal CSS copied and pasted into each HTML file. This is the solution that ships withRails. Works for simple apps that don’t need custom-branded errorpages. Option 2: Monkey patch. Use static error pages and point to /assets/application.css for styling. Then, monkey-patch Rails to restore the pre-Rails 4 behavior so that the asset pipeline generates non-cache-busted filenames in production. Make sure not to send far-future expires headers for thesefiles! Easiest option for migrating an existing app to Rails4. Option 3: Dynamic. Use dynamic view templates (ERB) for error pages, and take advantage of the stylesheet_link_tag helper to get the right cache-busted filename. Error pages can use your application styles. Be careful, though: if your Rails app is down, your error pages can’t beaccessed. Most flexible option. This is the solution I describebelow. OK, so