errors.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """
  2. h2/errors
  3. ~~~~~~~~~
  4. Global error code registry containing the established HTTP/2 error codes.
  5. The current registry is available at:
  6. https://tools.ietf.org/html/rfc7540#section-11.4
  7. """
  8. from __future__ import annotations
  9. import enum
  10. class ErrorCodes(enum.IntEnum):
  11. """
  12. All known HTTP/2 error codes.
  13. .. versionadded:: 2.5.0
  14. """
  15. #: Graceful shutdown.
  16. NO_ERROR = 0x0
  17. #: Protocol error detected.
  18. PROTOCOL_ERROR = 0x1
  19. #: Implementation fault.
  20. INTERNAL_ERROR = 0x2
  21. #: Flow-control limits exceeded.
  22. FLOW_CONTROL_ERROR = 0x3
  23. #: Settings not acknowledged.
  24. SETTINGS_TIMEOUT = 0x4
  25. #: Frame received for closed stream.
  26. STREAM_CLOSED = 0x5
  27. #: Frame size incorrect.
  28. FRAME_SIZE_ERROR = 0x6
  29. #: Stream not processed.
  30. REFUSED_STREAM = 0x7
  31. #: Stream cancelled.
  32. CANCEL = 0x8
  33. #: Compression state not updated.
  34. COMPRESSION_ERROR = 0x9
  35. #: TCP connection error for CONNECT method.
  36. CONNECT_ERROR = 0xa
  37. #: Processing capacity exceeded.
  38. ENHANCE_YOUR_CALM = 0xb
  39. #: Negotiated TLS parameters not acceptable.
  40. INADEQUATE_SECURITY = 0xc
  41. #: Use HTTP/1.1 for the request.
  42. HTTP_1_1_REQUIRED = 0xd
  43. def _error_code_from_int(code: int) -> ErrorCodes | int:
  44. """
  45. Given an integer error code, returns either one of :class:`ErrorCodes
  46. <h2.errors.ErrorCodes>` or, if not present in the known set of codes,
  47. returns the integer directly.
  48. """
  49. try:
  50. return ErrorCodes(code)
  51. except ValueError:
  52. return code
  53. __all__ = ["ErrorCodes"]