errors.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. func (e ErrCode) stringToken() string {
  50. if s, ok := errCodeName[e]; ok {
  51. return s
  52. }
  53. return fmt.Sprintf("ERR_UNKNOWN_%d", uint32(e))
  54. }
  55. // ConnectionError is an error that results in the termination of the
  56. // entire connection.
  57. type ConnectionError ErrCode
  58. func (e ConnectionError) Error() string { return fmt.Sprintf("connection error: %s", ErrCode(e)) }
  59. // StreamError is an error that only affects one stream within an
  60. // HTTP/2 connection.
  61. type StreamError struct {
  62. StreamID uint32
  63. Code ErrCode
  64. Cause error // optional additional detail
  65. }
  66. // errFromPeer is a sentinel error value for StreamError.Cause to
  67. // indicate that the StreamError was sent from the peer over the wire
  68. // and wasn't locally generated in the Transport.
  69. var errFromPeer = errors.New("received from peer")
  70. func streamError(id uint32, code ErrCode) StreamError {
  71. return StreamError{StreamID: id, Code: code}
  72. }
  73. func (e StreamError) Error() string {
  74. if e.Cause != nil {
  75. return fmt.Sprintf("stream error: stream ID %d; %v; %v", e.StreamID, e.Code, e.Cause)
  76. }
  77. return fmt.Sprintf("stream error: stream ID %d; %v", e.StreamID, e.Code)
  78. }
  79. // 6.9.1 The Flow Control Window
  80. // "If a sender receives a WINDOW_UPDATE that causes a flow control
  81. // window to exceed this maximum it MUST terminate either the stream
  82. // or the connection, as appropriate. For streams, [...]; for the
  83. // connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code."
  84. type goAwayFlowError struct{}
  85. func (goAwayFlowError) Error() string { return "connection exceeded flow control window size" }
  86. // connError represents an HTTP/2 ConnectionError error code, along
  87. // with a string (for debugging) explaining why.
  88. //
  89. // Errors of this type are only returned by the frame parser functions
  90. // and converted into ConnectionError(Code), after stashing away
  91. // the Reason into the Framer's errDetail field, accessible via
  92. // the (*Framer).ErrorDetail method.
  93. type connError struct {
  94. Code ErrCode // the ConnectionError error code
  95. Reason string // additional reason
  96. }
  97. func (e connError) Error() string {
  98. return fmt.Sprintf("http2: connection error: %v: %v", e.Code, e.Reason)
  99. }
  100. type pseudoHeaderError string
  101. func (e pseudoHeaderError) Error() string {
  102. return fmt.Sprintf("invalid pseudo-header %q", string(e))
  103. }
  104. type duplicatePseudoHeaderError string
  105. func (e duplicatePseudoHeaderError) Error() string {
  106. return fmt.Sprintf("duplicate pseudo-header %q", string(e))
  107. }
  108. type headerFieldNameError string
  109. func (e headerFieldNameError) Error() string {
  110. return fmt.Sprintf("invalid header field name %q", string(e))
  111. }
  112. type headerFieldValueError string
  113. func (e headerFieldValueError) Error() string {
  114. return fmt.Sprintf("invalid header field value for %q", string(e))
  115. }
  116. var (
  117. errMixPseudoHeaderTypes = errors.New("mix of request and response pseudo headers")
  118. errPseudoAfterRegular = errors.New("pseudo header field after regular")
  119. )