最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Rails中遇到錯誤跳轉到統(tǒng)一提示錯誤頁的方法

 更新時間:2015年01月04日 09:25:50   投稿:junjie  
這篇文章主要介紹了Rails中遇到錯誤跳轉到統(tǒng)一提示錯誤頁的方法,本文在ApplicationController中添加捕捉錯誤并跳轉到統(tǒng)一提示頁面,需要的朋友可以參考下

一個迭代開發(fā)中的網站難免存在bug,出bug的時候客戶體驗就很不好了,為解決此問題,可以在class error產生的時候,觸發(fā)跳轉到統(tǒng)一提示頁面,并給開發(fā)人員發(fā)郵件報錯誤信息,提高測試能力和用戶體驗。以下是核心方法;在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

相關文章

最新評論

华坪县| 郸城县| 博湖县| 平谷区| 台山市| 浙江省| 梅河口市| 德令哈市| 广州市| 屯门区| 共和县| 琼结县| 略阳县| 罗田县| 游戏| 广德县| 鲁山县| 藁城市| 和硕县| 郎溪县| 微博| 那曲县| 正镶白旗| 黄骅市| 绥江县| 响水县| 乐亭县| 甘德县| 昌乐县| 峨眉山市| 瓦房店市| 吴江市| 台江县| 巴楚县| 克东县| 乳源| 成都市| 林州市| 临沂市| 化州市| 崇义县|