如何临时让我的暂存 Rails 服务器显示错误?

4
在我的开发环境中,当我遇到后端错误时,它会给我显示错误消息和堆栈跟踪。然而在演示服务器上,它只是显示:
"We're sorry, but something went wrong. If you are the application owner check the logs for more information." 

是的,我知道我可以查看日志。但是如果我想在浏览器中查看堆栈跟踪怎么办?我能暂时启用它吗?这是针对Rails 4.04和Ruby 2.1的。

3个回答

9

是的,你可以通过设置,在你的测试环境中启用显示堆栈跟踪。

config.consider_all_requests_local = true

在你的Rails应用程序中的confing/environments/staging.rb文件中。

4

当我只想有时查看它时,我只需根据环境变量设置consider_all_requests_local,这样我可以在不更改应用程序源代码的情况下随时打开或关闭它:

config/environments/staging.rb:

config.consider_all_requests_local = !ENV['LOCAL_REQUESTS'].nil?

1
你可以使用 edariedl 的答案,但更好的方法是在服务器端处理异常:

这里有一个很棒的教程

--

设置

#config/environments/staging.rb
config.exceptions_app = ->(env) { ExceptionController.action(:show).call(env) }

控制器

#app/controllers/exception_controller.rb
class ExceptionController < ApplicationController

  #Response
  respond_to :html, :xml, :json

    #Dependencies
    before_action :status

  #Layout
  layout :layout_status

  ####################
  #      Action      #
  ####################

    #Show
  def show
    respond_with status: @status
  end

  ####################
  #   Dependencies   #
  ####################

  protected

  #Info
  def status
    @exception  = env['action_dispatch.exception']
    @status     = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
    @response   = ActionDispatch::ExceptionWrapper.rescue_responses[@exception.class.name]
  end

  #Format
  def details
    @details ||= {}.tap do |h|
      I18n.with_options scope: [:exception, :show, @response], exception_name: @exception.class.name, exception_message: @exception.message do |i18n|
        h[:name]    = i18n.t "#{@exception.class.name.underscore}.title", default: i18n.t(:title, default: @exception.class.name)
        h[:message] = i18n.t "#{@exception.class.name.underscore}.description", default: i18n.t(:description, default: @exception.message)
      end
    end
  end
  helper_method :details

  ####################
  #      Layout      #
  ####################

  private

  #Layout
  def layout_status
    @status.to_s == "404" ? "application" : "error"
  end

end

视图

#app/views/exception/show.html.haml
.box
    %h1
        = details[:name]
    %p
        = details[:message]


#app/views/layouts/error.html.haml
!!!
%html
%head

    /Info
    = meta_tags

    /CSS
    :css
        html {
            height: 100%;
            background: #fff;
        }
        body {
            font-family: Helvetica, Arial, Sans-Serif;
            font-size: 14px;
        }

        .error_container {
            display: block;
            margin: auto;
            margin: 10% auto 0 auto;
            width: 40%;
        }
        .error_container .error {
            display: block; 
            text-align: center;
        }
        .error_container .error img {
            display: block;
            margin: 0 auto 15px auto;
        }
        .error_container .message > * {
            display: block;
        }
        .error_container .message strong {
            font-weight: bold;
            color: #f00;
        }
        .error_container .contact_info {
            display: block;
            text-align: center;
            margin: 25px 0 0 0;
        }
        .error_container .contact_info a {
            display: inline-block;
            margin: 0;
            opacity: 0.4;
            transition: opacity 0.15s ease;
        }
        .error_container .contact_info a:hover {
            opacity: 0.8;
        }

/Body
%body
    .error_container
        = yield

这个结合了一个叫做ExceptionNotification的 gem,会使你在 stagingproduction 中拥有更强大的异常处理策略。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接