Skip to content

Commit

Permalink
Restored dynamic rendering of error pages and fixed 502 error. Need t…
Browse files Browse the repository at this point in the history
…o check that hoptoad notifications are still triggered once this goes to test!
  • Loading branch information
Lim Lim committed Oct 10, 2011
1 parent baa3f2e commit e856645
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 465 deletions.
23 changes: 0 additions & 23 deletions app/controllers/application_controller.rb
Expand Up @@ -318,29 +318,6 @@ def convert_date(hash, date_symbol_or_string)

public

# No longer works in rails 3 as routing got moved to middleware
# https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4444
# TODO find an alternative, see
# http://accuser.cc/posts/1-rails-3-0-exception-handling
# and
# http://github.com/vidibus/vidibus-routing_error
# # with thanks to http://henrik.nyh.se/2008/07/rails-404
# def render_optional_error_file(status_code)
# case(status_code)
# when :not_found then
# render :template => "errors/404", :layout => 'application', :status => 404
# when :forbidden then
# render :template => "errors/403", :layout => 'application', :status => 403
# when :unprocessable_entity then
# render :template => "errors/422", :layout => 'application', :status => 422
# when :internal_server_error then
# render :template => "errors/500", :layout => 'application', :status => 500
# notify_about_exception(error)
# else
# super
# end
# end

def valid_sort_column(param, model='work')
allowed = []
if model.to_s.downcase == 'work'
Expand Down
40 changes: 40 additions & 0 deletions app/controllers/errors_controller.rb
@@ -0,0 +1,40 @@
class ErrorsController < ApplicationController

def method_not_allowed
render :action => "403"
end

def forbidden
render :action => "403"
end

def not_found
render :action => "404"
end

def unprocessable_entity
render :action => "422"
end

def internal_server_error
render :action => "500"
end

def conflict
render :action => "500"
end

def not_implemented
render :action => "500"
end

%w(403 404 422 500 502).each do |error_code|
define_method error_code.to_sym do
respond_to do |format|
format.html { render error_code, :status => error_code }
format.any(:js) { head error_code }
end
end
end

end
11 changes: 5 additions & 6 deletions app/views/errors/403.html.erb
@@ -1,6 +1,5 @@
<!-- BEGIN main -->
<div id="main" class="error">
<h2 class="heading">Error 403</h2>
<h3 class="heading">Forbidden</h3>
<p>You do not have permission to access the requested file on this server.</p>
</div>
<div id="main" class="error">
<h2 class="heading">Error 403</h2>
<h3 class="heading">Forbidden</h3>
<p>You do not have permission to access the requested file on this server.</p>
</div>
11 changes: 5 additions & 6 deletions app/views/errors/404.html.erb
@@ -1,6 +1,5 @@
<!-- BEGIN main -->
<div id="main" class="error">
<h2 class="heading">Error 404</h2>
<h3 class="heading">The page you were looking for doesn't exist.</h3>
<p>You may have mistyped the address or the page may have moved.</p>
</div>
<div id="main" class="error">
<h2 class="heading">Error 404</h2>
<h3 class="heading">The page you were looking for doesn't exist.</h3>
<p>You may have mistyped the address or the page may have moved.</p>
</div>
11 changes: 5 additions & 6 deletions app/views/errors/422.html.erb
@@ -1,6 +1,5 @@
<!-- BEGIN main -->
<div id="main" class="error">
<h2 class="heading">Error 422</h2>
<h3 class="heading">The change you wanted was rejected.</h3>
<p>Maybe you tried to change something you didn't have access to.</p>
</div>
<div id="main" class="error">
<h2 class="heading">Error 422</h2>
<h3 class="heading">The change you wanted was rejected.</h3>
<p>Maybe you tried to change something you didn't have access to.</p>
</div>
11 changes: 5 additions & 6 deletions app/views/errors/500.html.erb
@@ -1,6 +1,5 @@
<!-- BEGIN main -->
<div id="main" class="error">
<h2 class="heading">Error 500</h2>
<h3 class="heading">We're sorry, but something went wrong.</h3>
<p>We've been notified about this issue and we'll take a look at it shortly</p>
</div>
<div id="main" class="error">
<h2 class="heading">Error 500</h2>
<h3 class="heading">We're sorry, but something went wrong.</h3>
<p>We've been notified about this issue and we'll take a look at it shortly</p>
</div>
20 changes: 20 additions & 0 deletions config/initializers/monkeypatches/show_exceptions.rb
@@ -0,0 +1,20 @@
# Enable handling exceptions with dynamic error pages in production
# See http://accuser.cc/posts/1-rails-3-0-exception-handling
require 'action_dispatch/middleware/show_exceptions'

module ActionDispatch
class ShowExceptions
private
def render_exception_with_template(env, exception)
body = ErrorsController.action(rescue_responses[exception.class.name]).call(env)
log_error(exception)
notify_hoptoad(exception)
body
rescue
render_exception_without_template(env, exception)
end

alias_method_chain :render_exception, :template if Rails.env == "production"
end
end

99 changes: 0 additions & 99 deletions public/403.html

This file was deleted.

100 changes: 0 additions & 100 deletions public/404.html

This file was deleted.

0 comments on commit e856645

Please sign in to comment.