Skip to content

Commit 4911699

Browse files
author
Thomas
committed
Improved handling exceptions.
1 parent f28e64b commit 4911699

File tree

7 files changed

+76
-74
lines changed

7 files changed

+76
-74
lines changed

netbox/circuits.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ def create_circuit(self, circuit_provider, cid, circuit_type, status_id, **kwarg
3434
try:
3535
provider_id = self.get_providers(name=circuit_provider)[0]['id']
3636
except IndexError:
37-
raise exceptions.NotFoundException('cirtcuit provider: {}'.format(circuit_provider)) from None
37+
raise exceptions.NotFoundException({"detail": "cirtcuit provider: {}".format(circuit_provider)}) from None
3838

3939
try:
4040
type_id = self.get_types(name=circuit_type)[0]['id']
4141
except IndexError:
42-
raise exceptions.NotFoundException('circuit type: {}'.format(circuit_type)) from None
42+
raise exceptions.NotFoundException({"detail": "circuit type: {}".format(circuit_type)}) from None
4343

4444
required_fields = {"provider": provider_id, "circuit": cid, "type": type_id,
4545
"status": status_id}
@@ -55,7 +55,7 @@ def delete_circuit(self, cid, provider):
5555
try:
5656
circuits_id = self.get_circuits(cid=cid, provider=provider)[0]['id']
5757
except IndexError:
58-
raise exceptions.NotFoundException('Circuit with circuit: {} and provider: {}'.format(cid, provider)) from None
58+
raise exceptions.NotFoundException({"detail": "Circuit with circuit: {} and provider: {}".format(cid, provider)}) from None
5959
return self.netbox_con.delete('/circuits/circuits/', circuits_id)
6060

6161
def update_circuit(self, cid, provider, **kwargs):
@@ -69,7 +69,7 @@ def update_circuit(self, cid, provider, **kwargs):
6969
try:
7070
circuits_id = self.get_circuits(cid=cid, provider=provider)[0]['id']
7171
except IndexError:
72-
raise exceptions.NotFoundException('Circuit with circuit: {} and provider {}'.format(cid, provider)) from None
72+
raise exceptions.NotFoundException({"detail": "Circuit with circuit: {} and provider {}".format(cid, provider)}) from None
7373
return self.netbox_con.patch('/circuits/circuits/', circuits_id, **kwargs)
7474

7575
def get_providers(self, **kwargs):
@@ -95,7 +95,7 @@ def delete_provider(self, provider_name):
9595
try:
9696
circuits_provider_id = self.get_providers(name=provider_name)[0]['id']
9797
except IndexError:
98-
raise exceptions.NotFoundException('circuit provider: {}'.format(provider_name)) from None
98+
raise exceptions.NotFoundException({"detail": "circuit provider: {}".format(provider_name)}) from None
9999
return self.netbox_con.delete('/circuits/providers/', circuits_provider_id)
100100

101101
def update_provider(self, provider_name, **kwargs):
@@ -108,7 +108,7 @@ def update_provider(self, provider_name, **kwargs):
108108
try:
109109
circuits_provider_id = self.get_providers(name=provider_name)[0]['id']
110110
except IndexError:
111-
raise exceptions.NotFoundException('circuit provider: {}'.format(provider_name)) from None
111+
raise exceptions.NotFoundException({"detail": "circuit provider: {}".format(provider_name)}) from None
112112
return self.netbox_con.patch('/circuits/providers/', circuits_provider_id, **kwargs)
113113

114114
def get_types(self, **kwargs):
@@ -134,7 +134,7 @@ def delete_type(self, type_name):
134134
try:
135135
circuits_type_id = self.get_types(name=type_name)[0]['id']
136136
except IndexError:
137-
raise exceptions.NotFoundException('circuit type: {}'.format(type_name)) from None
137+
raise exceptions.NotFoundException({"detail": "circuit type: {}".format(type_name)}) from None
138138
return self.netbox_con.delete('/circuits/circuit-types/', circuits_type_id)
139139

140140
def update_type(self, circuit_type_name, **kwargs):
@@ -147,7 +147,7 @@ def update_type(self, circuit_type_name, **kwargs):
147147
try:
148148
type_id = self.get_types(name=circuit_type_name)[0]['id']
149149
except IndexError:
150-
raise exceptions.NotFoundException('circuit type: {}'.format(circuit_type_name)) from None
150+
raise exceptions.NotFoundException({"detail": "circuit type: {}".format(circuit_type_name)}) from None
151151
return self.netbox_con.patch('/circuits/circuit-types/', type_id, **kwargs)
152152

153153
def get_terminations(self, **kwargs):
@@ -166,7 +166,7 @@ def create_termination(self, circuit, term_side, site, port_speed, **kwargs):
166166
try:
167167
site_id = self.dcim.get_sites(name=site)[0]['id']
168168
except IndexError:
169-
raise exceptions.NotFoundException('site: {}'.format(site)) from None
169+
raise exceptions.NotFoundException({"detail": "site: {}".format(site)}) from None
170170

171171
required_fields = {"circuit": circuit, "term_side": term_side, "site": site_id, "port_speed": port_speed}
172172
return self.netbox_con.post('/circuits/circuit-terminations/', required_fields, **kwargs)
@@ -183,13 +183,13 @@ def delete_termination(self, circuit, term_side, site, port_speed):
183183
try:
184184
site_id = self.dcim.get_sites(name=site)[0]['id']
185185
except IndexError:
186-
raise exceptions.NotFoundException('site: {}'.format(site)) from None
186+
raise exceptions.NotFoundException({"detail": "site: {}".format(site)}) from None
187187

188188
try:
189189
circuit_termination = self.get_terminations(circuit_id=circuit, term_side=term_side, site_id=site_id,
190190
port_speed=port_speed)[0]['id']
191191
except IndexError:
192-
raise exceptions.NotFoundException('circuit termination with given arguments') from None
192+
raise exceptions.NotFoundException({"detail": "circuit termination with given arguments"}) from None
193193

194194
return self.netbox_con.delete('/circuits/circuit-terminations/', circuit_termination)
195195

@@ -206,12 +206,12 @@ def update_termination(self, circuit, term_side, site, port_speed, **kwargs):
206206
try:
207207
site_id = self.dcim.get_sites(name=site)[0]['id']
208208
except IndexError:
209-
raise exceptions.NotFoundException('site: {}'.format(site)) from None
209+
raise exceptions.NotFoundException({"detail": "site: {}".format(site)}) from None
210210

211211
try:
212212
circuit_termination_id = self.get_terminations(circuit_id=circuit, term_side=term_side, site_id=site_id,
213213
port_speed=port_speed)[0]['id']
214214
except IndexError:
215-
raise exceptions.NotFoundException('circuit termination with given arguments') from None
215+
raise exceptions.NotFoundException({"detail" "circuit termination with given arguments"}) from None
216216

217217
return self.netbox_con.patch('/circuits/circuit-terminations/', circuit_termination_id, **kwargs)

netbox/dcim.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,19 @@ def create_device(self, name, device_role, site_name, device_type, **kwargs):
281281
device_role_id = self.get_device_roles(name=device_role)[0]['id']
282282
required_fields.update({"device_role": device_role_id})
283283
except IndexError:
284-
raise exceptions.NotFoundException('device-role: {}'.format(device_role)) from None
284+
raise exceptions.NotFoundException({"detail": "device-role {}".format(device_role)}) from None
285285

286286
try:
287287
site_id = self.get_sites(name=site_name)[0]['id']
288288
required_fields.update({"site": site_id})
289289
except IndexError:
290-
raise exceptions.NotFoundException('site: {}'.format(site_name)) from None
290+
raise exceptions.NotFoundException({"detail": "site: {}".format(site_name)}) from None
291291

292292
try:
293293
device_type_id = self.get_device_types(model=device_type)[0]['id']
294294
required_fields.update({"device_type": device_type_id})
295295
except IndexError:
296-
raise exceptions.NotFoundException('device-type: {}'.format(device_type)) from None
296+
raise exceptions.NotFoundException({"detail": "device-type: {}".format(device_type)}) from None
297297

298298
return self.netbox_con.post('/dcim/devices/', required_fields, **kwargs)
299299

@@ -306,7 +306,7 @@ def delete_device(self, device_name):
306306
try:
307307
device_id = self.get_devices(name=device_name)[0]['id']
308308
except IndexError:
309-
raise exceptions.NotFoundException('device: {}'.format(device_name)) from None
309+
raise exceptions.NotFoundException({"detail": "device: {}".format(device_name)}) from None
310310
return self.netbox_con.delete('/dcim/devices/', device_id)
311311

312312
def delete_device_by_id(self, device_id):
@@ -362,7 +362,7 @@ def update_device_type(self, device_type, **kwargs):
362362
try:
363363
device_type_id = self.get_device_types(model=device_type)[0]['id']
364364
except IndexError:
365-
raise exceptions.NotFoundException('device-type: {}'.format(device_type)) from None
365+
raise exceptions.NotFoundException({"detail": "device-type: {}".format(device_type)}) from None
366366
return self.netbox_con.patch('/dcim/device-types/', device_type_id, **kwargs)
367367

368368
def update_device_type_by_id(self, device_type_id, **kwargs):
@@ -383,7 +383,7 @@ def delete_device_type(self, model_name):
383383
try:
384384
device_type_id = self.get_device_types(model=model_name)[0]['id']
385385
except IndexError:
386-
raise exceptions.NotFoundException('device-type: {}'.format(model_name)) from None
386+
raise exceptions.NotFoundException({"detail": "device-type: {}".format(model_name)}) from None
387387
return self.netbox_con.delete('/dcim/device-types/', device_type_id)
388388

389389
def delete_device_type_by_id(self, device_type_id):
@@ -420,7 +420,7 @@ def update_device_role(self, device_role, **kwargs):
420420
try:
421421
device_role_id = self.get_device_roles(name=device_role)[0]['id']
422422
except IndexError:
423-
raise exceptions.NotFoundException('device-role: {}'.format(device_role)) from None
423+
raise exceptions.NotFoundException({"detail": "device-role: {}".format(device_role)}) from None
424424
return self.netbox_con.patch('/dcim/device-roles/', device_role_id, **kwargs)
425425

426426
def update_device_role_by_id(self, device_role_id, **kwargs):
@@ -441,7 +441,7 @@ def delete_device_role(self, device_role):
441441
try:
442442
device_role_id = self.get_device_roles(name=device_role)[0]['id']
443443
except IndexError:
444-
raise exceptions.NotFoundException('device-role: {}'.format(device_role)) from None
444+
raise exceptions.NotFoundException({"detail": "device-role: {}".format(device_role)}) from None
445445
return self.netbox_con.delete('/dcim/device-roles/', device_role_id)
446446

447447
def delete_device_role_by_id(self, device_role_id):
@@ -477,7 +477,7 @@ def update_manufacturer(self, manufacturer_name, **kwargs):
477477
try:
478478
manufacturer_id = self.get_manufacturers(name=manufacturer_name)[0]['id']
479479
except IndexError:
480-
raise exceptions.NotFoundException('manufacturer: {}'.format(manufacturer_name)) from None
480+
raise exceptions.NotFoundException({"detail": "manufacturer: {}".format(manufacturer_name)}) from None
481481
return self.netbox_con.patch('/dcim/manufacturer/', manufacturer_id, **kwargs)
482482

483483
def update_manufacturer_by_id(self, manufacturer_id, **kwargs):
@@ -498,7 +498,7 @@ def delete_manufacturer(self, manufacturer_name):
498498
try:
499499
manufacturer_id = self.get_manufacturers(name=manufacturer_name)[0]['id']
500500
except IndexError:
501-
raise exceptions.NotFoundException('manufacturer: {}'.format(manufacturer_name)) from None
501+
raise exceptions.NotFoundException({"detail": "manufacturer: {}".format(manufacturer_name)}) from None
502502
return self.netbox_con.delete('/dcim/manufacturers/', manufacturer_id)
503503

504504
def delete_manufacturer_id(self, manufacturer_id):
@@ -534,7 +534,7 @@ def update_platform(self, platform_name, **kwargs):
534534
try:
535535
platform_id = self.get_platforms(name=platform_name)[0]['id']
536536
except IndexError:
537-
raise exceptions.NotFoundException('platform: {}'.format(platform_name)) from None
537+
raise exceptions.NotFoundException({"detail": "platform: {}".format(platform_name)}) from None
538538
return self.netbox_con.patch('/dcim/platforms/', platform_id, **kwargs)
539539

540540
def update_platform_by_id(self, platform_id, **kwargs):
@@ -555,7 +555,7 @@ def delete_platform(self, platform_name):
555555
try:
556556
platform_id = self.get_platforms(name=platform_name)[0]['id']
557557
except IndexError:
558-
raise exceptions.NotFoundException('platform: {}'.format(platform_name)) from None
558+
raise exceptions.NotFoundException({"detail": "platform: {}".format(platform_name)}) from None
559559
return self.netbox_con.delete('/dcim/platforms/', platform_id)
560560

561561
def delete_platform_by_id(self, platform_id):
@@ -593,7 +593,7 @@ def update_interface(self, interface, device, **kwargs):
593593
try:
594594
interface_id = self.get_interfaces(name=interface, device=device)[0]['id']
595595
except IndexError:
596-
raise exceptions.NotFoundException('interface: {}'.format(interface)) from None
596+
raise exceptions.NotFoundException({"detail": "interface: {}".format(interface)}) from None
597597
return self.netbox_con.patch('/dcim/interfaces/', interface_id, **kwargs)
598598

599599
def update_interface_by_id(self, interface_id, **kwargs):
@@ -615,7 +615,7 @@ def delete_interface(self, interface_name, device):
615615
try:
616616
interface_id = self.get_interfaces(name=interface_name, device=device)[0]['id']
617617
except IndexError:
618-
raise exceptions.NotFoundException('interface: {}'.format(interface_name)) from None
618+
raise exceptions.NotFoundException({"detail": "interface: {}".format(interface_name)}) from None
619619
return self.netbox_con.delete('/dcim/interfaces/', interface_id)
620620

621621
def delete_interface_by_id(self, interface_id):
@@ -677,7 +677,7 @@ def create_interface_template(self, name, device_type, **kwargs):
677677
try:
678678
device_type_id = self.get_device_types(model=device_type)[0]['id']
679679
except IndexError:
680-
raise exceptions.NotFoundException('device-type: {}'.format(device_type)) from None
680+
raise exceptions.NotFoundException({"detail": "device-type: {}".format(device_type)}) from None
681681
required_fields = {"name": name, "device_type": device_type_id}
682682
return self.netbox_con.post('/dcim/interface-templates/', required_fields, **kwargs)
683683

@@ -691,7 +691,7 @@ def update_interface_template(self, interface_template_name, **kwargs):
691691
try:
692692
interface_template_id = self.get_interface_templates(name=interface_template_name)[0]['id']
693693
except IndexError:
694-
raise exceptions.NotFoundException('interface: {}'.format(interface_template_name)) from None
694+
raise exceptions.NotFoundException({"detail": "interface: {}".format(interface_template_name)}) from None
695695
return self.netbox_con.patch('/dcim/interface-templates/', interface_template_id, **kwargs)
696696

697697
def update_interface_template_by_id(self, interface_template_id, **kwargs):
@@ -712,7 +712,7 @@ def delete_interface_template(self, interface_template_name):
712712
try:
713713
interface_template_id = self.get_interface_templates(name=interface_template_name)[0]['id']
714714
except IndexError:
715-
raise exceptions.NotFoundException('interface-template: {}'.format(interface_template_name)) from None
715+
raise exceptions.NotFoundException({"detail": "interface-template: {}".format(interface_template_name)}) from None
716716
return self.netbox_con.delete('/dcim/interface-templates/', interface_template_id)
717717

718718
def delete_interface_template_by_id(self, interface_template_id):
@@ -738,7 +738,7 @@ def create_inventory_item(self, name, device_name, **kwargs):
738738
try:
739739
device_id = self.get_devices(name=device_name)[0]['id']
740740
except IndexError:
741-
raise exceptions.NotFoundException('device: {}'.format(device_name)) from None
741+
raise exceptions.NotFoundException({"detail": "device: {}".format(device_name)}) from None
742742
required_fields = {"name": name, "device": device_id}
743743
return self.netbox_con.post('/dcim/inventory-items/', required_fields, **kwargs)
744744

@@ -753,7 +753,7 @@ def update_inventory_item(self, name, device_name, **kwargs):
753753
try:
754754
inventory_item_id = self.get_inventory_items(name=name, device=device_name)[0]['id']
755755
except IndexError:
756-
raise exceptions.NotFoundException('inventory item: {}'.format(name)) from None
756+
raise exceptions.NotFoundException({"detail": "inventory item: {}".format(name)}) from None
757757
return self.netbox_con.patch('/dcim/inventory-items/', inventory_item_id, **kwargs)
758758

759759
def update_inventory_item_by_id(self, inventory_item_id, **kwargs):
@@ -775,7 +775,7 @@ def delete_inventory_item(self, name, device_name):
775775
try:
776776
inventory_item_id = self.get_inventory_items(name=name, device=device_name)[0]['id']
777777
except IndexError:
778-
raise exceptions.NotFoundException('inventory item: {}'.format(name)) from None
778+
raise exceptions.NotFoundException({"detail": "inventory item: {}".format(name)}) from None
779779
return self.netbox_con.delete('/dcim/inventory-items/', inventory_item_id)
780780

781781
def delete_inventory_item_by_id(self, inventory_item_id):

netbox/exceptions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class GeneralException(BaseException):
22
def __init__(self, resp_data):
3+
print(resp_data)
34
if isinstance(resp_data, dict):
45
if 'detail' in resp_data:
56
self.err = resp_data['detail']
67
else:
7-
self.err = ''.join('{} '.format(val[0]) for key, val in resp_data.items())
8+
self.err = ''.join('{} '.format(val) for key, val in resp_data.items())
89
else:
910
self.err = 'Unknown Error, please check the Netbox logs'
1011

@@ -20,7 +21,8 @@ def __init__(self, resp_data):
2021

2122
class NotFoundException(GeneralException):
2223
"""Raised when item is not found"""
23-
pass
24+
def __init__(self, resp_data):
25+
super().__init__(resp_data)
2426

2527

2628
class GetException(GeneralException):

netbox/extras.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def delete_config_context(self, name):
3030
try:
3131
config_context_id = self.get_config_contexts(name=name)[0]['id']
3232
except IndexError:
33-
raise exceptions.NotFoundException('config-context: {}'.format(name)) from None
33+
raise exceptions.NotFoundException({"detail": "config-context: {}".format(name)}) from None
3434
return self.netbox_con.delete('/extras/config-contexts/', config_context_id)
3535

3636
def delete_config_context_by_id(self, config_context_id):
@@ -51,7 +51,7 @@ def update_config_context(self, name, **kwargs):
5151
try:
5252
config_context_id = self.get_config_contexts(name=name)[0]['id']
5353
except IndexError:
54-
raise exceptions.NotFoundException('config-context: {}'.format(name)) from None
54+
raise exceptions.NotFoundException({"detail": "config-context: {}".format(name)}) from None
5555

5656
return self.netbox_con.patch('/extras/config-contexts/', config_context_id, **kwargs)
5757

@@ -88,7 +88,7 @@ def delete_tag(self, name):
8888
try:
8989
tag_id = self.get_tags(name=name)[0]['id']
9090
except IndexError:
91-
raise exceptions.NotFoundException('tag: {}'.format(name)) from None
91+
raise exceptions.NotFoundException({"detail": "tag: {}".format(name)}) from None
9292
return self.netbox_con.delete('/extras/tags/', tag_id)
9393

9494
def delete_tag_by_id(self, tag_id):
@@ -109,7 +109,7 @@ def update_tag(self, name, **kwargs):
109109
try:
110110
tag_id = self.get_tags(name=name)[0]['id']
111111
except IndexError:
112-
raise exceptions.NotFoundException('tag: {}'.format(name)) from None
112+
raise exceptions.NotFoundException({"detail": "tag: {}".format(name)}) from None
113113

114114
return self.netbox_con.patch('/extras/tags/', tag_id, **kwargs)
115115

0 commit comments

Comments
 (0)