3

I've installed Rails exception_handler and tried following the instructions to setup custom error handling, however I'm still getting the standard 500 error message that the gem creates:

500 Internal Server Error

If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.

Here's what I added to config/application.rb:

class Application < Rails::Application
  config.exception_handler = {
      dev: true,
      layouts: {
        '500' => 'exception'
      }
  }
end

I created an exception layout at layouts/exception.html.erb:

<!DOCTYPE html>
<html>
  <head>
    <title><%= "Error - #{@exception.status} Error" %></title>
  </head>

  <body>

    <div class="container">
      <%= yield %>
    </div>

  </body>
</html>

And generated the default exception views with: rails generate exception_handler:views

<div class="error">
    <% if /^(5[0-9]{2})$/ =~ @exception.status.to_s %>

        <!--Message -->
        <%= content_tag :div, class: "message" do %>
            <%= content_tag :div, class: "title" do %>
                <span><%= "#{@exception.status} Error - #{details[:name]}" %></span>
                <%= link_to image_tag("exception_handler/close.png"), main_app.root_url, title: "Close (Go back home)", class: "close" %>
            <% end %>

            <%= content_tag :div, class: "details" do %>
                <%= image_tag "exception_handler/alert.png", title: "#{@exception.status} Error" %>
                <div class="status"><%= @exception.status %> Error</div>
            <% end %>

            <%= content_tag :div, class: "info" do %>
                <span><%= details[:description] %></span>
                <div class="notification">
                    <%= link_to image_tag("exception_handler/home.png", title: "Go Back Home"), main_app.root_url, class: "home" %>
                    <div class="version">v<%= Rails.version %></div>
                    <strong>Our developers have been notified - we're working on it!</strong>
                </div>
            <% end %>
            
        <% end %>

    <% else %>

        <%= content_tag :div, details[:description], class: "message" %>
            
    <% end %>
</div>

I've tried restarting my rails server just to make sure the changes are taking effect, but it still doesn't work. What have I missed?

Community
  • 1
  • 1
Godzilla74
  • 2,358
  • 1
  • 31
  • 67
  • Did you have a look into your log file what the error is? Maybe it's an error this gem can't handle. – slowjack2k Oct 05 '16 at 13:55
  • you do have `config.consider_all_requests_local = true` in your `config/development.rb`, right? PS: I'm assuming you're testing this on dev env. – Alexandre Angelim Oct 05 '16 at 13:56
  • @AlexandreAngelim yes. – Godzilla74 Oct 05 '16 at 13:58
  • @slowjack2k looks like it's attempting to use my default layout `application.html.erb`, instead of `exception.html.erb`: `Completed 500 Internal Server Error in 299ms Error during failsafe response: undefined method `each' for nil:NilClass /Users/godzilla74/Coding/neo-api/app/views/layouts/_navigation.html.erb:38:in `_app_views_layouts__navigation_html_erb__2393319544277720129_70250216188520'` – Godzilla74 Oct 05 '16 at 13:59
  • So is the orignal error maybe not 500 . You did define the layout only for 500. – slowjack2k Oct 05 '16 at 14:04
  • 2
    you should change `'500' => 'exception'` to `500 => 'exception'` (the key 500 - is Integer not String) – okliv Mar 13 '17 at 16:09
  • That's the error Rails shows when you have an [error in an error](https://stackoverflow.com/a/69043842/886745). – rebagliatte Sep 03 '21 at 11:23

1 Answers1

0

In version exception_handler 0.8.0.0 the config/application.rb

config.exception_handler = {
dev: nil, # allows you to turn ExceptionHandler "on" in development
db:nil, # allocates a "table name" into which exceptions are saved (defaults to nil)
email: "", # sends exception emails to a listed email (string // "you@email.com")

  # Custom Exceptions
  custom_exceptions: {
    #'ActionController::RoutingError' => :not_found # => example
  },

  # On default 5xx error page, social media links included
  social: {        
    facebook: nil, # Facebook page name   
    twitter:  nil, # Twitter handle  
    youtube:  nil, # Youtube channel name / ID
    linkedin: nil, # LinkedIn name
    fusion:   nil  # FL Fusion handle
  },  

  # This is an entirely NEW structure for the "layouts" area
  # You're able to define layouts, notifications etc ↴

  # All keys interpolated as strings, so you can use symbols, strings or integers where necessary
  exceptions: {

    :all => {
      layout: "exception", # define layout
      notification: true # (false by default)
  # action: ____, (this is general)
  # background: (can define custom background for exceptions layout if required)
    },
    404 => {
      layout: "exception", # define layout
      notification: true # (false by default)
  # action: ____, (this is general)
  # background: (can define custom background for exceptions layout if required)
    },    
    500 => {
      layout: "exception", # define layout
      notification: true # (false by default)
  # action: ____, (this is general)
  # background: (can define custom background for exceptions layout if required)
    }

    # This is the old structure
    # Still works but will be deprecated in future versions

    # 501 => "exception",
    # 502 => "exception",
    # 503 => "exception",
    # 504 => "exception",
    # 505 => "exception",
    # 507 => "exception",
    # 510 => "exception"

  }
}
Marcelo Austria
  • 861
  • 8
  • 16