【问题标题】:Rails exception_handler gemRails 异常处理程序 gem
【发布时间】:2016-10-05 13:43:31
【问题描述】:

我已经安装了 Rails exception_handler 并尝试按照说明设置自定义错误处理,但是我仍然收到 gem 创建的标准 500 错误消息:

500 内部服务器错误

如果你是这个的管理员 网站,然后请阅读此 Web 应用程序的日志文件和/或 Web 服务器的日志文件以找出问题所在。

这是我添加到config/application.rb 的内容:

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

我在layouts/exception.html.erb 创建了一个异常布局:

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

  <body>

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

  </body>
</html>

并生成默认异常视图: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>

我尝试重新启动我的 Rails 服务器只是为了确保更改生效,但它仍然无法正常工作。我错过了什么?

【问题讨论】:

  • 您是否查看了日志文件中的错误是什么?也许这是这个 gem 无法处理的错误。
  • 您的config/development.rb 中确实有config.consider_all_requests_local = true,对吗? PS:我假设您正在开发环境中进行测试。
  • @AlexandreAngelim 是的。
  • @slowjack2k 看起来它正在尝试使用我的默认布局 application.html.erb,而不是 exception.html.erb: Completed 500 Internal Server Error in 299ms Error during failsafe response: undefined method each' 用于 nil:NilClass /Users/godzilla74/Coding/neo-api/app /views/layouts/_navigation.html.erb:38:in _app_views_layouts__navigation_html_erb__2393319544277720129_70250216188520'
  • 您应该将'500' =&gt; 'exception' 更改为500 =&gt; 'exception'(键500 - 是整数而不是字符串)

标签: ruby-on-rails ruby exceptionhandler


【解决方案1】:

在 exception_handler 0.8.0.0 版本中的 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"

  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2020-05-17
    相关资源
    最近更新 更多