Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff6e8dc

Browse files
add safety check to verify that nomad_resp is indeed an requests.Response object and has text attribute. If not stringify given exception/value.
1 parent e038c68 commit ff6e8dc

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

nomad/api/exceptions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import requests
2+
3+
14
class BaseNomadException(Exception):
25
"""General Error occurred when interacting with nomad API"""
36
def __init__(self, nomad_resp):
47
self.nomad_resp = nomad_resp
58

69
def __str__(self):
7-
return 'The {0} was raised with following response: {1}.'.format(self.__class__.__name__, self.nomad_resp.text)
10+
if isinstance(self.nomad_resp, requests.Response) and hasattr(self.nomad_resp, "text"):
11+
return 'The {0} was raised with following response: {1}.'.format(self.__class__.__name__, self.nomad_resp.text)
12+
else:
13+
return 'The {0} was raised due to the following error: {1}'.format(self.__class__.__name__, str(self.nomad_resp))
814

915

1016
class URLNotFoundNomadException(BaseNomadException):

tests/test_base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def test_base_delete_connection_error():
4646
j = n.job.deregister_job("example")
4747

4848

49+
def test_base_raise_exception_():
50+
n = nomad.Nomad(
51+
host="162.16.10.102", port=common.NOMAD_PORT, timeout=0.001, verify=False)
52+
with pytest.raises(nomad.api.exceptions.BaseNomadException):
53+
j = n.job.deregister_job("example")
54+
55+
4956
@pytest.mark.skipif(tuple(int(i) for i in os.environ.get("NOMAD_VERSION").split(".")) < (0, 7, 0), reason="Nomad dispatch not supported")
5057
def test_base_get_connnection_not_authorized():
5158
n = nomad.Nomad(

0 commit comments

Comments
 (0)