Tag: exception

Devise中的InvalidAuthenticityToken :: SessionsController#destroy(已经注销后退出)

我正在使用Devise 3.2.0进行身份validation,并在执行以下操作时发现问题: 标签1:登录应用 选项卡2:转到应用程序中的任何页面 选项卡2: 退出 (成功) 选项卡1: 退出 (失败 – 请参阅下面的例外情况) 提出例外: ActionController :: Devise中的InvalidAuthenticityToken :: SessionsController#destroy 在开发日志中,我看到: 无法validationCSRF令牌的真实性 堆栈跟踪的前三行是: ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken): actionpack (4.0.0) lib/action_controller/metal/request_forgery_protection.rb:163:in `handle_unverified_request’ actionpack (4.0.0) lib/action_controller/metal/request_forgery_protection.rb:170:in `handle_unverified_request’ devise (3.2.0) lib/devise/controllers/helpers.rb:198:in `handle_unverified_request’ 如何确保连续退出不会引发exception?

替代“救援exception”

我偶尔会遇到一些意外错误,例如超时错误,503错误等。有些错误我甚至不知道我可能收到的错误。 做以下事情我无法解释所有这些: rescue Timeout::Error => e 拯救Exception也是一个可怕的想法。 我可以使用哪种替代方案? 当出现错误时,我希望我的代码能够拯救所有这些代码; 如果没有错误,我需要避免它。 我希望能够杀死我的脚本但不会跳过语法错误等。

当你没有在ruby中指定exception类时,你会遇到哪些exception?

当您未指定如下的exception类时,您会捕获哪些exception: begin # do something rescue puts “Exception!” end

Ruby如何知道要救援什么?

我正在使用eurovat gem检查增值税号。 Eurovat.check_vat_number vat_number 如果我从irb运行,有时我会收到: SOAP::FaultError: MS_UNAVAILABLE from (“来自”之后没有任何内容) 我想写一个begin rescue块来拯救这些错误,但我怎么知道要救援什么? 我试过救援SOAP::FaultError但是没有用

如何在Rails中捕获整个应用程序的ArgumentError?

环境 铁路3.1 Ruby 1.9.1 我已经尝试过application_controller但似乎没有用。 我可能做错了什么? rescue_from ArgumentError do |exception| flash.now[:error] = “Arguments for your request are incorrect” #ExceptionNotifier::Notifier.background_exception_notification(exception).deliver if Rails.env.production? redirect_to root_url, :alert => exception.message end 我试图处理的例外情况 A ArgumentError occurred in marketplace#index: invalid byte sequence in UTF-8 .bundle/gems/ruby/1.9.1/gems/rack-1.4.5/lib/rack/utils.rb:104:in `normalize_params’ 要么 A ArgumentError occurred in connect#index: invalid %-encoding (%u2713) .bundle/gems/ruby/1.9.1/gems/rack-1.4.5/lib/rack/backports/uri/common_192.rb:46:in `decode_www_form_component’

无法解救YAML.loadexception

我正在尝试处理在Ruby中加载无效的YAML数据,但似乎无法挽救心理引发的exception。 这是一些示例代码,用于演示我遇到的问题: require ‘yaml’ begin YAML.load(‘&*%^*’) rescue puts “Rescued” end 例外: # ruby test.rb /usr/lib64/ruby/1.9.1/psych.rb:203:in `parse’: (): did not find expected alphabetic or numeric character while scanning an anchor at line 1 column 1 (Psych::SyntaxError) from /usr/lib64/ruby/1.9.1/psych.rb:203:in `parse_stream’ from /usr/lib64/ruby/1.9.1/psych.rb:151:in `parse’ from /usr/lib64/ruby/1.9.1/psych.rb:127:in `load’ from test.rb:3:in `’

为未处理的exception注册处理程序

是否可以为任何未处理的exception定义exception处理程序? 将我的整个代码块包装在begin / rescue / end块中会感觉很乱。

Ruby“定义?”运算符错误?

所以,我们有代码: class Foo def bar puts “Before existent: #{(defined? some_variable)}” puts “Before not_existent: #{(defined? nonexistent_variable)}” raise “error” some_variable = 42 rescue puts “exception” ensure puts “Ensure existent: #{(defined? some_variable)}” puts “Ensure not_existent: #{(defined? nonexistent_variable)}” end end 并从irb调用它: > Foo.new.bar 而且,这将返回: Before existent: Before not_existent: exception Ensure existent: local-variable Ensure not_existent: => nil 现在问题 – 为什么? […]

为什么控制器没有捕获错误

我试图从控制器中的RecordNotFound错误中解救,如下所示: def create_user_role authorize User role = params[:user][:rolify_role].to_sym resource_type = params[:user][:resource_type] resource_id = params[:user][:id] # Catch RecordNotFound doesn’t work begin resource = nil resource = resource_type.constantize.find(resource_id) if RolifyRoles.available_resources.include?(resource_type) && resource_id.present? rescue ActiveRecord::RecordNotFound => e format.html { render :show } flash[:error] = e.message end end 并且当resource_type.constantize.find(resource_id)找不到记录时,它不会被救援块捕获。 Completed 500 Internal Server Error in 46ms (ActiveRecord: 3.2ms) ActiveRecord::RecordNotFound […]

为什么在Ruby中,|| 当`a`未定义时,1会抛出错误,但a = a || 1会不会?

当a未定义时,则为a || 1 a || 1将抛出错误,但a = a || 1 a = a || 1不会。 是不是有点不一致? irb(main):001:0> a NameError: undefined local variable or method ‘a’ for main:Object from (irb):1 from c:/ruby/bin/irb:12:in ” irb(main):002:0> a || 1 NameError: undefined local variable or method ‘a’ for main:Object from (irb):2 from c:/ruby/bin/irb:12:in ” irb(main):003:0> a = a || […]