簡體   English   中英

針對javascript請求的Rescue_from

[英]Rescue_from for javascript requests

在我的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

因此,如果ajax調用遇到錯誤,我的用戶會收到錯誤消息。 在Rails 3中,我不能簡單地調用默認的錯誤處理,因為“without_handler”方法不再存在。

更新 doh

經過3個小時的搜索,我發布了這個,但發布后僅30分鍾我就找到了解決方案。

只是重新提出異常。

由於您處於錯誤處理狀態,因此不會對此異常進行進一步處理。

只是重新加注例外。

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/同意:

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

[...]異常被正確地重新拋出,但標准的Rails救援機制沒有捕獲[原文如此] ,並且不呈現標准異常頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM