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. type Error interface {
  46. IsStreamError() bool
  47. IsConnectionError() bool
  48. error
  49. }
  50. // ConnectionError is an error that results in the termination of the
  51. // entire connection.
  52. type ConnectionError ErrCode
  53. var _ Error = ConnectionError(0)
  54. func (e ConnectionError) IsStreamError() bool { return false }
  55. func (e ConnectionError) IsConnectionError() bool { return true }
  56. func (e ConnectionError) Error() string { return fmt.Sprintf("connection error: %s", ErrCode(e)) }
  57. // StreamError is an error that only affects one stream within an
  58. // HTTP/2 connection.
  59. type StreamError uint32
  60. var _ Error = StreamError(0)
  61. func (e StreamError) IsStreamError() bool { return true }
  62. func (e StreamError) IsConnectionError() bool { return false }
  63. func (e StreamError) Error() string { return fmt.Sprintf("stream error: stream ID = %d", uint32(e)) }