How to use 'raise_for_status' in Python

Every line of 'raise_for_status' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
12def raise_for_status(r):
13 if r.status_code != requests.codes.ok:
14 error = f'PIC-SURE API reported an error: [{r.status_code}] {r.text}'
15 logger.exception(error)
16 raise RuntimeError(error)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

110@staticmethod
111def _raise_for_status(response):
112 """Raises stored :class:`HTTPError`, if one occurred."""
113 http_error_msg = ''
114 if isinstance(response.reason, bytes):
115 try:
116 reason = response.reason.decode('utf-8')
117 except UnicodeDecodeError:
118 reason = response.reason.decode('iso-8859-1')
119 else:
120 reason = response.reason
121
122 if 400 <= response.status_code < 500:
123 http_error_msg = u'%s Client Error: %s for url: %s (%s)' % (response.status_code, reason, response.url, response.content.decode('utf-8'))
124
125 elif 500 <= response.status_code < 600:
126 http_error_msg = u'%s Server Error: %s for url: %s (%s)' % (response.status_code, reason, response.url, response.content.decode('utf-8'))
127
128 if http_error_msg:
129 raise requests.exceptions.HTTPError(http_error_msg, response=response)

Related snippets