@@ -73,6 +73,21 @@ def _get_project(): # type: () -> str
73
73
sys .exit (1 )
74
74
75
75
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
+
76
91
def get (url , params = None ): # type: (str, dict) -> requests.Response
77
92
"""Make GET request and handle errors."""
78
93
LOG .debug ('GET %s' , url )
@@ -82,18 +97,7 @@ def get(url, params=None): # type: (str, dict) -> requests.Response
82
97
params = params )
83
98
rsp .raise_for_status ()
84
99
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 )
97
101
98
102
LOG .debug ('Got response' )
99
103
@@ -109,17 +113,7 @@ def put(url, data): # type: (str, dict) -> requests.Response
109
113
data = data )
110
114
rsp .raise_for_status ()
111
115
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 )
123
117
124
118
LOG .debug ('Got response' )
125
119
0 commit comments