I have created a method, which is raising the same exception in case of any error:
def fetch_aws_instances
region_name = 'us-west-2'
ec2 = Aws::EC2::Client.new(region: region_name)
describe_regions_result = ec2.describe_region
...
...
rescue Seahorse::Client::NetworkingError, Aws::Errors::MissingCredentialsError, Aws::EC2::Errors::UnauthorizedOperation => error
raise error
end
Now, I am calling this method like this:
begin
fetch_aws_instances
rescue Seahorse::Client::NetworkingError
Rails.logger.error 'Network Error: Please check whether the application instance is allowed outbound access to the Internet.'
rescue Aws::Errors::MissingCredentialsError, Aws::EC2::Errors::UnauthorizedOperation
Rails.logger.error 'Authorization Error: Please ensure IAM role is attached to the application instance.'
end
Am I calling the method in correct way, do I need to rescue all the errors or is there some better way to do it?
1 Answer 1
The rescue in your fetch_aws_instances
method is pointless. Rescuing errors just to re-raise them identically, is the same as not rescuing them at all.
You should only rescue when you want to customize what happens if the error is raisedd. Unless you want to move the Rails.logger calls to the fetch_aws_instances
method, you should only have the rescues when you call it (as you do in your second code block)
Explore related questions
See similar questions with these tags.