13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
#
16
- from requests import exceptions
16
+ from requests import exceptions , Response
17
17
18
18
19
19
# base exception class for server responses
20
20
class RequestError (exceptions .HTTPError ):
21
21
default_msg = 'Server Error'
22
22
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
25
35
try :
26
- decoded = server_response .json ()
36
+ decoded = self . response .json ()
27
37
if 'exception' in decoded :
28
38
# use labkey server error message if available
29
39
msg = decoded ['exception' ]
30
40
self .server_exception = decoded
31
- else :
32
- msg = self .default_msg
33
41
except ValueError :
34
42
# no valid json to decode
35
- raise ServerNotFoundError ( server_response )
43
+ pass
36
44
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'
38
48
39
- self .response = server_response
49
+ def __str__ (self ):
50
+ return repr (self .message )
40
51
41
52
42
53
class QueryNotFoundError (RequestError ):
@@ -48,11 +59,7 @@ class RequestAuthorizationError(RequestError):
48
59
49
60
50
61
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'
56
63
57
64
58
65
class ServerContextError (exceptions .HTTPError ):
0 commit comments