Skip to content

Commit a67e4dc

Browse files
committed
25209: API returns incorrect error messages for 401 responses
1 parent 8aa377d commit a67e4dc

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

labkey/exceptions.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,41 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
from requests import exceptions
16+
from requests import exceptions, Response
1717

1818

1919
# base exception class for server responses
2020
class RequestError(exceptions.HTTPError):
2121
default_msg = 'Server Error'
2222

23-
def __init__(self, server_response=None):
24-
if server_response is not None:
23+
def __init__(self, server_response, **kwargs):
24+
"""
25+
:type server_response: Response
26+
"""
27+
super(RequestError, self).__init__(**kwargs)
28+
29+
# base class allows for kwargs 'request' and 'response'
30+
self.response = server_response
31+
self.server_exception = None
32+
33+
if self.response is not None:
34+
msg = self.default_msg
2535
try:
26-
decoded = server_response.json()
36+
decoded = self.response.json()
2737
if 'exception' in decoded:
2838
# use labkey server error message if available
2939
msg = decoded['exception']
3040
self.server_exception = decoded
31-
else:
32-
msg = self.default_msg
3341
except ValueError:
3442
# no valid json to decode
35-
raise ServerNotFoundError(server_response)
43+
pass
3644

37-
self.message = '{0}: {1}'.format(server_response.status_code, msg)
45+
self.message = '{0}: {1}'.format(self.response.status_code, msg)
46+
else:
47+
self.message = 'No response received'
3848

39-
self.response = server_response
49+
def __str__(self):
50+
return repr(self.message)
4051

4152

4253
class QueryNotFoundError(RequestError):
@@ -48,11 +59,7 @@ class RequestAuthorizationError(RequestError):
4859

4960

5061
class ServerNotFoundError(RequestError):
51-
SERVER_NOT_FOUND_MSG = 'Server resource not found. Please verify context path and project path are valid'
52-
53-
def __init__(self, server_response=None):
54-
self.message = '{0}: {1}'.format(server_response.status_code, self.SERVER_NOT_FOUND_MSG)
55-
self.response = server_response
62+
default_msg = 'Server resource not found. Please verify context path and project path are valid'
5663

5764

5865
class ServerContextError(exceptions.HTTPError):

0 commit comments

Comments
 (0)