Skip to content

Commit 3b95c32

Browse files
committed
Scope errors under NetboxClientRuby::Error namespace
1 parent d1287ee commit 3b95c32

File tree

9 files changed

+37
-39
lines changed

9 files changed

+37
-39
lines changed

lib/netbox_client_ruby.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
loader.setup
3737
end
3838

39-
# load gem errors
40-
require_relative 'netbox_client_ruby/error'
41-
4239
module NetboxClientRuby
4340
extend Dry::Configurable
4441

lib/netbox_client_ruby/api/secrets/rsa_key_pair.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get
2525
if authorization_token
2626
@response ||= response connection.get(PATH)
2727
else
28-
raise LocalError,
28+
raise NetboxClientRuby::Error::LocalError,
2929
"The authorization_token has not been configured, but it's required for get-session-key."
3030
end
3131
end

lib/netbox_client_ruby/api/secrets/session_key.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def request
2525
if authorization_token
2626
response(post)
2727
else
28-
raise LocalError,
28+
raise NetboxClientRuby::Error::LocalError,
2929
"The authorization_token has not been configured, but it's required for get-session-key."
3030
end
3131
end
@@ -56,15 +56,15 @@ def decode_private_key(encoded_private_key)
5656
return private_key if private_key.private?
5757
rescue OpenSSL::PKey::RSAError
5858
if rsa_private_key_password.empty?
59-
raise LocalError,
59+
raise NetboxClientRuby::Error::LocalError,
6060
"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'.)"
6161
else
62-
raise LocalError,
62+
raise NetboxClientRuby::Error::LocalError,
6363
"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'.)"
6464
end
6565
end
6666

67-
raise LocalError,
67+
raise NetboxClientRuby::Error::LocalError,
6868
"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'.)"
6969
end
7070

@@ -80,14 +80,14 @@ def read_private_key_file(key_file)
8080
encoded_private_key = key_file.read
8181
return encoded_private_key unless encoded_private_key.nil? || encoded_private_key.empty?
8282

83-
raise LocalError,
83+
raise NetboxClientRuby::Error::LocalError,
8484
"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'.)"
8585
end
8686

8787
def open_private_key_file
8888
return File.new rsa_private_key_path if File.exist? rsa_private_key_path
8989

90-
raise LocalError,
90+
raise NetboxClientRuby::Error::LocalError,
9191
"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'.)"
9292
end
9393

lib/netbox_client_ruby/communication.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def raise_on_http_error(response)
3030
when 300..499 then
3131
raise_on_http_client_error response
3232
when 500..599 then
33-
raise NetboxClientRuby::RemoteError, "#{status} Remote Error#{formatted_body(body)}"
33+
raise NetboxClientRuby::Error::RemoteError, "#{status} Remote Error#{formatted_body(body)}"
3434
else
35-
raise NetboxClientRuby::RemoteError, "#{status} Unknown Error Code#{formatted_body(body)}"
35+
raise NetboxClientRuby::Error::RemoteError, "#{status} Unknown Error Code#{formatted_body(body)}"
3636
end
3737
end
3838

@@ -59,7 +59,7 @@ def raise_on_http_client_error(response)
5959
end
6060

6161
def raise_client_error(message, body = nil)
62-
raise NetboxClientRuby::ClientError, "#{message}#{formatted_body(body)}"
62+
raise NetboxClientRuby::Error::ClientError, "#{message}#{formatted_body(body)}"
6363
end
6464

6565
def formatted_body(body)

lib/netbox_client_ruby/entity.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def revert
129129
end
130130

131131
def reload
132-
raise LocalError, "Can't 'reload', this object has never been saved" unless ids_set?
132+
raise NetboxClientRuby::Error::LocalError, "Can't 'reload', this object has never been saved" unless ids_set?
133133

134134
@data = get
135135
revert
@@ -142,14 +142,14 @@ def save
142142
end
143143

144144
def create(raw_data)
145-
raise LocalError, "Can't 'create', this object already exists" if ids_set?
145+
raise NetboxClientRuby::Error::LocalError, "Can't 'create', this object already exists" if ids_set?
146146

147147
@dirty_data = raw_data
148148
post
149149
end
150150

151151
def delete
152-
raise NetboxClientRuby::LocalError, "Can't delete unless deletable=true" unless deletable
152+
raise NetboxClientRuby::Error::LocalError, "Can't delete unless deletable=true" unless deletable
153153
return self if @deleted
154154

155155
@data = response connection.delete path
@@ -325,7 +325,7 @@ def replace_path_variables_in(path)
325325
path.scan(/:([a-zA-Z_][a-zA-Z0-9_]+[!?=]?)/) do |match, *|
326326
path_variable_value = send(match)
327327
return interpreted_path.gsub! ":#{match}", path_variable_value.to_s unless path_variable_value.nil?
328-
raise LocalError, "Received 'nil' while replacing ':#{match}' in '#{path}' with a value."
328+
raise NetboxClientRuby::Error::LocalError, "Received 'nil' while replacing ':#{match}' in '#{path}' with a value."
329329
end
330330
interpreted_path
331331
end
@@ -341,7 +341,7 @@ def id_fields
341341
def extract_ids
342342
id_fields.each do |id_attr, id_field|
343343
unless data.key?(id_field)
344-
raise LocalError, "Can't find the id field '#{id_field}' in the received data."
344+
raise NetboxClientRuby::Error::LocalError, "Can't find the id field '#{id_field}' in the received data."
345345
end
346346

347347
instance_variable_set(:"@#{id_attr}", data[id_field])

lib/netbox_client_ruby/error.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# frozen_string_literal: true
22

33
module NetboxClientRuby
4-
class Error < StandardError; end
5-
class ClientError < Error; end
6-
class LocalError < Error; end
7-
class RemoteError < Error; end
4+
class Error < StandardError
5+
class LocalError < Error; end
6+
class ClientError < Error; end
7+
class RemoteError < Error; end
8+
end
89
end

spec/netbox_client_ruby/api/secrets/session_key_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
it 'does not send any request to the server' do
5454
expect(faraday).to_not receive(request_method)
5555

56-
expect { subject.session_key }.to raise_error(NetboxClientRuby::LocalError)
56+
expect { subject.session_key }.to raise_error(NetboxClientRuby::Error::LocalError)
5757
end
5858
end
5959

@@ -63,7 +63,7 @@
6363
it 'does not send any request to the server' do
6464
expect(faraday).to_not receive(request_method)
6565

66-
expect { subject.session_key }.to raise_error(NetboxClientRuby::LocalError)
66+
expect { subject.session_key }.to raise_error(NetboxClientRuby::Error::LocalError)
6767
end
6868
end
6969
end
@@ -74,7 +74,7 @@
7474
it 'does not send any request to the server' do
7575
expect(faraday).to_not receive(request_method)
7676

77-
expect { subject.session_key }.to raise_error(NetboxClientRuby::LocalError)
77+
expect { subject.session_key }.to raise_error(NetboxClientRuby::Error::LocalError)
7878
end
7979
end
8080

@@ -84,7 +84,7 @@
8484
it 'does not send any request to the server' do
8585
expect(faraday).to_not receive(request_method)
8686

87-
expect { subject.session_key }.to raise_error(NetboxClientRuby::LocalError)
87+
expect { subject.session_key }.to raise_error(NetboxClientRuby::Error::LocalError)
8888
end
8989
end
9090

spec/netbox_client_ruby/communication_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,79 +60,79 @@ class Victim
6060
let(:response) { double('response', status: 400, body: nil) }
6161

6262
it 'returns and empty object' do
63-
expect { subject.response response }.to raise_error NetboxClientRuby::ClientError
63+
expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError
6464
end
6565
end
6666

6767
context '401 Unauthorized' do
6868
let(:response) { double('response', status: 401, body: nil) }
6969

7070
it 'returns and empty object' do
71-
expect { subject.response response }.to raise_error NetboxClientRuby::ClientError
71+
expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError
7272
end
7373
end
7474

7575
context '403 Forbidden' do
7676
let(:response) { double('response', status: 403, body: nil) }
7777

7878
it 'returns and empty object' do
79-
expect { subject.response response }.to raise_error NetboxClientRuby::ClientError
79+
expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError
8080
end
8181
end
8282

8383
context '405 Method Not Allowed' do
8484
let(:response) { double('response', status: 405, body: nil) }
8585

8686
it 'returns and empty object' do
87-
expect { subject.response response }.to raise_error NetboxClientRuby::ClientError
87+
expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError
8888
end
8989
end
9090

9191
context '415 Unsupported Media Type' do
9292
let(:response) { double('response', status: 415, body: nil) }
9393

9494
it 'returns and empty object' do
95-
expect { subject.response response }.to raise_error NetboxClientRuby::ClientError
95+
expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError
9696
end
9797
end
9898

9999
context '429 Too many requests' do
100100
let(:response) { double('response', status: 429, body: nil) }
101101

102102
it 'returns and empty object' do
103-
expect { subject.response response }.to raise_error NetboxClientRuby::ClientError
103+
expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError
104104
end
105105
end
106106

107107
context '499 Random' do
108108
let(:response) { double('response', status: 499, body: nil) }
109109

110110
it 'returns and empty object' do
111-
expect { subject.response response }.to raise_error NetboxClientRuby::ClientError
111+
expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError
112112
end
113113
end
114114

115115
context '500 Internal Server Error' do
116116
let(:response) { double('response', status: 500, body: nil) }
117117

118118
it 'returns and empty object' do
119-
expect { subject.response response }.to raise_error NetboxClientRuby::RemoteError
119+
expect { subject.response response }.to raise_error NetboxClientRuby::Error::RemoteError
120120
end
121121
end
122122

123123
context '600 Undefined Error' do
124124
let(:response) { double('response', status: 600, body: nil) }
125125

126126
it 'returns and empty object' do
127-
expect { subject.response response }.to raise_error NetboxClientRuby::RemoteError
127+
expect { subject.response response }.to raise_error NetboxClientRuby::Error::RemoteError
128128
end
129129
end
130130

131131
context '400 Bad Request with body' do
132132
let(:response) { double('response', status: 400, body: 'you did it all wrong') }
133133

134134
it 'returns and empty object' do
135-
expect { subject.response response }.to raise_error NetboxClientRuby::ClientError
135+
expect { subject.response response }.to raise_error NetboxClientRuby::Error::ClientError
136136
end
137137
end
138138
end

spec/netbox_client_ruby/entity_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class TestEntity4
174174
let(:request_params) { { 'name' => name } }
175175

176176
it 'does raise an exception when trying to fetch data' do
177-
expect { subject.reload }.to raise_error(NetboxClientRuby::LocalError)
177+
expect { subject.reload }.to raise_error(NetboxClientRuby::Error::LocalError)
178178
end
179179

180180
it 'returns itself when calling save' do
@@ -317,7 +317,7 @@ class TestEntity4
317317
context 'non-deletable entity' do
318318
let(:subject) { TestEntity2.new 42 }
319319
it 'raises an error' do
320-
expect { subject.delete }.to raise_error NetboxClientRuby::LocalError
320+
expect { subject.delete }.to raise_error NetboxClientRuby::Error::LocalError
321321
end
322322
end
323323
end
@@ -349,7 +349,7 @@ class TestEntity4
349349
context 'non-deletable entity' do
350350
let(:subject) { TestEntity2.new 42 }
351351
it 'raises an error' do
352-
expect { subject.delete }.to raise_error NetboxClientRuby::LocalError
352+
expect { subject.delete }.to raise_error NetboxClientRuby::Error::LocalError
353353
end
354354
end
355355
end

0 commit comments

Comments
 (0)