Raise the `HTTPStatusError` if one occurred.
(self)
| 792 | ) |
| 793 | |
| 794 | def raise_for_status(self) -> Response: |
| 795 | class="st">""" |
| 796 | Raise the `HTTPStatusError` if one occurred. |
| 797 | class="st">""" |
| 798 | request = self._request |
| 799 | if request is None: |
| 800 | raise RuntimeError( |
| 801 | class="st">"Cannot call `raise_for_status` as the request " |
| 802 | class="st">"instance has not been set on this response." |
| 803 | ) |
| 804 | |
| 805 | if self.is_success: |
| 806 | return self |
| 807 | |
| 808 | if self.has_redirect_location: |
| 809 | message = ( |
| 810 | class="st">"{error_type} &class="cm">#x27;{0.status_code} {0.reason_phrase}' for url '{0.url}'\n" |
| 811 | class="st">"Redirect location: &class="cm">#x27;{0.headers[location]}'\n" |
| 812 | class="st">"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}" |
| 813 | ) |
| 814 | else: |
| 815 | message = ( |
| 816 | class="st">"{error_type} &class="cm">#x27;{0.status_code} {0.reason_phrase}' for url '{0.url}'\n" |
| 817 | class="st">"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}" |
| 818 | ) |
| 819 | |
| 820 | status_class = self.status_code // 100 |
| 821 | error_types = { |
| 822 | 1: class="st">"Informational response", |
| 823 | 3: class="st">"Redirect response", |
| 824 | 4: class="st">"Client error", |
| 825 | 5: class="st">"Server error", |
| 826 | } |
| 827 | error_type = error_types.get(status_class, class="st">"Invalid status code") |
| 828 | message = message.format(self, error_type=error_type) |
| 829 | raise HTTPStatusError(message, request=request, response=self) |
| 830 | |
| 831 | def json(self, **kwargs: typing.Any) -> typing.Any: |
| 832 | return jsonlib.loads(self.content, **kwargs) |