1
\$\begingroup\$

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?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jun 27, 2019 at 16:52
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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)

answered Jun 27, 2019 at 20:24
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.