forked from dgrr/http2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
146 lines (128 loc) · 3.7 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package http2
import (
"errors"
"fmt"
"strconv"
)
// ErrorCode defines the HTTP/2 error codes:
//
// Error codes are defined here http://httpwg.org/specs/rfc7540.html#ErrorCodes
//
// Errors must be uint32 because of FrameReset.
type ErrorCode uint32
const (
NoError ErrorCode = 0x0
ProtocolError ErrorCode = 0x1
InternalError ErrorCode = 0x2
FlowControlError ErrorCode = 0x3
SettingsTimeoutError ErrorCode = 0x4
StreamClosedError ErrorCode = 0x5
FrameSizeError ErrorCode = 0x6
RefusedStreamError ErrorCode = 0x7
StreamCanceled ErrorCode = 0x8
CompressionError ErrorCode = 0x9
ConnectionError ErrorCode = 0xa
EnhanceYourCalm ErrorCode = 0xb
InadequateSecurity ErrorCode = 0xc
HTTP11Required ErrorCode = 0xd
)
var errStr = [...]string{
NoError: "NoError",
ProtocolError: "ProtocolError",
InternalError: "InternalError",
FlowControlError: "FlowControlError",
SettingsTimeoutError: "SettingsTimeoutError",
StreamClosedError: "StreamClosedError",
FrameSizeError: "FrameSizeError",
RefusedStreamError: "RefusedStreamError",
StreamCanceled: "StreamCanceled",
CompressionError: "CompressionError",
ConnectionError: "ConnectionError",
EnhanceYourCalm: "EnhanceYourCalm",
InadequateSecurity: "InadequateSecurity",
HTTP11Required: "HTTP11Required",
}
func (e ErrorCode) String() string {
if int(e) >= len(errStr) {
return "Unknown"
}
return errStr[e]
}
// Error implements the error interface.
func (e ErrorCode) Error() string {
if int(e) < len(errParser) {
return errParser[e]
}
return strconv.Itoa(int(e))
}
// Error defines the HTTP/2 errors, composed by the code and debug data.
type Error struct {
code ErrorCode
frameType FrameType
debug string
}
// Is implements the interface for errors.Is.
func (e Error) Is(target error) bool {
return errors.Is(e.code, target)
}
// Code returns the error code.
func (e Error) Code() ErrorCode {
return e.code
}
// Debug returns the debug string.
func (e Error) Debug() string {
return e.debug
}
// NewError creates a new Error.
func NewError(e ErrorCode, debug string) Error {
return Error{
code: e,
debug: debug,
frameType: FrameResetStream,
}
}
func NewGoAwayError(e ErrorCode, debug string) Error {
return Error{
code: e,
debug: debug,
frameType: FrameGoAway,
}
}
func NewResetStreamError(e ErrorCode, debug string) Error {
return Error{
code: e,
debug: debug,
frameType: FrameResetStream,
}
}
// Error implements the error interface.
func (e Error) Error() string {
return fmt.Sprintf("%s: %s", e.code, e.debug)
}
var (
errParser = []string{
NoError: "No errors",
ProtocolError: "Protocol error",
InternalError: "Internal error",
FlowControlError: "Flow control error",
SettingsTimeoutError: "Settings timeout",
StreamClosedError: "Stream have been closed",
FrameSizeError: "FrameHeader size error",
RefusedStreamError: "Refused Stream",
StreamCanceled: "Stream canceled",
CompressionError: "Compression error",
ConnectionError: "Connection error",
EnhanceYourCalm: "Enhance your calm",
InadequateSecurity: "Inadequate security",
HTTP11Required: "HTTP/1.1 required",
}
// ErrUnknownFrameType This error codes must be used with FrameGoAway.
ErrUnknownFrameType = NewError(
ProtocolError, "unknown frame type")
ErrMissingBytes = NewError(
ProtocolError, "missing payload bytes. Need more")
ErrPayloadExceeds = NewError(
FrameSizeError, "FrameHeader payload exceeds the negotiated maximum size")
ErrCompression = NewGoAwayError(
CompressionError, "Compression error")
)