Skip to content

Commit

Permalink
Merge pull request ansible#71 from bdunne/error_subclassing
Browse files Browse the repository at this point in the history
All client errors should derive from AnsibleTowerClient::ClientError
  • Loading branch information
gmcculloug authored Feb 16, 2017
2 parents 96f90f7 + 92c1814 commit b0b0959
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 4 additions & 2 deletions lib/ansible_tower_client/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ def method_missing(method_name, *args, &block)
else
super
end
rescue Faraday::ConnectionFailed, Faraday::SSLError => err
raise
rescue Faraday::ConnectionFailed => err
raise AnsibleTowerClient::ConnectionError, err
rescue Faraday::SSLError => err
raise AnsibleTowerClient::SSLError, err
rescue Faraday::ClientError => err
raise if err.response.nil?
response = err.response
Expand Down
11 changes: 7 additions & 4 deletions lib/ansible_tower_client/exception.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module AnsibleTowerClient
class Error < Exception; end
class ClientError < Error; end
class ConnectionError < Error; end
class Error < Exception; end
class ClientError < Error; end
class NoMethodError < Error; end
class UnlicensedFeatureError < Error; end

class ConnectionError < ClientError; end
class ResourceNotFoundError < ClientError; end
class SSLError < ClientError; end
class UnlicensedFeatureError < ClientError; end
end
4 changes: 2 additions & 2 deletions lib/ansible_tower_client/middleware/raise_tower_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def on_complete(env)
when 402
raise AnsibleTowerClient::UnlicensedFeatureError
when 404
raise Faraday::Error::ResourceNotFound, response_values(env)
raise AnsibleTowerClient::ResourceNotFoundError, response_values(env)
when 407
# mimic the behavior that we get with proxy requests with HTTPS
raise Faraday::Error::ConnectionFailed, %(407 "Proxy Authentication Required ")
raise AnsibleTowerClient::ConnectionError, %(407 "Proxy Authentication Required ")
when CLIENT_ERROR_STATUSES
raise AnsibleTowerClient::ClientError, env.body
end
Expand Down
5 changes: 3 additions & 2 deletions spec/middleware/raise_tower_error_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

require 'faraday'
require 'ansible_tower_client/middleware/raise_tower_error'
require_relative '../spec_helper'
Expand All @@ -20,11 +21,11 @@
end

it "raises ResourceNotFound exception with a status 404" do
expect { error.on_complete(env_404) }.to raise_error(Faraday::Error::ResourceNotFound)
expect { error.on_complete(env_404) }.to raise_error(AnsibleTowerClient::ResourceNotFoundError)
end

it "raises ConnectionFailed exception with a status 407" do
expect { error.on_complete(env_407) }.to raise_error(Faraday::Error::ConnectionFailed)
expect { error.on_complete(env_407) }.to raise_error(AnsibleTowerClient::ConnectionError)
end
end
end
Expand Down

0 comments on commit b0b0959

Please sign in to comment.