@@ -13,16 +13,12 @@ class ResponseError(RuntimeError):
1313 ``NetworkError``, ``ParserError``, ``ServerError``,
1414 ``AuthenticationError``, ``NotFoundError`` and ``UnknownError``.
1515 :vartype code: str
16-
17- :var description: The content of the response that describes the error
18- :vartype description: str
1916 '''
2017
2118 def __init__ (self , response ):
2219 self .response = response
23- self .description = self .__determine_description ()
2420 self .code = self .__determine_code ()
25- RuntimeError .__init__ (self , self .description )
21+ RuntimeError .__init__ (self , self .description () )
2622
2723 # PROTECTED
2824
@@ -35,13 +31,51 @@ def _log(self, client):
3531
3632 # PRIVATE
3733
38- # extracts the error description from the response, if it exists
39- def __determine_description (self ):
40- if (self .response and self .response .parsed ):
41- if 'errors' in self .response .result :
42- return self .response .result ['errors' ]
43- if 'error_description' in self .response .result :
44- return self .response .result
34+ # Determines the description for this error, as used in in the error output
35+ def description (self ):
36+ description = self .short_description (self .response )
37+ return description + self .long_description (self .response )
38+
39+ # Determines the short description, printed after on the same line as the
40+ # error class name
41+ def short_description (self , response ):
42+ if hasattr (response , 'status_code' ) and response .status_code :
43+ return '[{0}]' .format (response .status_code )
44+ else :
45+ return '[---]'
46+
47+ # Determines the longer description, printed after the initial error
48+ def long_description (self , response ):
49+ message = ''
50+ if not (response and response .parsed ):
51+ return message
52+ if 'error_description' in response .result :
53+ message += self .error_description (self .response )
54+ if 'errors' in response .result :
55+ message += self .errors_descriptions (self .response )
56+ return message
57+
58+ # Returns the description of a single error
59+ def error_description (self , response ):
60+ message = ''
61+ if 'error' in response .result :
62+ message += '\n {0}' .format (response .result ['error' ])
63+ message += '\n {0}' .format (response .result ['error_description' ])
64+ return message
65+
66+ # Returns the description of multiple errors
67+ def errors_descriptions (self , response ):
68+ messages = map (self .errors_description , response .result ['errors' ])
69+ return '' .join (messages )
70+
71+ # Returns the description of a single error in a multi error response
72+ def errors_description (self , error ):
73+ message = '\n '
74+ if ('source' in error ) and ('parameter' in error ['source' ]):
75+ message += '[{0}] ' .format (error ['source' ]['parameter' ])
76+ if 'detail' in error :
77+ message += error ['detail' ]
78+ return message
4579
4680 # sets the error code to the name of this class
4781 def __determine_code (self ):
0 commit comments