errors.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # -*- coding: utf-8 -*-
  2. """
  3. h2/errors
  4. ~~~~~~~~~~~~~~~~~~~
  5. Global error code registry containing the established HTTP/2 error codes.
  6. The current registry is available at:
  7. https://tools.ietf.org/html/rfc7540#section-11.4
  8. """
  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):
  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. #: Graceful shutdown.
  54. #:
  55. #: .. deprecated:: 2.5.0
  56. #: Deprecated in favour of :class:`ErrorCodes.NO_ERROR
  57. #: <h2.errors.ErrorCodes.NO_ERROR>`.
  58. NO_ERROR = ErrorCodes.NO_ERROR
  59. #: Protocol error detected.
  60. #:
  61. #: .. deprecated:: 2.5.0
  62. #: Deprecated in favour of :class:`ErrorCodes.PROTOCOL_ERROR
  63. #: <h2.errors.ErrorCodes.PROTOCOL_ERROR>`.
  64. PROTOCOL_ERROR = ErrorCodes.PROTOCOL_ERROR
  65. #: Implementation fault.
  66. #:
  67. #: .. deprecated:: 2.5.0
  68. #: Deprecated in favour of :class:`ErrorCodes.INTERNAL_ERROR
  69. #: <h2.errors.ErrorCodes.INTERNAL_ERROR>`.
  70. INTERNAL_ERROR = ErrorCodes.INTERNAL_ERROR
  71. #: Flow-control limits exceeded.
  72. #:
  73. #: .. deprecated:: 2.5.0
  74. #: Deprecated in favour of :class:`ErrorCodes.FLOW_CONTROL_ERROR
  75. #: <h2.errors.ErrorCodes.FLOW_CONTROL_ERROR>`.
  76. FLOW_CONTROL_ERROR = ErrorCodes.FLOW_CONTROL_ERROR
  77. #: Settings not acknowledged.
  78. #:
  79. #: .. deprecated:: 2.5.0
  80. #: Deprecated in favour of :class:`ErrorCodes.SETTINGS_TIMEOUT
  81. #: <h2.errors.ErrorCodes.SETTINGS_TIMEOUT>`.
  82. SETTINGS_TIMEOUT = ErrorCodes.SETTINGS_TIMEOUT
  83. #: Frame received for closed stream.
  84. #:
  85. #: .. deprecated:: 2.5.0
  86. #: Deprecated in favour of :class:`ErrorCodes.STREAM_CLOSED
  87. #: <h2.errors.ErrorCodes.STREAM_CLOSED>`.
  88. STREAM_CLOSED = ErrorCodes.STREAM_CLOSED
  89. #: Frame size incorrect.
  90. #:
  91. #: .. deprecated:: 2.5.0
  92. #: Deprecated in favour of :class:`ErrorCodes.FRAME_SIZE_ERROR
  93. #: <h2.errors.ErrorCodes.FRAME_SIZE_ERROR>`.
  94. FRAME_SIZE_ERROR = ErrorCodes.FRAME_SIZE_ERROR
  95. #: Stream not processed.
  96. #:
  97. #: .. deprecated:: 2.5.0
  98. #: Deprecated in favour of :class:`ErrorCodes.REFUSED_STREAM
  99. #: <h2.errors.ErrorCodes.REFUSED_STREAM>`.
  100. REFUSED_STREAM = ErrorCodes.REFUSED_STREAM
  101. #: Stream cancelled.
  102. #:
  103. #: .. deprecated:: 2.5.0
  104. #: Deprecated in favour of :class:`ErrorCodes.CANCEL
  105. #: <h2.errors.ErrorCodes.CANCEL>`.
  106. CANCEL = ErrorCodes.CANCEL
  107. #: Compression state not updated.
  108. #:
  109. #: .. deprecated:: 2.5.0
  110. #: Deprecated in favour of :class:`ErrorCodes.COMPRESSION_ERROR
  111. #: <h2.errors.ErrorCodes.COMPRESSION_ERROR>`.
  112. COMPRESSION_ERROR = ErrorCodes.COMPRESSION_ERROR
  113. #: TCP connection error for CONNECT method.
  114. #:
  115. #: .. deprecated:: 2.5.0
  116. #: Deprecated in favour of :class:`ErrorCodes.CONNECT_ERROR
  117. #: <h2.errors.ErrorCodes.CONNECT_ERROR>`.
  118. CONNECT_ERROR = ErrorCodes.CONNECT_ERROR
  119. #: Processing capacity exceeded.
  120. #:
  121. #: .. deprecated:: 2.5.0
  122. #: Deprecated in favour of :class:`ErrorCodes.ENHANCE_YOUR_CALM
  123. #: <h2.errors.ErrorCodes.ENHANCE_YOUR_CALM>`.
  124. ENHANCE_YOUR_CALM = ErrorCodes.ENHANCE_YOUR_CALM
  125. #: Negotiated TLS parameters not acceptable.
  126. #:
  127. #: .. deprecated:: 2.5.0
  128. #: Deprecated in favour of :class:`ErrorCodes.INADEQUATE_SECURITY
  129. #: <h2.errors.ErrorCodes.INADEQUATE_SECURITY>`.
  130. INADEQUATE_SECURITY = ErrorCodes.INADEQUATE_SECURITY
  131. #: Use HTTP/1.1 for the request.
  132. #:
  133. #: .. deprecated:: 2.5.0
  134. #: Deprecated in favour of :class:`ErrorCodes.HTTP_1_1_REQUIRED
  135. #: <h2.errors.ErrorCodes.HTTP_1_1_REQUIRED>`.
  136. HTTP_1_1_REQUIRED = ErrorCodes.HTTP_1_1_REQUIRED
  137. #: All known HTTP/2 error codes.
  138. #:
  139. #: .. deprecated:: 2.5.0
  140. #: Deprecated in favour of :class:`ErrorCodes <h2.errors.ErrorCodes>`.
  141. H2_ERRORS = list(ErrorCodes)
  142. __all__ = ['H2_ERRORS', 'NO_ERROR', 'PROTOCOL_ERROR', 'INTERNAL_ERROR',
  143. 'FLOW_CONTROL_ERROR', 'SETTINGS_TIMEOUT', 'STREAM_CLOSED',
  144. 'FRAME_SIZE_ERROR', 'REFUSED_STREAM', 'CANCEL', 'COMPRESSION_ERROR',
  145. 'CONNECT_ERROR', 'ENHANCE_YOUR_CALM', 'INADEQUATE_SECURITY',
  146. 'HTTP_1_1_REQUIRED', 'ErrorCodes']