zoukankan      html  css  js  c++  java
  • rails异常(错误处理)

    方法1:http://www.uedidea.com/rails%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86.html

    Ruby代码  收藏代码
    1. def add_to_cart  
    2. product = Product.find(params[:id])  
    3. rescue ActiveRecord::RecordNotFound #拦截Product.find()异常  
    4. logger.error("Attempt to access invalid product #{params[:id]}") #日志记录  
    5. flash[:notice] = "Invalid product" #notice 存放提示信息  
    6. redirect_to :action => "index" #跳转到index  
    7. end  
    8.   
    9. begin  
    10.    @user.destroy  
    11.     flash[:notice] = "User #{@user.name} deleted"  
    12. rescue Exception => e #如果上面的代码执行发生异常就捕获  
    13.    flash[:notice] = e.message  
    14. end  
    Ruby代码  收藏代码
    1. begin  
    2.   raise ArgumentError, "Bad data"  
    3. rescue => err   
    4.   puts err   
    5. ensure  
    6. ...                       #执行清理工作   
    7. end    




    方法2:around_filter  

    http://hlee.iteye.com/blog/323025

    Ruby代码  收藏代码
    1. around_filter :rescue_record_not_found       
    2.       
    3. def rescue_record_not_found       
    4.   begin       
    5.     yield       
    6.   rescue ActiveRecord::RecordNotFound       
    7.     render :file => "#{RAILS_ROOT}/public/404.html"      
    8.   end       
    9. end      

     



       hlee 在文中还提到可以这样做:

    Ruby代码  收藏代码
    1. rescue_from ActiveRecord::RecordNotFound, with => :rescue_record_not_found       
    2.       
    3. def rescue_record_not_found       
    4.   render :file => "#{RAILS_ROOT}/public/404.html"      
    5. end      

     

    方法3:rescue_action_in_public



    参考:http://202.199.224.30/post/article/7

    Ruby代码  收藏代码
    1. def rescue_action_in_public(exception)        
    2.     logger.error("rescue_action_in_public executed")        
    3.     case exception        
    4.     when ActiveRecord::RecordNotFound, ::ActionController::RoutingError,          
    5.       ::ActionController::UnknownAction        
    6.       logger.error("404 displayed")        
    7.       render(:file  => "#{RAILS_ROOT}/public/404.html",        
    8.       :status   => "404 Not Found")        
    9.     else        
    10.       logger.error("500 displayed")        
    11.       render(:file  => "#{RAILS_ROOT}/public/500.html",        
    12.       :status   => "500 Error")        
    13. #      SystemNotifier.deliver_exception_notification(self, request,          
    14. #                                                    exception)        
    15.     end        
    16.   end      



       注意:在不同环境中的配置,生产环境中,默认的配置应该就可以显示效果,但在开发模式下,需要确认/config/environments/development.rb中的



    代码

    Ruby代码  收藏代码
    1. config.action_controller.consider_all_requests_local = false     

     
    如果在本机访问必须增加(app/controllers/application.rb):



    代码

    Ruby代码  收藏代码
    1. def local_request?   
    2.   false   
    3. end    

     


    错误类型参考:

    Ruby代码  收藏代码
    1. DEFAULT_RESCUE_RESPONSE = :internal_server_error    
    2. DEFAULT_RESCUE_RESPONSES = {    
    3.   'ActionController::RoutingError'             => :not_found,    
    4.   'ActionController::UnknownAction'            => :not_found,    
    5.   'ActiveRecord::RecordNotFound'               => :not_found,    
    6.   'ActiveRecord::StaleObjectError'             => :conflict,    
    7.   'ActiveRecord::RecordInvalid'                => :unprocessable_entity,    
    8.   'ActiveRecord::RecordNotSaved'               => :unprocessable_entity,    
    9.   'ActionController::MethodNotAllowed'         => :method_not_allowed,    
    10.   'ActionController::NotImplemented'           => :not_implemented,    
    11.   'ActionController::InvalidAuthenticityToken' => :unprocessable_entity    
    12. }    
    13.     
    14. DEFAULT_RESCUE_TEMPLATE = 'diagnostics'    
    15. DEFAULT_RESCUE_TEMPLATES = {    
    16.   'ActionView::MissingTemplate'       => 'missing_template',    
    17.   'ActionController::RoutingError'    => 'routing_error',    
    18.   'ActionController::UnknownAction'   => 'unknown_action',    
    19.   'ActionView::TemplateError'         => 'template_error'    
    20. }    

     
    参考网站:

      http://runupwind.iteye.com/blog/1100925
      http://www.uedidea.com/rails-erorr-handle.html
      http://www.iteye.com/topic/708334

  • 相关阅读:
    SYSTEM32 下的几乎所有文件的简单说明
    Windows2000 下管理员账户真的不安全
    Windows2000 下管理员账户真的不安全
    SYSTEM32 下的几乎所有文件的简单说明
    看看你电脑里面有多少垃圾文件浪费内存
    巧妙利用三招保护局域网中的 IP 地址
    Sun推出OpenSolaris 为技术立异注入生机
    高质量C /C编程指南附录B :C /C试题和答案
    高质量C /C编程指南第8章 C 函数的初级特性
    俄罗斯邮政将迁徙到Linux 有关机构已入手入手测试Linux
  • 原文地址:https://www.cnblogs.com/qinyan20/p/3954293.html
Copyright © 2011-2022 走看看