校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > Rails中遇到錯誤跳轉到統一提示錯誤頁的方法

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

熱門標簽:天津外呼系統怎么收費 巫師3地圖標注魔力之所 AI電銷機器人 線路 漯河電銷 柯城手機地圖如何做地圖標注 淮安自動外呼系統供應商 中牟外呼系統違法嗎 征服者企業地圖標注 外呼線路從哪里出來的

一個迭代開發中的網站難免存在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

標簽:甘孜 內江 棗莊 西雙版納 大慶 河池 南昌 克拉瑪依

巨人網絡通訊聲明:本文標題《Rails中遇到錯誤跳轉到統一提示錯誤頁的方法》,本文關鍵詞  Rails,中,遇到,錯誤,跳,轉到,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Rails中遇到錯誤跳轉到統一提示錯誤頁的方法》相關的同類信息!
  • 本頁收集關于Rails中遇到錯誤跳轉到統一提示錯誤頁的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 昂仁县| 来宾市| 新营市| 涟水县| 阜宁县| 彩票| 堆龙德庆县| 晋江市| 鄱阳县| 商水县| 同江市| 政和县| 建始县| 杭锦后旗| 会理县| 楚雄市| 玉溪市| 定边县| 潼关县| 报价| 三台县| 平顶山市| 青冈县| 大竹县| 揭东县| 湖口县| 嘉峪关市| 湄潭县| 理塘县| 万荣县| 陕西省| 湘潭县| 商洛市| 杭州市| 黄陵县| 明星| 油尖旺区| 孝义市| 晋中市| 图们市| 安远县|