errors.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "fmt"
  6. // An ErrCode is an unsigned 32-bit error code as defined in the HTTP/2 spec.
  7. type ErrCode uint32
  8. const (
  9. ErrCodeNo ErrCode = 0x0
  10. ErrCodeProtocol ErrCode = 0x1
  11. ErrCodeInternal ErrCode = 0x2
  12. ErrCodeFlowControl ErrCode = 0x3
  13. ErrCodeSettingsTimeout ErrCode = 0x4
  14. ErrCodeStreamClosed ErrCode = 0x5
  15. ErrCodeFrameSize ErrCode = 0x6
  16. ErrCodeRefusedStream ErrCode = 0x7
  17. ErrCodeCancel ErrCode = 0x8
  18. ErrCodeCompression ErrCode = 0x9
  19. ErrCodeConnect ErrCode = 0xa
  20. ErrCodeEnhanceYourCalm ErrCode = 0xb
  21. ErrCodeInadequateSecurity ErrCode = 0xc
  22. ErrCodeHTTP11Required ErrCode = 0xd
  23. )
  24. var errCodeName = map[ErrCode]string{
  25. ErrCodeNo: "NO_ERROR",
  26. ErrCodeProtocol: "PROTOCOL_ERROR",
  27. ErrCodeInternal: "INTERNAL_ERROR",
  28. ErrCodeFlowControl: "FLOW_CONTROL_ERROR",
  29. ErrCodeSettingsTimeout: "SETTINGS_TIMEOUT",
  30. ErrCodeStreamClosed: "STREAM_CLOSED",
  31. ErrCodeFrameSize: "FRAME_SIZE_ERROR",
  32. ErrCodeRefusedStream: "REFUSED_STREAM",
  33. ErrCodeCancel: "CANCEL",
  34. ErrCodeCompression: "COMPRESSION_ERROR",
  35. ErrCodeConnect: "CONNECT_ERROR",
  36. ErrCodeEnhanceYourCalm: "ENHANCE_YOUR_CALM",
  37. ErrCodeInadequateSecurity: "INADEQUATE_SECURITY",
  38. ErrCodeHTTP11Required: "HTTP_1_1_REQUIRED",
  39. }
  40. func (e ErrCode) String() string {
  41. if s, ok := errCodeName[e]; ok {
  42. return s
  43. }
  44. return fmt.Sprintf("unknown error code 0x%x", uint32(e))
  45. }
  46. // ConnectionError is an error that results in the termination of the
  47. // entire connection.
  48. type ConnectionError ErrCode
  49. func (e ConnectionError) Error() string { return fmt.Sprintf("connection error: %s", ErrCode(e)) }
  50. // StreamError is an error that only affects one stream within an
  51. // HTTP/2 connection.
  52. type StreamError struct {
  53. StreamID uint32
  54. Code ErrCode
  55. }
  56. func (e StreamError) Error() string {
  57. return fmt.Sprintf("stream error: stream ID %d; %v", e.StreamID, e.Code)
  58. }
  59. // 6.9.1 The Flow Control Window
  60. // "If a sender receives a WINDOW_UPDATE that causes a flow control
  61. // window to exceed this maximum it MUST terminate either the stream
  62. // or the connection, as appropriate. For streams, [...]; for the
  63. // connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code."
  64. type goAwayFlowError struct{}
  65. func (goAwayFlowError) Error() string { return "connection exceeded flow control window size" }
  66. // connErrorReason wraps a ConnectionError with an informative error about why it occurs.
  67. // Errors of this type are only returned by the frame parser functions
  68. // and converted into ConnectionError(ErrCodeProtocol).
  69. type connError struct {
  70. Code ErrCode
  71. Reason string
  72. }
  73. func (e connError) Error() string {
  74. return fmt.Sprintf("http2: connection error: %v: %v", e.Code, e.Reason)
  75. }