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

首頁 > 編程 > Ruby > 正文

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

2020-10-29 19:43:20
字體:
來源:轉載
供稿:網友

一個迭代開發中的網站難免存在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 
 
  def render_access_denied(exception) 
    case exception 
    when *ACCESS_DENIED_CLASSES then exception_class = exception.class.to_s 
    else exception_class = "Exception" 
    end 
 
    send_error_email(exception, exception_class) 
  end

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台安县| 巨鹿县| 德兴市| 丰城市| 漯河市| 亚东县| 洛宁县| 含山县| 兴和县| 奈曼旗| 隆林| 凤冈县| 扶绥县| 菏泽市| 墨竹工卡县| 阳新县| 棋牌| 龙井市| 奉化市| 蚌埠市| 重庆市| 尖扎县| 灵寿县| 屏东市| 涿州市| 云龙县| 金川县| 九龙坡区| 漳平市| 舟山市| 收藏| 沂南县| 绥芬河市| 交城县| 曲水县| 微博| 兴和县| 城步| 河北区| 秀山| 田阳县|