ruby-on-rails - Shopify:从 ShopfyAPI 捕获 RestAPI 调用的异常
问题描述
我试图捕获一个异常,当找不到某个 id 时,但应用程序仍然停止并显示消息:ActiveResource::ResourceNotFound
代码如下:
begin
ShopifyAPI::ScriptTag.find(x.scriptid)
rescue => e
if e.message == '404 Not Found'
# handle 404 error
else
raise e
end
end
我做错什么了吗?
解决方案
这里更好的做法是拯救你想要的异常,而不是StandardError
rescue ActiveResource::ResourceNotFound => e
# handle 404 error
end
我不能马上说为什么你的例子不起作用,但我猜这个信息并不完全正确404 Not Found
在这种情况下您可以使用正则表达式e.message.match?(/404 Not Found/)
,但我更喜欢上面的方法
推荐阅读
- nestjs - Nestjs在useFactory中实例化服务类
- reactjs - Framer Motion - 不透明度过渡在开发中有效,但在部署时无效
- javascript - 通知通过 list.js 过滤的项目数
- firebase - 如何在flutter firebase中使用子集合填充扩展列表视图
- multithreading - GDAL - 多线程 gdalbuildvrt 和 gdalwarp
- excel - 运行时错误 440 属性是只读的 - 在一个宏上
- ios - 单击按钮后将名称附加到数组,然后将其发送到另一个视图控制器
- python - 将python文件转换为ui文件错误
- django - 如果我通过 git 部署了 Django 应用程序,如何创建超级用户?
- node.js - 在节点 js 中使用 puppeteer 时无法单击网页中的按钮