Permalink
Browse files

Dynamic error pages, closes #26

  • Loading branch information...
1 parent 2eb1ea0 commit 5c2df5a108ad204bc407183b959bb355ff5ed53d @sheerun committed Aug 18, 2013
@@ -0,0 +1,40 @@
+class ErrorsController < ActionController::Base
+
+ # Because we don't inherit from ApplicationController
+ layout 'error'
+
+ # You can use these method in views
+ helper_method :status_code, :status_name, :error_message
+
+ # You can edit format.json for proper API response format.
+ def show
+ custom_template = template_exists?(status_code, 'errors')
+
+ respond_to do |format|
+ format.html { render action: custom_template ? status_code.to_s : 'show' }
+ format.json { render json: { status: status_code, message: error_message } }
+ end
+ end
+
+ protected
+
+ def status_code
+ (request.path.match(/\d{3}/) || ['500'])[0].to_i
+ end
+
+ def status_name
+ Rack::Utils::HTTP_STATUS_CODES.fetch(status_code, "Internal Server Error")
+ end
+
+ def error_message
+ if status_code != 500 && exception
+ exception.message
+ else
+ "Our administrator has been notified about this event."
+ end
+ end
+
+ def exception
+ env['action_dispatch.exception']
+ end
+end
@@ -0,0 +1,4 @@
+/ An example of custom error page.
+.dialog
+ h1 We're sorry, but something went wrong.
+p If you are the application owner check the logs for more information.
@@ -0,0 +1,6 @@
+/ Generic template for errors
+.dialog
+ h1
+ = Rack::Utils::HTTP_STATUS_CODES[status_code] || "Internal Server Error"
+ = " (#{status_code})"
+p= error_message
@@ -0,0 +1,48 @@
+doctype
+html
+ head
+ title We're sorry, but something went wrong (500)
+ css:
+ body {
+ background-color: #EFEFEF;
+ color: #2E2F30;
+ text-align: center;
+ font-family: arial, sans-serif;
+ }
+
+ div.dialog {
+ width: 25em;
+ margin: 4em auto 0 auto;
+ border: 1px solid #CCC;
+ border-right-color: #999;
+ border-left-color: #999;
+ border-bottom-color: #BBB;
+ border-top: #B00100 solid 4px;
+ border-top-left-radius: 9px;
+ border-top-right-radius: 9px;
+ background-color: white;
+ padding: 7px 4em 0 4em;
+ }
+
+ h1 {
+ font-size: 100%;
+ color: #730E15;
+ line-height: 1.5em;
+ }
+
+ body > p {
+ width: 31em;
+ margin: 0 auto 1em;
+ padding: 1em;
+ background-color: #F7F7F7;
+ border: 1px solid #CCC;
+ border-right-color: #999;
+ border-bottom-color: #999;
+ border-bottom-left-radius: 4px;
+ border-bottom-right-radius: 4px;
+ border-top-color: #DADADA;
+ color: #666;
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
+ }
+ body
+ = yield
@@ -13,4 +13,6 @@ class Application < Rails::Application
g.javascripts = false
g.helper = false
end
+
+ config.exceptions_app = self.routes
end
View
@@ -53,4 +53,9 @@
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
+
+ match '(errors)/:status', to: 'errors#show',
+ constraints: { status: /\d{3}/ },
+ defaults: { status: '500' },
+ via: :all
end
View
@@ -1,58 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
- <title>The page you were looking for doesn't exist (404)</title>
- <style>
- body {
- background-color: #EFEFEF;
- color: #2E2F30;
- text-align: center;
- font-family: arial, sans-serif;
- }
-
- div.dialog {
- width: 25em;
- margin: 4em auto 0 auto;
- border: 1px solid #CCC;
- border-right-color: #999;
- border-left-color: #999;
- border-bottom-color: #BBB;
- border-top: #B00100 solid 4px;
- border-top-left-radius: 9px;
- border-top-right-radius: 9px;
- background-color: white;
- padding: 7px 4em 0 4em;
- }
-
- h1 {
- font-size: 100%;
- color: #730E15;
- line-height: 1.5em;
- }
-
- body > p {
- width: 33em;
- margin: 0 auto 1em;
- padding: 1em 0;
- background-color: #F7F7F7;
- border: 1px solid #CCC;
- border-right-color: #999;
- border-bottom-color: #999;
- border-bottom-left-radius: 4px;
- border-bottom-right-radius: 4px;
- border-top-color: #DADADA;
- color: #666;
- box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
- }
- </style>
-</head>
-
-<body>
- <!-- This file lives in public/404.html -->
- <div class="dialog">
- <h1>The page you were looking for doesn't exist.</h1>
- <p>You may have mistyped the address or the page may have moved.</p>
- </div>
- <p>If you are the application owner check the logs for more information.</p>
-</body>
-</html>
View
@@ -1,58 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
- <title>The change you wanted was rejected (422)</title>
- <style>
- body {
- background-color: #EFEFEF;
- color: #2E2F30;
- text-align: center;
- font-family: arial, sans-serif;
- }
-
- div.dialog {
- width: 25em;
- margin: 4em auto 0 auto;
- border: 1px solid #CCC;
- border-right-color: #999;
- border-left-color: #999;
- border-bottom-color: #BBB;
- border-top: #B00100 solid 4px;
- border-top-left-radius: 9px;
- border-top-right-radius: 9px;
- background-color: white;
- padding: 7px 4em 0 4em;
- }
-
- h1 {
- font-size: 100%;
- color: #730E15;
- line-height: 1.5em;
- }
-
- body > p {
- width: 33em;
- margin: 0 auto 1em;
- padding: 1em 0;
- background-color: #F7F7F7;
- border: 1px solid #CCC;
- border-right-color: #999;
- border-bottom-color: #999;
- border-bottom-left-radius: 4px;
- border-bottom-right-radius: 4px;
- border-top-color: #DADADA;
- color: #666;
- box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
- }
- </style>
-</head>
-
-<body>
- <!-- This file lives in public/422.html -->
- <div class="dialog">
- <h1>The change you wanted was rejected.</h1>
- <p>Maybe you tried to change something you didn't have access to.</p>
- </div>
- <p>If you are the application owner check the logs for more information.</p>
-</body>
-</html>
View
@@ -1,57 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
- <title>We're sorry, but something went wrong (500)</title>
- <style>
- body {
- background-color: #EFEFEF;
- color: #2E2F30;
- text-align: center;
- font-family: arial, sans-serif;
- }
-
- div.dialog {
- width: 25em;
- margin: 4em auto 0 auto;
- border: 1px solid #CCC;
- border-right-color: #999;
- border-left-color: #999;
- border-bottom-color: #BBB;
- border-top: #B00100 solid 4px;
- border-top-left-radius: 9px;
- border-top-right-radius: 9px;
- background-color: white;
- padding: 7px 4em 0 4em;
- }
-
- h1 {
- font-size: 100%;
- color: #730E15;
- line-height: 1.5em;
- }
-
- body > p {
- width: 33em;
- margin: 0 auto 1em;
- padding: 1em 0;
- background-color: #F7F7F7;
- border: 1px solid #CCC;
- border-right-color: #999;
- border-bottom-color: #999;
- border-bottom-left-radius: 4px;
- border-bottom-right-radius: 4px;
- border-top-color: #DADADA;
- color: #666;
- box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
- }
- </style>
-</head>
-
-<body>
- <!-- This file lives in public/500.html -->
- <div class="dialog">
- <h1>We're sorry, but something went wrong.</h1>
- </div>
- <p>If you are the application owner check the logs for more information.</p>
-</body>
-</html>

3 comments on commit 5c2df5a

@richpeck

This is great! But does it ensure stable error reporting? I've found if I got to http://mysite.com/404, it uses the old error pages still

@sheerun
Owner

@richpeck Maybe you didn't remove html files. It works for me.

I've tested it with getsentry and it works. It also logs exceptions. I guess I can call it stable error reporting ;)

@richpeck

@sheerun Thanks Adam 👍

Please sign in to comment.