_exceptions.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. from typing import Callable, Generator, Type
  9. class InvalidProxyHeader(Exception):
  10. """
  11. The provided PROXY protocol header is invalid.
  12. """
  13. class InvalidNetworkProtocol(InvalidProxyHeader):
  14. """
  15. The network protocol was not one of TCP4 TCP6 or UNKNOWN.
  16. """
  17. class MissingAddressData(InvalidProxyHeader):
  18. """
  19. The address data is missing or incomplete.
  20. """
  21. @contextlib.contextmanager
  22. def convertError(
  23. sourceType: Type[BaseException], targetType: Callable[[], BaseException]
  24. ) -> Generator[None, None, None]:
  25. """
  26. Convert an error into a different error type.
  27. @param sourceType: The type of exception that should be caught and
  28. converted.
  29. @type sourceType: L{BaseException}
  30. @param targetType: The type of exception to which the original should be
  31. converted.
  32. @type targetType: L{BaseException}
  33. """
  34. try:
  35. yield
  36. except sourceType as e:
  37. raise targetType().with_traceback(e.__traceback__)