Skip to content

Commit 8c5d75a

Browse files
committed
Scope errors under NetboxClientRuby::Error namespace
1 parent b75f400 commit 8c5d75a

File tree

9 files changed

+41
-39
lines changed

9 files changed

+41
-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
@@ -57,15 +57,15 @@ def decode_private_key(encoded_private_key)
5757
return private_key if private_key.private?
5858
rescue OpenSSL::PKey::RSAError
5959
if rsa_private_key_password.empty?
60-
raise LocalError,
60+
raise NetboxClientRuby::Error::LocalError,
6161
"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'.)"
6262
else
63-
raise LocalError,
63+
raise NetboxClientRuby::Error::LocalError,
6464
"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'.)"
6565
end
6666
end
6767

68-
raise LocalError,
68+
raise NetboxClientRuby::Error::LocalError,
6969
"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'.)"
7070
end
7171
# rubocop:enable Layout/LineLength,Metrics/MethodLength
@@ -84,14 +84,14 @@ def read_private_key_file(key_file)
8484
encoded_private_key = key_file.read
8585
return encoded_private_key unless encoded_private_key.nil? || encoded_private_key.empty?
8686

87-
raise LocalError,
87+
raise NetboxClientRuby::Error::LocalError,
8888
"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'.)"
8989
end
9090

9191
def open_private_key_file
9292
return File.new rsa_private_key_path if File.exist? rsa_private_key_path
9393

94-
raise LocalError,
94+
raise NetboxClientRuby::Error::LocalError,
9595
"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'.)"
9696
end
9797
# rubocop:enable Layout/LineLength

lib/netbox_client_ruby/communication.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def raise_on_http_error(response) # rubocop:disable Metrics/MethodLength
3131
when 300..499
3232
raise_on_http_client_error response
3333
when 500..599
34-
raise NetboxClientRuby::RemoteError, "#{status} Remote Error#{formatted_body(body)}"
34+
raise NetboxClientRuby::Error::RemoteError, "#{status} Remote Error#{formatted_body(body)}"
3535
else
36-
raise NetboxClientRuby::RemoteError, "#{status} Unknown Error Code#{formatted_body(body)}"
36+
raise NetboxClientRuby::Error::RemoteError, "#{status} Unknown Error Code#{formatted_body(body)}"
3737
end
3838
end
3939

@@ -60,7 +60,7 @@ def raise_on_http_client_error(response) # rubocop:disable Metrics/MethodLength
6060
end
6161

6262
def raise_client_error(message, body = nil)
63-
raise NetboxClientRuby::ClientError, "#{message}#{formatted_body(body)}"
63+
raise NetboxClientRuby::Error::ClientError, "#{message}#{formatted_body(body)}"
6464
end
6565

6666
def formatted_body(body)

lib/netbox_client_ruby/entity.rb

Lines changed: 9 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
@@ -143,14 +143,14 @@ def save
143143
end
144144

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

148148
@dirty_data = raw_data
149149
post
150150
end
151151

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

156156
@data = response connection.delete path
@@ -327,7 +327,8 @@ def replace_path_variables_in(path)
327327
path_variable_value = send(match)
328328
return interpreted_path.gsub! ":#{match}", path_variable_value.to_s unless path_variable_value.nil?
329329

330-
raise LocalError, "Received 'nil' while replacing ':#{match}' in '#{path}' with a value."
330+
raise NetboxClientRuby::Error::LocalError,
331+
"Received 'nil' while replacing ':#{match}' in '#{path}' with a value."
331332
end
332333
interpreted_path
333334
end
@@ -342,7 +343,10 @@ def id_fields
342343

343344
def extract_ids
344345
id_fields.each do |id_attr, id_field|
345-
raise LocalError, "Can't find the id field '#{id_field}' in the received data." unless data.key?(id_field)
346+
unless data.key?(id_field)
347+
raise NetboxClientRuby::Error::LocalError,
348+
"Can't find the id field '#{id_field}' in the received data."
349+
end
346350

347351
instance_variable_set(:"@#{id_attr}", data[id_field])
348352
end

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
@@ -54,7 +54,7 @@
5454
it 'does not send any request to the server' do
5555
expect(faraday).to_not receive(request_method)
5656

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

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

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

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

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

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

spec/netbox_client_ruby/communication_spec.rb

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

spec/netbox_client_ruby/entity_spec.rb

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

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

183183
it 'returns itself when calling save' do
@@ -321,7 +321,7 @@ class TestEntity4
321321
let(:subject) { TestEntity2.new 42 }
322322

323323
it 'raises an error' do
324-
expect { subject.delete }.to raise_error NetboxClientRuby::LocalError
324+
expect { subject.delete }.to raise_error NetboxClientRuby::Error::LocalError
325325
end
326326
end
327327
end
@@ -354,7 +354,7 @@ class TestEntity4
354354
let(:subject) { TestEntity2.new 42 }
355355

356356
it 'raises an error' do
357-
expect { subject.delete }.to raise_error NetboxClientRuby::LocalError
357+
expect { subject.delete }.to raise_error NetboxClientRuby::Error::LocalError
358358
end
359359
end
360360
end

0 commit comments

Comments
 (0)