Attempt to decode utf-8 first since some servers localize reason strings, for invalid utf-8, fall back to decoding with iso-8859-1. Args: rsp: The http response. Raises: HTTPError: The http error info.
(rsp)
| 210 | |
| 211 | |
| 212 | def raise_for_http_status(rsp): |
| 213 | """Attempt to decode utf-8 first since some servers |
| 214 | localize reason strings, for invalid utf-8, fall back |
| 215 | to decoding with iso-8859-1. |
| 216 | |
| 217 | Args: |
| 218 | rsp: The http response. |
| 219 | |
| 220 | Raises: |
| 221 | HTTPError: The http error info. |
| 222 | """ |
| 223 | http_error_msg = '' |
| 224 | if isinstance(rsp.reason, bytes): |
| 225 | try: |
| 226 | reason = rsp.reason.decode('utf-8') |
| 227 | except UnicodeDecodeError: |
| 228 | reason = rsp.reason.decode('iso-8859-1') |
| 229 | else: |
| 230 | reason = rsp.reason |
| 231 | request_id = get_request_id(rsp) |
| 232 | if 404 == rsp.status_code: |
| 233 | http_error_msg = 'The request resource(model or dataset) does not exist!,' |
| 234 | 'url: %s, reason: %s' % (rsp.url, reason) |
| 235 | elif 403 == rsp.status_code: |
| 236 | http_error_msg = 'Authentication token does not exist or invalid.' |
| 237 | elif 400 <= rsp.status_code < 500: |
| 238 | http_error_msg = u'%s Client Error: %s, Request id: %s for url: %s' % ( |
| 239 | rsp.status_code, reason, request_id, rsp.url) |
| 240 | |
| 241 | elif 500 <= rsp.status_code < 600: |
| 242 | http_error_msg = u'%s Server Error: %s, Request id: %s, for url: %s' % ( |
| 243 | rsp.status_code, reason, request_id, rsp.url) |
| 244 | |
| 245 | if http_error_msg: |
| 246 | req = rsp.request |
| 247 | if req.method == 'POST': |
| 248 | http_error_msg = u'%s, body: %s' % (http_error_msg, req.body) |
| 249 | raise HTTPError(http_error_msg, response=rsp) |
no test coverage detected
searching dependent graphs…