国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Rails中遇到錯誤跳轉到統一提示錯誤頁的方法

2019-10-26 19:24:26
字體:
來源:轉載
供稿:網友

一個迭代開發中的網站難免存在bug,出bug的時候客戶體驗就很不好了,為解決此問題,可以在class error產生的時候,觸發跳轉到統一提示頁面,并給開發人員發郵件報錯誤信息,提高測試能力和用戶體驗。以下是核心方法;在ApplicationController中添加如下代碼,不同rails版本的class error略有變化。

代碼如下:
AR_ERROR_CLASSES = [ActiveRecord::RecordNotFound, ActiveRecord::StatementInvalid] 
  ERROR_CLASSES = [NameError, NoMethodError, RuntimeError, 
         ActionView::TemplateError, 
         ActiveRecord::StaleObjectError, ActionController::RoutingError, 
         ActionController::UnknownController, AbstractController::ActionNotFound, 
         ActionController::MethodNotAllowed, ActionController::InvalidAuthenticityToken] 
 
  ACCESS_DENIED_CLASSES = [CanCan::AccessDenied] 
 
  if Rails.env.production? 
    rescue_from *AR_ERROR_CLASSES, :with => :render_ar_error 
    rescue_from *ERROR_CLASSES, :with => :render_error 
    rescue_from *ACCESS_DENIED_CLASSES, :with => :render_access_denied 
  end 
   
  #called by last route matching unmatched routes.  Raises RoutingError which will be rescued from in the same way as other exceptions. 
 
#備注rails3.1后ActionController::RoutingError在routes.rb中最后加如下代碼才能catch了。 
#rails3下:match '*unmatched_route', :to => 'application#raise_not_found!' 
#rails4下:get '*unmatched_route', :to => 'application#raise_not_found!' 
 
  def raise_not_found! 
    raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}") 
  end 
 
  def render_ar_error(exception) 
    case exception 
    when *AR_ERROR_CLASSES then exception_class = exception.class.to_s 
    else exception_class = 'Exception' 
    end 
 
    send_error_email(exception, exception_class) 
  end 
 
  def render_error(exception) 
    case exception 
    when *ERROR_CLASSES then exception_class = exception.class.to_s 
    else exception_class = 'Exception' 
    end 
 
    send_error_email(exception, exception_class) 
  end 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洛南县| 柳河县| 金秀| 天柱县| 通城县| 全椒县| 连城县| 黑河市| 辉县市| 岐山县| 余姚市| 焉耆| 尉氏县| 长治县| 辽中县| 通榆县| 兴业县| 岑巩县| 当涂县| 康平县| 怀安县| 兴国县| 宜春市| 县级市| 杭州市| 社旗县| 家居| 宁强县| 合水县| 大新县| 定安县| 长春市| 清河县| 巨野县| 浦县| 商都县| 休宁县| 溧水县| 福安市| 凤城市| 尖扎县|