Rails Resuce,在 Switch 救援中提供信息
我有以下问题:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
rescue
关键字仅捕获错误对象。您需要在异常对象中捕获这些值:但是这是对错误处理的滥用。对于真正的错误、您没有预料到的错误,通常最好使用 begin 和 raise 。 MissingInfo 听起来像是处理用户输入。您可以预期用户输入会丢失数据。对此进行正常检查。试着想想你真正想传达的行为。
The
rescue
keyword just captures the error object. You need to capture these values inside an exception object: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.