_exceptions.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- test-case-name: twisted.protocols.haproxy.test -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. HAProxy specific exceptions.
  6. """
  7. import contextlib
  8. import sys
  9. from twisted.python import compat
  10. class InvalidProxyHeader(Exception):
  11. """
  12. The provided PROXY protocol header is invalid.
  13. """
  14. class InvalidNetworkProtocol(InvalidProxyHeader):
  15. """
  16. The network protocol was not one of TCP4 TCP6 or UNKNOWN.
  17. """
  18. class MissingAddressData(InvalidProxyHeader):
  19. """
  20. The address data is missing or incomplete.
  21. """
  22. @contextlib.contextmanager
  23. def convertError(sourceType, targetType):
  24. """
  25. Convert an error into a different error type.
  26. @param sourceType: The type of exception that should be caught and
  27. converted.
  28. @type sourceType: L{Exception}
  29. @param targetType: The type of exception to which the original should be
  30. converted.
  31. @type targetType: L{Exception}
  32. """
  33. try:
  34. yield None
  35. except sourceType:
  36. compat.reraise(targetType(), sys.exc_info()[-1])