how to handle errors like 404 / 500 in rails3 - ruby-on-rails

Hey, I hope you can help me.
I am trying to find a way to direct the user to the default error pages 404.html and 500.html in my public folder.
So when there is a routing or nomethod error it should be directed there to.
I already tried some stuff in my application controller but it didnt work.
Many thanks!!

Rails does this for you automatically when running in production mode. When you upload your application to a live server, Rails takes care of handling those exceptions and rendering the correct error pages with the correct header status. If you're trying to see what those pages look like (for testing or something), just access them directly via http://localhost:3000/404.html
Whenever you set up your Rails application on a live server (let's use Apache as an example), you give the site root as the /public folder in your application. Then, whenever a request is made to that server address, Apache first looks in that public folder and tries to serve a static asset (this is a configurable option in [environment].rb). If it can't find the requested page, then the request is forwarded through the Ruby stack.
When in production mode, if Rails encounters an error that isn't handled (i.e begin, rescue), it throws the error the whole way up to the stack, which then tells Apache (again, in my example) to render an appropriate error.
Here are some common errors that you'll see in development mode and what they render in production mode:
ActiveRecord::RecordNotFound => 404 (page not found)
nil.method => 500 (server error) unless you turn off whiny nils
ActionController::RoutingError => 404 (page not found)

Happens automatically if run in production mode - no need that you do that manually.

Have a look at this post to redirect all requests causing a Routing Error.

Related

How can I display a custom error page with Ruby on Rails in any failure case?

I know that if an exception is raised in my main app, I can use the exceptions_app configuration attribute to serve a dynamic error page.
However, if this error page is dynamic, it might also fail. In my case, I'm sharing some of the display logic with the main app, and it happened that that logic was the source of the exception, and rendering the error page raised it again. My end users then saw the Passenger error page, which is all purple and weird and frightening.
How can I reliably display a custom error page to my users, possibly with a static page fallback?
There is actually a whole series of configuration switches and redirection possibilities depending on your stack.
Here is a decision flow diagram showing all the possibilities. If you have a dynamic error page, you should really also add a static rendering of it in public/500.html, and symlink it to some other file and reference that one in your web server (in the diagram, Apache’s ErrorDocument).
You cannot simply serve public/<HTTP status code>.html directly. Indeed, the web server would then consider /<HTTP status code> requests as requests for the static page, and our errors controller would not trigger Rails routing anymore, preempting dynamic errors display.
You should set config.action_dispatch.show_exceptions to false (more details), as well as disable your app runner (Passenger in the diagram) error page, otherwise you will leak exceptions content.
If you want to go to that level of detail, it is very important that you add integration tests on a user-hidden, voluntarily broken page, as you are disabling some debugging and failsafe features.
References
You could also be interested in the following references:
Base tutorial for dynamic error pages in Rails.
If you want to use your main app to display errors.
CustomErrorsHandler is a gem that automatically tries to use <HTTP status code>.erb templates and falls back to static public/<HTTP status code>.html pages.
How to render a static version automatically by precompiling Rails Static 404 and 500 Pages.

Heroku 500 Internal Server Error on broken pages

My app is being hosted on Heroku and when I go to a broken page on my website (ie. www.example.herokuapp.com/mumbojumbo), i get a 500 Internal Server Error message.
Obviously, when I go to a broken page something will go wrong, I would just prefer it to show the Oops error page that I made that's on an Amazson s3 like Heroku suggests.
How can I fix this?
Right now, I already have a custom Error Page that set up in Heroku.

JRuby/tomcat 404 errors in rseponse to ajax GET

This problem appears specific to routing for JRuby ajax requests. My page sends an ajax GET which works fine in rails development mode. When moved to tomcat via warbler, the request is generated correctly, but tomcat responds with a 404 error.
In other words, the line in routes.rb get '/sector/method' is sufficient that http://localhost:3000/sector/method is processed by Webrick, but if I change the port to 8080 (tomcat) I get a HTTP status 404 The requested resource (/sector/method) is not available.
Apparently tomcat needs to be told it is OK to process the GET even though there is no corresponding file to be found. Right? Fixes?
Thanks.
Are you sure your request point to correct address? if application works, there is no way that GETs does not. Please try to put the request directly into your browser and check...

symfony - 500 error page customisation problem

I am ready to push a site to production (currently on staging), but I'm having a slight problem with the internal server error 500 page.
I have created a file in web/errors/error500.php and I've also got: ErrorDocument 500 errors/error500.php in my .htaccess file
The problem is, if there is a 500 error in the admin, it displays the symfony default 500 page instead of mine in the errors folder.
Does anyone have a rough idea why this is?
I've cleared symfony cache, cleared browser cache and used several browsers.
Thanks
The standard Symfony way of doing this isn't with an Apache ErrorDocument directive, but simply by putting your error page in either <project>/apps/<appname>/config/error/error.html.php or <project>/config/error/error.html.php, for per-app or general error pages respectively. See this checklist item on the very handy Symfony Deployment Cheat Sheet.
Also make sure you are not in debug mode when you test this otherwise it will still show full stack trace.

grails and debugging UrlMappings

In grails, how can I add some code to the UrlMappings.groovy (ex: println) so that I can figure out what the request URI is and which mapping is getting hit (if any)?
Background:
In this situation, there are two servers which serve different things based on file extension. So, the two servers need to always see file extensions for error processing to continue. Otherwise, the servers get confused and 1 serves up a 404 page instead of our 500 page.
The bigger picture involves taking a 500 response due to something like a NullPointerException and tracing it through the code to see what is happening.
I added code like the following which is at http://jetlet.blogspot.com/2010/08/grails-exception-handling-with-response.html :
"500" (controller: "error", action: "internalError")
In testing this, I am throwing a NullPointerException (NPE) on purpose. The 500 handling is called and 500 page is served up when entering in the URL into the browser's address bar. However, when posting a form to the server and having the processing code blow up on purpose with a NPE, the "500" handling code does not get called. The URL in the form's action seems to end with an extension. So, not sure why the difference in behavior between the GET (browser URL) and POST (form submission).
Thanks for insights and thoughts on tracking this down !
In your Config.groovy you can modify your log4j settings for url mapping events by adding
all 'org.codehaus.groovy.grails.web.mapping'
to your existing log4j settings. all is the most verbose setting so it can be tuned back if that starts giving you too much. You can find more information in the official grails documentation here : http://grails.org/doc/latest/guide/3.%20Configuration.html

Resources