如何暂时使我的staging rails服务器显示错误?

时间:2014-05-26 02:57:02

标签: ruby-on-rails

在我的开发框中,当我收到后端错误时,它会给出错误消息和堆栈跟踪。但是在登台服务器上它只是说

"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 个答案:

答案 0 :(得分:7)

是的,您可以通过设置启用在暂存环境中显示堆栈跟踪

config.consider_all_requests_local = true

在您的Rails应用的confing/environments/staging.rb文件中。

答案 1 :(得分:3)

当我只想看到它的一部分时间时,我只需根据环境变量设置Conside_all_requests_local,因此我可以根据需要将其切换为关闭而不更改应用程序的源代码:

配置/环境/ staging.rb:

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

答案 2 :(得分:1)

您可以使用edariedl的答案,但更好的方法是处理服务器上的例外:

Great tutorial here

-

<强>设置

#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

中为您提供更强大的异常处理策略