diff --git a/lib/netbox_client_ruby.rb b/lib/netbox_client_ruby.rb index 28df3dc..63efc1d 100644 --- a/lib/netbox_client_ruby.rb +++ b/lib/netbox_client_ruby.rb @@ -36,9 +36,6 @@ loader.setup end -# load gem errors -require_relative 'netbox_client_ruby/error' - module NetboxClientRuby extend Dry::Configurable diff --git a/lib/netbox_client_ruby/api/secrets/rsa_key_pair.rb b/lib/netbox_client_ruby/api/secrets/rsa_key_pair.rb index 3b26742..cfba385 100644 --- a/lib/netbox_client_ruby/api/secrets/rsa_key_pair.rb +++ b/lib/netbox_client_ruby/api/secrets/rsa_key_pair.rb @@ -25,7 +25,7 @@ def get if authorization_token @response ||= response connection.get(PATH) else - raise LocalError, + raise NetboxClientRuby::Error::LocalError, "The authorization_token has not been configured, but it's required for get-session-key." end end diff --git a/lib/netbox_client_ruby/api/secrets/session_key.rb b/lib/netbox_client_ruby/api/secrets/session_key.rb index 08fcced..58d0bfa 100644 --- a/lib/netbox_client_ruby/api/secrets/session_key.rb +++ b/lib/netbox_client_ruby/api/secrets/session_key.rb @@ -25,7 +25,7 @@ def request if authorization_token response(post) else - raise LocalError, + raise NetboxClientRuby::Error::LocalError, "The authorization_token has not been configured, but it's required for get-session-key." end end @@ -57,15 +57,15 @@ def decode_private_key(encoded_private_key) return private_key if private_key.private? rescue OpenSSL::PKey::RSAError if rsa_private_key_password.empty? - raise LocalError, + raise NetboxClientRuby::Error::LocalError, "The private key at '#{rsa_private_key_path}' requires a password, but none was given, or the key data is corrupted. (The corresponding configuration is 'netbox.auth.rsa_private_key.password'.)" else - raise LocalError, + raise NetboxClientRuby::Error::LocalError, "The password given for the private key at '#{rsa_private_key_path}' is not valid or the key data is corrupted. (The corresponding configuration is 'netbox.auth.rsa_private_key.password'.)" end end - raise LocalError, + raise NetboxClientRuby::Error::LocalError, "The file at '#{rsa_private_key_path}' is not a private key, but a private key is required for get-session-key. (The corresponding configuration is 'netbox.auth.rsa_private_key.path'.)" end # rubocop:enable Layout/LineLength,Metrics/MethodLength @@ -84,14 +84,14 @@ def read_private_key_file(key_file) encoded_private_key = key_file.read return encoded_private_key unless encoded_private_key.nil? || encoded_private_key.empty? - raise LocalError, + raise NetboxClientRuby::Error::LocalError, "The file at '#{rsa_private_key_path}' is empty, but a private key is required for get-session-key. (The corresponding configuration is 'netbox.auth.rsa_private_key.path'.)" end def open_private_key_file return File.new rsa_private_key_path if File.exist? rsa_private_key_path - raise LocalError, + raise NetboxClientRuby::Error::LocalError, "No file exists at the given path '#{rsa_private_key_path}', but it's required for get-session-key. (The corresponding configuration is 'netbox.auth.rsa_private_key.path'.)" end # rubocop:enable Layout/LineLength diff --git a/lib/netbox_client_ruby/communication.rb b/lib/netbox_client_ruby/communication.rb index c173129..326ba62 100644 --- a/lib/netbox_client_ruby/communication.rb +++ b/lib/netbox_client_ruby/communication.rb @@ -31,9 +31,9 @@ def raise_on_http_error(response) # rubocop:disable Metrics/MethodLength when 300..499 raise_on_http_client_error response when 500..599 - raise NetboxClientRuby::RemoteError, "#{status} Remote Error#{formatted_body(body)}" + raise NetboxClientRuby::Error::RemoteError, "#{status} Remote Error#{formatted_body(body)}" else - raise NetboxClientRuby::RemoteError, "#{status} Unknown Error Code#{formatted_body(body)}" + raise NetboxClientRuby::Error::RemoteError, "#{status} Unknown Error Code#{formatted_body(body)}" end end @@ -60,7 +60,7 @@ def raise_on_http_client_error(response) # rubocop:disable Metrics/MethodLength end def raise_client_error(message, body = nil) - raise NetboxClientRuby::ClientError, "#{message}#{formatted_body(body)}" + raise NetboxClientRuby::Error::ClientError, "#{message}#{formatted_body(body)}" end def formatted_body(body) diff --git a/lib/netbox_client_ruby/entity.rb b/lib/netbox_client_ruby/entity.rb index 914a228..35556a9 100644 --- a/lib/netbox_client_ruby/entity.rb +++ b/lib/netbox_client_ruby/entity.rb @@ -137,7 +137,7 @@ def revert end def reload - raise LocalError, "Can't 'reload', this object has never been saved" unless ids_set? + raise NetboxClientRuby::Error::LocalError, "Can't 'reload', this object has never been saved" unless ids_set? @data = get revert @@ -151,14 +151,14 @@ def save end def create(raw_data) - raise LocalError, "Can't 'create', this object already exists" if ids_set? + raise NetboxClientRuby::Error::LocalError, "Can't 'create', this object already exists" if ids_set? @dirty_data = raw_data post end def delete - raise NetboxClientRuby::LocalError, "Can't delete unless deletable=true" unless deletable + raise NetboxClientRuby::Error::LocalError, "Can't delete unless deletable=true" unless deletable return self if @deleted @data = response connection.delete path @@ -335,7 +335,8 @@ def replace_path_variables_in(path) path_variable_value = send(match) return interpreted_path.gsub! ":#{match}", path_variable_value.to_s unless path_variable_value.nil? - raise LocalError, "Received 'nil' while replacing ':#{match}' in '#{path}' with a value." + raise NetboxClientRuby::Error::LocalError, + "Received 'nil' while replacing ':#{match}' in '#{path}' with a value." end interpreted_path end @@ -350,7 +351,10 @@ def id_fields def extract_ids id_fields.each do |id_attr, id_field| - raise LocalError, "Can't find the id field '#{id_field}' in the received data." unless data.key?(id_field) + unless data.key?(id_field) + raise NetboxClientRuby::Error::LocalError, + "Can't find the id field '#{id_field}' in the received data." + end instance_variable_set(:"@#{id_attr}", data[id_field]) end diff --git a/lib/netbox_client_ruby/error.rb b/lib/netbox_client_ruby/error.rb index 1bd6754..fc8d4e8 100644 --- a/lib/netbox_client_ruby/error.rb +++ b/lib/netbox_client_ruby/error.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true module NetboxClientRuby - class Error < StandardError; end - class ClientError < Error; end - class LocalError < Error; end - class RemoteError < Error; end + class Error < StandardError + class LocalError < Error; end + class ClientError < Error; end + class RemoteError < Error; end + end end diff --git a/spec/netbox_client_ruby/api/secrets/session_key_spec.rb b/spec/netbox_client_ruby/api/secrets/session_key_spec.rb index 0b5e068..5425534 100644 --- a/spec/netbox_client_ruby/api/secrets/session_key_spec.rb +++ b/spec/netbox_client_ruby/api/secrets/session_key_spec.rb @@ -54,7 +54,7 @@ it 'does not send any request to the server' do expect(faraday).to_not receive(request_method) - expect { subject.session_key }.to raise_error(NetboxClientRuby::LocalError) + expect { subject.session_key }.to raise_error(NetboxClientRuby::Error::LocalError) end end @@ -64,7 +64,7 @@ it 'does not send any request to the server' do expect(faraday).to_not receive(request_method) - expect { subject.session_key }.to raise_error(NetboxClientRuby::LocalError) + expect { subject.session_key }.to raise_error(NetboxClientRuby::Error::LocalError) end end end @@ -75,7 +75,7 @@ it 'does not send any request to the server' do expect(faraday).to_not receive(request_method) - expect { subject.session_key }.to raise_error(NetboxClientRuby::LocalError) + expect { subject.session_key }.to raise_error(NetboxClientRuby::Error::LocalError) end end @@ -85,7 +85,7 @@ it 'does not send any request to the server' do expect(faraday).to_not receive(request_method) - expect { subject.session_key }.to raise_error(NetboxClientRuby::LocalError) + expect { subject.session_key }.to raise_error(NetboxClientRuby::Error::LocalError) end end diff --git a/spec/netbox_client_ruby/communication_spec.rb b/spec/netbox_client_ruby/communication_spec.rb index 029e81e..0968508 100644 --- a/spec/netbox_client_ruby/communication_spec.rb +++ b/spec/netbox_client_ruby/communication_spec.rb @@ -61,7 +61,7 @@ class Victim let(:response) { double('response', status: 400, body: nil) } it 'returns and empty object' do - expect { subject.response response }.to raise_error NetboxClientRuby::ClientError + expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError end end @@ -69,7 +69,7 @@ class Victim let(:response) { double('response', status: 401, body: nil) } it 'returns and empty object' do - expect { subject.response response }.to raise_error NetboxClientRuby::ClientError + expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError end end @@ -77,7 +77,7 @@ class Victim let(:response) { double('response', status: 403, body: nil) } it 'returns and empty object' do - expect { subject.response response }.to raise_error NetboxClientRuby::ClientError + expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError end end @@ -85,7 +85,7 @@ class Victim let(:response) { double('response', status: 405, body: nil) } it 'returns and empty object' do - expect { subject.response response }.to raise_error NetboxClientRuby::ClientError + expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError end end @@ -93,7 +93,7 @@ class Victim let(:response) { double('response', status: 415, body: nil) } it 'returns and empty object' do - expect { subject.response response }.to raise_error NetboxClientRuby::ClientError + expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError end end @@ -101,7 +101,7 @@ class Victim let(:response) { double('response', status: 429, body: nil) } it 'returns and empty object' do - expect { subject.response response }.to raise_error NetboxClientRuby::ClientError + expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError end end @@ -109,7 +109,7 @@ class Victim let(:response) { double('response', status: 499, body: nil) } it 'returns and empty object' do - expect { subject.response response }.to raise_error NetboxClientRuby::ClientError + expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError end end @@ -117,7 +117,7 @@ class Victim let(:response) { double('response', status: 500, body: nil) } it 'returns and empty object' do - expect { subject.response response }.to raise_error NetboxClientRuby::RemoteError + expect { subject.response response }.to raise_error NetboxClientRuby::Error::RemoteError end end @@ -125,7 +125,7 @@ class Victim let(:response) { double('response', status: 600, body: nil) } it 'returns and empty object' do - expect { subject.response response }.to raise_error NetboxClientRuby::RemoteError + expect { subject.response response }.to raise_error NetboxClientRuby::Error::RemoteError end end @@ -133,7 +133,7 @@ class Victim let(:response) { double('response', status: 400, body: 'you did it all wrong') } it 'returns and empty object' do - expect { subject.response response }.to raise_error NetboxClientRuby::ClientError + expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError end end end diff --git a/spec/netbox_client_ruby/entity_spec.rb b/spec/netbox_client_ruby/entity_spec.rb index 87ab121..e9d6efa 100644 --- a/spec/netbox_client_ruby/entity_spec.rb +++ b/spec/netbox_client_ruby/entity_spec.rb @@ -177,7 +177,7 @@ class TestEntity4 let(:request_params) { { 'name' => name } } it 'does raise an exception when trying to fetch data' do - expect { subject.reload }.to raise_error(NetboxClientRuby::LocalError) + expect { subject.reload }.to raise_error(NetboxClientRuby::Error::LocalError) end it 'returns itself when calling save' do @@ -321,7 +321,7 @@ class TestEntity4 let(:subject) { TestEntity2.new 42 } it 'raises an error' do - expect { subject.delete }.to raise_error NetboxClientRuby::LocalError + expect { subject.delete }.to raise_error NetboxClientRuby::Error::LocalError end end end @@ -354,7 +354,7 @@ class TestEntity4 let(:subject) { TestEntity2.new 42 } it 'raises an error' do - expect { subject.delete }.to raise_error NetboxClientRuby::LocalError + expect { subject.delete }.to raise_error NetboxClientRuby::Error::LocalError end end end