【发布时间】:2019-06-11 21:43:32
【问题描述】:
我正在运行一个异常处理程序,然后在另一个文件中想要评估异常处理程序是否成功运行,或者救援案例是否运行。
我对 Ruby 很陌生,我不确定如何评估异常处理程序中实际发生的情况,以及如何存储结果(或者是否可能)。代码如下所示。
文件一 - 运行 API 调用
begin
HTTParty.get(BASE_URL + url)
rescue
Hash['message' => 'There was an error connecting with the API, contact support if error persists.']
end
文件二 - 分析 API 调用是否成功
response = call_to_api #api call is ran in file one
if response == #I'm not sure what to put here, but it needs to check if the exception handler didn't trip the rescue
success
else
error
end
【问题讨论】:
-
顺便说一句,
Hash['message' => 'There was...']不是创建哈希的正确方法。Hash[]method 返回一个新的哈希,其中对象的键和值作为其参数。你给出的参数已经是一个哈希(键'message'和值'There was...'),你得到的是它的副本。您应该只使用哈希文字:{ 'message' => 'There was...' }.