errors.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2014 The Go Authors.
  2. // See https://code.google.com/p/go/source/browse/CONTRIBUTORS
  3. // Licensed under the same terms as Go itself:
  4. // https://code.google.com/p/go/source/browse/LICENSE
  5. package http2
  6. import "fmt"
  7. // An ErrCode is an unsigned 32-bit error code as defined in the HTTP/2 spec.
  8. type ErrCode uint32
  9. const (
  10. ErrCodeNo ErrCode = 0x0
  11. ErrCodeProtocol ErrCode = 0x1
  12. ErrCodeInternal ErrCode = 0x2
  13. ErrCodeFlowControl ErrCode = 0x3
  14. ErrCodeSettingsTimeout ErrCode = 0x4
  15. ErrCodeStreamClosed ErrCode = 0x5
  16. ErrCodeFrameSize ErrCode = 0x6
  17. ErrCodeRefusedStream ErrCode = 0x7
  18. ErrCodeCancel ErrCode = 0x8
  19. ErrCodeCompression ErrCode = 0x9
  20. ErrCodeConnect ErrCode = 0xa
  21. ErrCodeEnhanceYourCalm ErrCode = 0xb
  22. ErrCodeInadequateSecurity ErrCode = 0xc
  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. }
  39. func (e ErrCode) String() string {
  40. if s, ok := errCodeName[e]; ok {
  41. return s
  42. }
  43. return fmt.Sprintf("unknown error code 0x%x", e)
  44. }
  45. // ConnectionError is an error that results in the termination of the
  46. // entire connection.
  47. type ConnectionError ErrCode
  48. func (e ConnectionError) Error() string { return fmt.Sprintf("connection error: %s", ErrCode(e)) }
  49. // StreamError is an error that only affects one stream within an
  50. // HTTP/2 connection.
  51. type StreamError struct {
  52. streamID uint32
  53. code ErrCode
  54. }
  55. func (e StreamError) Error() string {
  56. return fmt.Sprintf("stream error: stream ID %d; %v", e.streamID, e.code)
  57. }
  58. // 6.9.1 The Flow Control Window
  59. // "If a sender receives a WINDOW_UPDATE that causes a flow control
  60. // window to exceed this maximum it MUST terminate either the stream
  61. // or the connection, as appropriate. For streams, [...]; for the
  62. // connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code."
  63. type goAwayFlowError struct{}
  64. func (goAwayFlowError) Error() string { return "connection exceeded flow control window size" }