国产探花免费观看_亚洲丰满少妇自慰呻吟_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 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玉树县| 舟山市| 胶南市| 贡觉县| 东光县| 西和县| 贵港市| 深水埗区| 吉木乃县| 洪湖市| 儋州市| 额敏县| 辉南县| 达孜县| 乌苏市| 凌云县| 嘉兴市| 旬阳县| 剑河县| 谷城县| 江都市| 襄垣县| 榕江县| 舒兰市| 高碑店市| 高密市| 富蕴县| 航空| 阜宁县| 阿拉善右旗| 高平市| 都安| 金塔县| 贺兰县| 贵定县| 绍兴县| 广饶县| 赫章县| 久治县| 城口县| 当雄县|