Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/netbox_client_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
loader.setup
end

# load gem errors
require_relative 'netbox_client_ruby/error'

module NetboxClientRuby
extend Dry::Configurable

Expand Down
2 changes: 1 addition & 1 deletion lib/netbox_client_ruby/api/secrets/rsa_key_pair.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/netbox_client_ruby/api/secrets/session_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/netbox_client_ruby/communication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions lib/netbox_client_ruby/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lib/netbox_client_ruby/error.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions spec/netbox_client_ruby/api/secrets/session_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down
20 changes: 10 additions & 10 deletions spec/netbox_client_ruby/communication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,79 +61,79 @@ 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

context '401 Unauthorized' do
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

context '403 Forbidden' do
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

context '405 Method Not Allowed' do
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

context '415 Unsupported Media Type' do
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

context '429 Too many requests' do
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

context '499 Random' do
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

context '500 Internal Server Error' do
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

context '600 Undefined Error' do
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

context '400 Bad Request with body' do
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
Expand Down
6 changes: 3 additions & 3 deletions spec/netbox_client_ruby/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down