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?

\$\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)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.