From b45dd25afe54e9d027fe62c61bea4b9172e2c925 Mon Sep 17 00:00:00 2001 From: Sam Lucidi Date: Wed, 25 Oct 2017 17:40:12 -0400 Subject: [PATCH] Translate exceptions from raw_connect --- .../providers/openstack/manager_mixin.rb | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/app/models/manageiq/providers/openstack/manager_mixin.rb b/app/models/manageiq/providers/openstack/manager_mixin.rb index 2042faae4..ec0aa39e0 100644 --- a/app/models/manageiq/providers/openstack/manager_mixin.rb +++ b/app/models/manageiq/providers/openstack/manager_mixin.rb @@ -25,7 +25,32 @@ def raw_connect(password, params, service = "Compute") authentication = {:userid => user, :password => MiqPassword.try_decrypt(password), :save => false, :role => 'default', :authtype => 'default'} ems.connection_configurations = [{:endpoint => endpoint, :authentication => authentication}] - ems.connect(:service => service) + + begin + ems.connect(:service => service) + rescue => err + miq_exception = translate_exception(err) + raise unless miq_exception + + _log.error("Error Class=#{err.class.name}, Message=#{err.message}") + raise miq_exception + end + end + + def translate_exception(err) + require 'excon' + case err + when Excon::Errors::Unauthorized + MiqException::MiqInvalidCredentialsError.new("Login failed due to a bad username or password.") + when Excon::Errors::Timeout + MiqException::MiqUnreachableError.new("Login attempt timed out") + when Excon::Errors::SocketError + MiqException::MiqHostError.new("Socket error: #{err.message}") + when MiqException::MiqInvalidCredentialsError, MiqException::MiqHostError + err + else + MiqException::MiqEVMLoginError.new("Unexpected response returned from system: #{err.message}") + end end end @@ -132,19 +157,7 @@ def stop_event_monitor_queue_on_credential_change end def translate_exception(err) - require 'excon' - case err - when Excon::Errors::Unauthorized - MiqException::MiqInvalidCredentialsError.new "Login failed due to a bad username or password." - when Excon::Errors::Timeout - MiqException::MiqUnreachableError.new "Login attempt timed out" - when Excon::Errors::SocketError - MiqException::MiqHostError.new "Socket error: #{err.message}" - when MiqException::MiqInvalidCredentialsError, MiqException::MiqHostError - err - else - MiqException::MiqEVMLoginError.new "Unexpected response returned from system: #{err.message}" - end + self.class.translate_exception(err) end def verify_api_credentials(options = {})