errors.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package http2
  5. import (
  6. "errors"
  7. "fmt"
  8. )
  9. // An ErrCode is an unsigned 32-bit error code as defined in the HTTP/2 spec.
  10. type ErrCode uint32
  11. const (
  12. ErrCodeNo ErrCode = 0x0
  13. ErrCodeProtocol ErrCode = 0x1
  14. ErrCodeInternal ErrCode = 0x2
  15. ErrCodeFlowControl ErrCode = 0x3
  16. ErrCodeSettingsTimeout ErrCode = 0x4
  17. ErrCodeStreamClosed ErrCode = 0x5
  18. ErrCodeFrameSize ErrCode = 0x6
  19. ErrCodeRefusedStream ErrCode = 0x7
  20. ErrCodeCancel ErrCode = 0x8
  21. ErrCodeCompression ErrCode = 0x9
  22. ErrCodeConnect ErrCode = 0xa
  23. ErrCodeEnhanceYourCalm ErrCode = 0xb
  24. ErrCodeInadequateSecurity ErrCode = 0xc
  25. ErrCodeHTTP11Required ErrCode = 0xd
  26. )
  27. var errCodeName = map[ErrCode]string{
  28. ErrCodeNo: "NO_ERROR",
  29. ErrCodeProtocol: "PROTOCOL_ERROR",
  30. ErrCodeInternal: "INTERNAL_ERROR",
  31. ErrCodeFlowControl: "FLOW_CONTROL_ERROR",
  32. ErrCodeSettingsTimeout: "SETTINGS_TIMEOUT",
  33. ErrCodeStreamClosed: "STREAM_CLOSED",
  34. ErrCodeFrameSize: "FRAME_SIZE_ERROR",
  35. ErrCodeRefusedStream: "REFUSED_STREAM",
  36. ErrCodeCancel: "CANCEL",
  37. ErrCodeCompression: "COMPRESSION_ERROR",
  38. ErrCodeConnect: "CONNECT_ERROR",
  39. ErrCodeEnhanceYourCalm: "ENHANCE_YOUR_CALM",
  40. ErrCodeInadequateSecurity: "INADEQUATE_SECURITY",
  41. ErrCodeHTTP11Required: "HTTP_1_1_REQUIRED",
  42. }
  43. func (e ErrCode) String() string {
  44. if s, ok := errCodeName[e]; ok {
  45. return s
  46. }
  47. return fmt.Sprintf("unknown error code 0x%x", uint32(e))
  48. }
  49. // ConnectionError is an error that results in the termination of the
  50. // entire connection.
  51. type ConnectionError ErrCode
  52. func (e ConnectionError) Error() string { return fmt.Sprintf("connection error: %s", ErrCode(e)) }
  53. // StreamError is an error that only affects one stream within an
  54. // HTTP/2 connection.
  55. type StreamError struct {
  56. StreamID uint32
  57. Code ErrCode
  58. }
  59. func (e StreamError) Error() string {
  60. return fmt.Sprintf("stream error: stream ID %d; %v", e.StreamID, e.Code)
  61. }
  62. // 6.9.1 The Flow Control Window
  63. // "If a sender receives a WINDOW_UPDATE that causes a flow control
  64. // window to exceed this maximum it MUST terminate either the stream
  65. // or the connection, as appropriate. For streams, [...]; for the
  66. // connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code."
  67. type goAwayFlowError struct{}
  68. func (goAwayFlowError) Error() string { return "connection exceeded flow control window size" }
  69. // connErrorReason wraps a ConnectionError with an informative error about why it occurs.
  70. // Errors of this type are only returned by the frame parser functions
  71. // and converted into ConnectionError(ErrCodeProtocol).
  72. type connError struct {
  73. Code ErrCode
  74. Reason string
  75. }
  76. func (e connError) Error() string {
  77. return fmt.Sprintf("http2: connection error: %v: %v", e.Code, e.Reason)
  78. }
  79. type pseudoHeaderError string
  80. func (e pseudoHeaderError) Error() string {
  81. return fmt.Sprintf("invalid pseudo-header %q", string(e))
  82. }
  83. type duplicatePseudoHeaderError string
  84. func (e duplicatePseudoHeaderError) Error() string {
  85. return fmt.Sprintf("duplicate pseudo-header %q", string(e))
  86. }
  87. type headerFieldNameError string
  88. func (e headerFieldNameError) Error() string {
  89. return fmt.Sprintf("invalid header field name %q", string(e))
  90. }
  91. type headerFieldValueError string
  92. func (e headerFieldValueError) Error() string {
  93. return fmt.Sprintf("invalid header field value %q", string(e))
  94. }
  95. var (
  96. errMixPseudoHeaderTypes = errors.New("mix of request and response pseudo headers")
  97. errPseudoAfterRegular = errors.New("pseudo header field after regular")
  98. )