An HTTP error from the API.
| 40 | |
| 41 | |
| 42 | class APIError(requests.exceptions.HTTPError, DockerException): |
| 43 | """ |
| 44 | An HTTP error from the API. |
| 45 | """ |
| 46 | def __init__(self, message, response=None, explanation=None): |
| 47 | # requests 1.2 supports response as a keyword argument, but |
| 48 | # requests 1.1 doesn't |
| 49 | super().__init__(message) |
| 50 | self.response = response |
| 51 | self.explanation = explanation |
| 52 | |
| 53 | def __str__(self): |
| 54 | message = super().__str__() |
| 55 | |
| 56 | if self.is_client_error(): |
| 57 | message = ( |
| 58 | f'{self.response.status_code} Client Error for ' |
| 59 | f'{self.response.url}: {self.response.reason}' |
| 60 | ) |
| 61 | |
| 62 | elif self.is_server_error(): |
| 63 | message = ( |
| 64 | f'{self.response.status_code} Server Error for ' |
| 65 | f'{self.response.url}: {self.response.reason}' |
| 66 | ) |
| 67 | |
| 68 | if self.explanation: |
| 69 | message = f'{message} ("{self.explanation}")' |
| 70 | |
| 71 | return message |
| 72 | |
| 73 | @property |
| 74 | def status_code(self): |
| 75 | if self.response is not None: |
| 76 | return self.response.status_code |
| 77 | |
| 78 | def is_error(self): |
| 79 | return self.is_client_error() or self.is_server_error() |
| 80 | |
| 81 | def is_client_error(self): |
| 82 | if self.status_code is None: |
| 83 | return False |
| 84 | return 400 <= self.status_code < 500 |
| 85 | |
| 86 | def is_server_error(self): |
| 87 | if self.status_code is None: |
| 88 | return False |
| 89 | return 500 <= self.status_code < 600 |
| 90 | |
| 91 | |
| 92 | class NotFound(APIError): |
no outgoing calls