Rails Resuce,在 Switch 救援中提供信息

发布于 2024-10-07 10:01:06 字数 922 浏览 0 评论 0原文

我有以下问题:

class MailingJob < Struct.new(:mailing_id)

  class MissingInfo < ArgumentError; end

  def perform
   ....

    begin
      ......
          raise MissingInfo, "Not found", message_all, @message_from if @message_reply.length == 0
      ......    
    rescue MissingInfo => reason, message_all, message_from
      UserMailer.delay.incoming_mails_error_notification(reason, message_all, message_from)
    end

  end

我在这里遇到的问题是,在我的恢复中,我需要访问开始块中的几个变量,因此当我调用 RAISE 时,我试图传递它们。这似乎不起作用。此外,这些变量在多次加薪中都是一致的,因此它确实填满了页面。

有没有办法使这些变量可以在resuce中访问,而不必在raise中定义它们?

如果没有,我该如何使用 raise 来拯救它们?以上错误如下:

SyntaxError (/Users/xxxxx/Sites/xxxxxxx/lib/mailing_job.rb:117: syntax error, unexpected ',', expecting kTHEN or ':' or '\n' or ';'
    rescue MissingInfo => reason, message_all, message_from
                                 ^

谢谢!

I have the following:

class MailingJob < Struct.new(:mailing_id)

  class MissingInfo < ArgumentError; end

  def perform
   ....

    begin
      ......
          raise MissingInfo, "Not found", message_all, @message_from if @message_reply.length == 0
      ......    
    rescue MissingInfo => reason, message_all, message_from
      UserMailer.delay.incoming_mails_error_notification(reason, message_all, message_from)
    end

  end

The problem I'm having here is that in my resuce, I need access to several of the vars in the begin block, so I'm trying to pass them when I call RAISE. That doesn't appear to be working. Also, these variables are consisten across many raises, so it really fills up the page.

Is there a way to make these variable accessible in the resuce without having to define them in the raise?

If not, how do I use raise to pass them to the rescue? The above errors with:

SyntaxError (/Users/xxxxx/Sites/xxxxxxx/lib/mailing_job.rb:117: syntax error, unexpected ',', expecting kTHEN or ':' or '\n' or ';'
    rescue MissingInfo => reason, message_all, message_from
                                 ^

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九局 2024-10-14 10:01:06

rescue 关键字仅捕获错误对象。您需要在异常对象中捕获这些值:

class MissingInfo < ArgumentError
   attr_accessor :messages
   def initialize(messages = {})
     self.messages = messages
   end
end

begin
  raise MissingInfo.new(:all => message_all, :from => message_from, :reason => reason)
rescue MissingInfo => missing_info
  puts missing_info.messages[:all]
end

但是这是对错误处理的滥用。对于真正的错误、您没有预料到的错误,通常最好使用 begin 和 raise 。 MissingInfo 听起来像是处理用户输入。您可以预期用户输入会丢失数据。对此进行正常检查。试着想想你真正想传达的行为。

The rescue keyword just captures the error object. You need to capture these values inside an exception object:

class MissingInfo < ArgumentError
   attr_accessor :messages
   def initialize(messages = {})
     self.messages = messages
   end
end

begin
  raise MissingInfo.new(:all => message_all, :from => message_from, :reason => reason)
rescue MissingInfo => missing_info
  puts missing_info.messages[:all]
end

BUT This is an abuse of error handling. It's usually better to use begin and raise for real errors, errors which you do not expect. MissingInfo sounds like handling user input. You can expect user input to have missing data. Do normal checks for that. Try to think of the behavior you are really trying to convey.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文