【问题标题】:How to handle exceptions from a gem如何处理来自 gem 的异常
【发布时间】:2013-10-03 14:46:36
【问题描述】:

我正在调用 gem aws-s3 的方法 url_for,它会抛出一个 MissingCredentialsError < StandardError。如何从调用 url_for 的类中拯救它?我知道丢失凭据问题的解决方案,但我想知道如何处理异常。试过了,没有运气:

begin
  ... code that get the error...
rescue MissingCredentialsError
  a = "THIS IS AN ERROR"
end

当我运行我的测试时,它会遇到这个错误:

ActionView::Template::Error: uninitialized constant Receipt::MissingCredentialsError

【问题讨论】:

  • 正确的响应取决于上下文,例如url_for 成功对代码功能的重要性。它可以是任何东西,从打印有用的错误消息并退出,到忽略错误并继续执行多个请求的循环,就好像没有任何问题一样
  • 怎么不行?您的代码应该捕获异常。
  • 对不起,我忘了把错误放在那里。编辑和添加

标签: ruby-on-rails ruby exception-handling amazon-web-services amazon-s3


【解决方案1】:

捕获错误时更具体:

begin
  # code that get the error
rescue AWS::Errors::MissingCredentialsError => e
  # code that handles the exception
end

【讨论】:

  • 是的,这就是问题所在……我对 ruby​​ 很陌生。天哪,新手对吗?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多