Ruby如何在AWS Lambda处理程序中引发错误

问题描述 投票:0回答:1

我在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失败了,该如何实现?

ruby amazon-web-services aws-lambda
1个回答
0
投票

您可以使用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

© www.soinside.com 2019 - 2024. All rights reserved.