You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

34 lines
1.2 KiB

  1. """HTTP client errors"""
  2. from nilmdb.utils.printf import *
  3. class Error(Exception):
  4. """Base exception for both ClientError and ServerError responses"""
  5. def __init__(self,
  6. status = "Unspecified error",
  7. message = None,
  8. url = None,
  9. traceback = None):
  10. Exception.__init__(self, status)
  11. self.status = status # e.g. "400 Bad Request"
  12. self.message = message # textual message from the server
  13. self.url = url # URL we were requesting
  14. self.traceback = traceback # server traceback, if available
  15. def _format_error(self, show_url):
  16. s = sprintf("[%s]", self.status)
  17. if self.message:
  18. s += sprintf(" %s", self.message)
  19. if show_url and self.url: # pragma: no cover
  20. s += sprintf(" (%s)", self.url)
  21. if self.traceback: # pragma: no cover
  22. s += sprintf("\nServer traceback:\n%s", self.traceback)
  23. return s
  24. def __str__(self):
  25. return self._format_error(show_url = False)
  26. def __repr__(self): # pragma: no cover
  27. return self._format_error(show_url = True)
  28. class ClientError(Error):
  29. pass
  30. class ServerError(Error):
  31. pass