I’m trying to get a bucket in Ruby using the AWS SDK, and trying to catch a NoSuchBucket error. Problem is, my rescue block is not catching the error, and so my app crashes. Here is the relevant code:
begin
b = s3.buckets[bucket_name]
rescue AWS::S3::Errors::NoSuchBucket
puts Invalid bucket name.
exit 1
end
and the error message is:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/aws-sdk-1.5.6/lib/aws/core/client.rb:277:in
`return_or_raise': The specified bucket does not exist (AWS::S3::Errors::NoSuchBucket)
Am I just making a stupid beginner syntax error, or is there a bug in the AWS code that’s not actually throwing the error? I’ve also tried catching all errors and still no dice.
Doesn’t actually make any requests and won’t ever through exceptions like
NoSuchBucket.It just returns a bucket object that knows what its name is. A request only happens when you actually try to do something with the bucket (list its contents, add a file to it) and it is at this point that
NoSuchBucketis raised. This is outside of yourbeginblock and so yourrescuedoesn’t handle it. If you need to rescue that exception, you need to be putting your begin/rescue around the places that you actually use the bucket.If you are just trying to validate that it actually exists you could do something like