如何处理gem中的异常

时间:2013-10-03 14:46:37

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

我正在调用gem url_for的方法aws-s3,并抛出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

1 个答案:

答案 0 :(得分:3)

捕获错误时更具体:

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