简体   繁体   English

针对javascript请求的Rescue_from

[英]Rescue_from for javascript requests

In my Rails 2.3.8 application I had a rescue_from code for exceptions, which are thrown during javascript actions: 在我的Rails 2.3.8应用程序中,我有一个例外的rescue_from代码,它在javascript操作期间抛出:

rescue_from ::Exception, :with => :show_js_errors

...

def show_js_errors exception
  if request.format == :js
    flash[:error] = 'some error occured'
    render :update do |page|
      page.redirect_to({:controller => '/home', :action => :index})
    end
  else
    # use default error handling for non-JS requests
    rescue_action_without_handler(exception)
  end
end

So my users get an error message, if an ajax call runs into an error. 因此,如果ajax调用遇到错误,我的用户会收到错误消息。 In Rails 3, I can't simply call the default error handling, because the "without_handler" method doesn't exist any more. 在Rails 3中,我不能简单地调用默认的错误处理,因为“without_handler”方法不再存在。

update doh 更新 doh

I posted this after 3 hours of searching, but only 30 minutes after posting I found a solution myself. 经过3个小时的搜索,我发布了这个,但发布后仅30分钟我就找到了解决方案。

Just re-raise the exception. 只是重新提出异常。

Since you are in the error handling, no further handling is done with this exception. 由于您处于错误处理状态,因此不会对此异常进行进一步处理。

Just reraise the exception. 只是重新加注例外。

def show_js_errors exception
  if request.format == :js
    flash[:error] = 'some error occured'
    render :update do |page|
      page.redirect_to({:controller => '/home', :action => :index})
    end
  else
    raise # <<
  end
end

http://simonecarletti.com/blog/2009/11/re-raise-a-ruby-exception-in-a-rails-rescue_from-statement/ concurs: http://simonecarletti.com/blog/2009/11/re-raise-a-ruby-exception-in-a-rails-rescue_from-statement/同意:

 rescue_from ActiveRecord::StatementInvalid do |exception| if exception.message =~ /invalid byte sequence for encoding/ rescue_invalid_encoding(exception) else raise end end 

[...]The exception is correctly rethrown but it isn't catched [sic] by the standard Rails rescue mechanism and the standard exception page is not rendered. [...]异常被正确地重新抛出,但标准的Rails救援机制没有捕获[原文如此] ,并且不呈现标准异常页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM