|
| 1 | +# |
| 2 | +# Copyright (c) 2015 LabKey Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +from requests import exceptions |
| 17 | + |
| 18 | + |
| 19 | +# base exception class for server responses |
| 20 | +class RequestError(exceptions.HTTPError): |
| 21 | + default_msg = 'Server Error' |
| 22 | + |
| 23 | + def __init__(self, server_response=None): |
| 24 | + if server_response is not None: |
| 25 | + try: |
| 26 | + decoded = server_response.json() |
| 27 | + if 'exception' in decoded: |
| 28 | + # use labkey server error message if available |
| 29 | + msg = decoded['exception'] |
| 30 | + self.server_exception = decoded |
| 31 | + else: |
| 32 | + msg = self.default_msg |
| 33 | + except ValueError: |
| 34 | + # no valid json to decode |
| 35 | + raise ServerNotFoundError(server_response) |
| 36 | + |
| 37 | + self.message = '{0}: {1}'.format(server_response.status_code, msg) |
| 38 | + |
| 39 | + self.response = server_response |
| 40 | + |
| 41 | + |
| 42 | +class QueryNotFoundError(RequestError): |
| 43 | + default_msg = 'Query Resource Not Found' |
| 44 | + |
| 45 | + |
| 46 | +class RequestAuthorizationError(RequestError): |
| 47 | + default_msg = 'Authorization Failed' |
| 48 | + |
| 49 | + |
| 50 | +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 |
| 56 | + |
| 57 | + |
| 58 | +class ServerContextError(exceptions.HTTPError): |
| 59 | + def __init__(self, inner_exception=None): |
| 60 | + self.message = self._get_message(inner_exception) |
| 61 | + self.exception = inner_exception |
| 62 | + |
| 63 | + def _get_message(self, e): |
| 64 | + switcher = { |
| 65 | + exceptions.SSLError: |
| 66 | + 'Failed to match server SSL configuration. Ensure the server_context is configured correctly.' |
| 67 | + } |
| 68 | + return switcher.get(type(e), 'Please verify server_context is configured correctly') |
0 commit comments