errors.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright 2014 dotCloud inc.
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. import requests
  14. class APIError(requests.exceptions.HTTPError):
  15. def __init__(self, message, response, explanation=None):
  16. # requests 1.2 supports response as a keyword argument, but
  17. # requests 1.1 doesn't
  18. super(APIError, self).__init__(message)
  19. self.response = response
  20. self.explanation = explanation
  21. if self.explanation is None and response.content:
  22. self.explanation = response.content.strip()
  23. def __str__(self):
  24. message = super(APIError, self).__str__()
  25. if self.is_client_error():
  26. message = '%s Client Error: %s' % (
  27. self.response.status_code, self.response.reason)
  28. elif self.is_server_error():
  29. message = '%s Server Error: %s' % (
  30. self.response.status_code, self.response.reason)
  31. if self.explanation:
  32. message = '%s ("%s")' % (message, self.explanation)
  33. return message
  34. def is_client_error(self):
  35. return 400 <= self.response.status_code < 500
  36. def is_server_error(self):
  37. return 500 <= self.response.status_code < 600
  38. class DockerException(Exception):
  39. pass
  40. class InvalidRepository(DockerException):
  41. pass
  42. class InvalidConfigFile(DockerException):
  43. pass