Skip to content

Commit fe3b6ab

Browse files
authored
Merge pull request #109 from jrxFive/107-bug-fix
Exception not requests.Response with BaseNomadException str dunder
2 parents e038c68 + b7e3c09 commit fe3b6ab

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

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: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import pytest
2-
import tests.common as common
3-
import nomad
41
import os
2+
3+
import mock
4+
import pytest
5+
import requests
56
import responses
67

8+
import nomad
9+
import tests.common as common
10+
711

812
def test_base_region_qs():
913
n = nomad.Nomad(host=common.IP, port=common.NOMAD_PORT, verify=False, token=common.NOMAD_TOKEN, region="random")
@@ -46,6 +50,40 @@ def test_base_delete_connection_error():
4650
j = n.job.deregister_job("example")
4751

4852

53+
@mock.patch("nomad.api.base.requests.Session")
54+
def test_base_raise_exception_not_requests_response_object(mock_requests):
55+
mock_requests().delete.side_effect = [requests.RequestException()]
56+
57+
with pytest.raises(nomad.api.exceptions.BaseNomadException) as excinfo:
58+
n = nomad.Nomad(
59+
host="162.16.10.102",
60+
port=common.NOMAD_PORT,
61+
timeout=0.001,
62+
verify=False
63+
)
64+
65+
_ = n.job.deregister_job("example")
66+
67+
# excinfo is a ExceptionInfo instance, which is a wrapper around the actual exception raised.
68+
# The main attributes of interest are .type, .value and .traceback.
69+
# https://docs.pytest.org/en/3.0.1/assert.html#assertions-about-expected-exceptions
70+
assert hasattr(excinfo.value.nomad_resp, "text") is False
71+
assert isinstance(excinfo.value.nomad_resp, requests.RequestException)
72+
assert "raised due" in str(excinfo)
73+
74+
75+
def test_base_raise_exception_is_requests_response_object(nomad_setup):
76+
with pytest.raises(nomad.api.exceptions.BaseNomadException) as excinfo:
77+
_ = nomad_setup.job.get_job("examplezz")
78+
79+
# excinfo is a ExceptionInfo instance, which is a wrapper around the actual exception raised.
80+
# The main attributes of interest are .type, .value and .traceback.
81+
# https://docs.pytest.org/en/3.0.1/assert.html#assertions-about-expected-exceptions
82+
assert hasattr(excinfo.value.nomad_resp, "text") is True
83+
assert isinstance(excinfo.value.nomad_resp, requests.Response)
84+
assert "raised with" in str(excinfo)
85+
86+
4987
@pytest.mark.skipif(tuple(int(i) for i in os.environ.get("NOMAD_VERSION").split(".")) < (0, 7, 0), reason="Nomad dispatch not supported")
5088
def test_base_get_connnection_not_authorized():
5189
n = nomad.Nomad(

0 commit comments

Comments
 (0)