@@ -73,6 +73,21 @@ def _get_project(): # type: () -> str
7373 sys .exit (1 )
7474
7575
76+ def _handle_error (operation , exc ):
77+ if exc .response is not None and exc .response .content :
78+ # we make the assumption that all responses will be JSON encoded
79+ LOG .error (exc .response .json ()['detail' ])
80+ else :
81+ LOG .error ('Failed to %s resource. Is your configuration '
82+ 'correct?' % operation )
83+ LOG .error ("Use the '--debug' flag for more information" )
84+
85+ if CONF .debug :
86+ raise
87+ else :
88+ sys .exit (1 )
89+
90+
7691def get (url , params = None ): # type: (str, dict) -> requests.Response
7792 """Make GET request and handle errors."""
7893 LOG .debug ('GET %s' , url )
@@ -82,18 +97,7 @@ def get(url, params=None): # type: (str, dict) -> requests.Response
8297 params = params )
8398 rsp .raise_for_status ()
8499 except requests .exceptions .RequestException as exc :
85- if exc .response is not None and exc .response .content :
86- # we make the assumption that all resposes will be JSON encoded
87- LOG .error (exc .response .json ()['detail' ])
88- else :
89- LOG .error ('Failed to fetch resource. Is your configuration '
90- 'correct?' )
91- LOG .error ("Use the '--debug' flag for more information" )
92-
93- if CONF .debug :
94- raise
95- else :
96- sys .exit (1 )
100+ _handle_error ('fetch' , exc )
97101
98102 LOG .debug ('Got response' )
99103
@@ -109,17 +113,7 @@ def put(url, data): # type: (str, dict) -> requests.Response
109113 data = data )
110114 rsp .raise_for_status ()
111115 except requests .exceptions .RequestException as exc :
112- if exc .response is not None and exc .response .content :
113- LOG .error (exc .response .json ()['detail' ])
114- else :
115- LOG .error ('Failed to update resource. Is your configuration '
116- 'correct?' )
117- LOG .error ("Use the '--debug' flag for more information" )
118-
119- if CONF .debug :
120- raise
121- else :
122- sys .exit (1 )
116+ _handle_error ('update' , exc )
123117
124118 LOG .debug ('Got response' )
125119
0 commit comments