Permalink
Browse files

Dynamic error pages, closes #26

  • Loading branch information...
sheerun committed Aug 18, 2013
1 parent 2eb1ea0 commit 5c2df5a108ad204bc407183b959bb355ff5ed53d
@@ -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
View
@@ -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

This file was deleted.

Oops, something went wrong.
View

This file was deleted.

Oops, something went wrong.
View

This file was deleted.

Oops, something went wrong.

3 comments on commit 5c2df5a

@richpeck

This comment has been minimized.

Show comment
Hide comment
@richpeck

richpeck Oct 10, 2013

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

richpeck replied Oct 10, 2013

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

This comment has been minimized.

Show comment
Hide comment
@sheerun

sheerun Oct 10, 2013

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 ;)

Owner

sheerun replied Oct 10, 2013

@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

This comment has been minimized.

Show comment
Hide comment
@richpeck

richpeck Oct 10, 2013

@sheerun Thanks Adam 👍

richpeck replied Oct 10, 2013

@sheerun Thanks Adam 👍

Please sign in to comment.