我在AWS Lambda处理程序中调用了两个彼此不相关的类。因为我需要设置相同的cron作业调度程序,所以两者都在同一个lambda处理程序中。如果某人失败,但我想显示一个错误,但同时应该再次调用该类。
def handle(event:, context:)
ListCreator.new.call
Messenger.new.call
{ statusCode: 200 }
end
例如
ListCreator.new.call无效->引发错误'I was not able to send a message'
-> Messenger.new.call-> 200
如果Messenger失败了,该如何实现?
您可以使用ensure
def handle(event:, context:)
ListCreator.new.call
rescue SomeErrorTypeFromListCreator => e # or just rescue => e
raise 'I was not able to send a message'
ensure
Messenger.new.call
{ statusCode: 200 }
rescue SomeErrorTypeFromMessenger => e # or just rescue => e
raise 'I was not able to send the other message'
end
[ensure
块每次都会被调用,source