exceptions.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. # (The MIT License)
  3. #
  4. # Copyright (c) 2014 Kura
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the 'Software'), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. from requests.exceptions import HTTPError as RHTTPError
  24. class YargException(Exception):
  25. pass
  26. class HTTPError(YargException, RHTTPError):
  27. """
  28. A catchall HTTPError exception to handle HTTP errors
  29. when using :meth:`yarg.get`.
  30. This exception is also loaded at :class:`yarg.HTTPError`
  31. for ease of access.
  32. :member: status_code
  33. """
  34. def __init__(self, *args, **kwargs):
  35. for key, val in kwargs.items():
  36. setattr(self, key, val)
  37. if hasattr(self, 'status_code'):
  38. setattr(self, 'errno', self.status_code)
  39. if hasattr(self, 'reason'):
  40. setattr(self, 'message', self.reason)
  41. def __str__(self):
  42. return self.__repr__()
  43. def __repr__(self):
  44. if hasattr(self, 'status_code') and hasattr(self, 'reason'):
  45. return "<HTTPError {0} {1}>".format(self.status_code, self.reason)
  46. return "<HTTPError>"