【问题标题】:rails 3.0 exception handling in middleware中间件中的 rails 3.0 异常处理
【发布时间】:2010-11-05 15:02:08
【问题描述】:

在 rails 3.0 中,我试图围绕中间件代码进行异常处理。具体来说,如果请求的内容类型为 application/json 但输入的 json 无效,rails 当前会呈现 public/500.html - 这是不幸的。

由于这还没有在控制器中,所以我看到的大多数东西都不起作用/适用。

【问题讨论】:

  • 您找到解决方案了吗?我遇到了同样的问题。

标签: exception ruby-on-rails-3


【解决方案1】:

你可以从抛出的异常中解救出来并决定做什么:

rescue_from Exception, :with => :render_exception

def render_exception
  # examine the Exception here
  # and decide which template to render
  render :template => "shared/???.html", :status => ???, :layout => 'error' 
end

将此代码放在 app/controllers/application_controller.rb 中

我希望这就是您正在寻找的...

【讨论】:

    猜你喜欢
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多