-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transport/grpcframer: create grpcframer package (#7397)
- Loading branch information
1 parent
2bcbcab
commit 0231b0d
Showing
3 changed files
with
456 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* | ||
* Copyright 2024 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package grpchttp2 | ||
|
||
import "fmt" | ||
|
||
// ErrCode represents an HTTP/2 Error Code. Error codes are 32-bit fields | ||
// that are used in [RST_STREAM] and [GOAWAY] frames to convey the reasons for | ||
// the stream or connection error. See [HTTP/2 Error Code] for definitions of | ||
// each of the following error codes. | ||
// | ||
// [HTTP/2 Error Code]: https://httpwg.org/specs/rfc7540.html#ErrorCodes | ||
// [RST_STREAM]: https://httpwg.org/specs/rfc7540.html#RST_STREAM | ||
// [GOAWAY]: https://httpwg.org/specs/rfc7540.html#GOAWAY | ||
type ErrCode uint32 | ||
|
||
const ( | ||
ErrCodeNoError ErrCode = 0x0 | ||
ErrCodeProtocol ErrCode = 0x1 | ||
ErrCodeInternal ErrCode = 0x2 | ||
ErrCodeFlowControl ErrCode = 0x3 | ||
ErrCodeSettingsTimeout ErrCode = 0x4 | ||
ErrCodeStreamClosed ErrCode = 0x5 | ||
ErrCodeFrameSize ErrCode = 0x6 | ||
ErrCodeRefusedStream ErrCode = 0x7 | ||
ErrCodeCancel ErrCode = 0x8 | ||
ErrCodeCompression ErrCode = 0x9 | ||
ErrCodeConnect ErrCode = 0xa | ||
ErrCodeEnhanceYourCalm ErrCode = 0xb | ||
ErrCodeIndaequateSecurity ErrCode = 0xc | ||
ErrCodeHTTP11Required ErrCode = 0xd | ||
) | ||
|
||
var errorCodeNames = map[ErrCode]string{ | ||
ErrCodeNoError: "NO_ERROR", | ||
ErrCodeProtocol: "PROTOCOL_ERROR", | ||
ErrCodeInternal: "INTERNAL_ERROR", | ||
ErrCodeFlowControl: "FLOW_CONTROL_ERROR", | ||
ErrCodeSettingsTimeout: "SETTINGS_TIMEOUT", | ||
ErrCodeStreamClosed: "STREAM_CLOSED", | ||
ErrCodeFrameSize: "FRAME_SIZE_ERROR", | ||
ErrCodeRefusedStream: "REFUSED_STREAM", | ||
ErrCodeCancel: "CANCEL", | ||
ErrCodeCompression: "COMPRESSION_ERROR", | ||
ErrCodeConnect: "CONNECT_ERROR", | ||
ErrCodeEnhanceYourCalm: "ENHANCE_YOUR_CALM", | ||
ErrCodeIndaequateSecurity: "INADEQUATE_SECURITY", | ||
ErrCodeHTTP11Required: "HTTP_1_1_REQUIRED", | ||
} | ||
|
||
func (err ErrCode) String() string { | ||
if v, ok := errorCodeNames[err]; ok { | ||
return v | ||
} | ||
return fmt.Sprintf("unknown error code %#x", uint32(err)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* | ||
* Copyright 2024 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package grpchttp2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"google.golang.org/grpc/internal/grpctest" | ||
) | ||
|
||
type s struct { | ||
grpctest.Tester | ||
} | ||
|
||
func Test(t *testing.T) { | ||
grpctest.RunSubTests(t, s{}) | ||
} | ||
|
||
func (s) TestErrorCodeString(t *testing.T) { | ||
tests := []struct { | ||
err ErrCode | ||
want string | ||
}{ | ||
// Known error cases | ||
{err: ErrCodeNoError, want: "NO_ERROR"}, | ||
{err: ErrCodeProtocol, want: "PROTOCOL_ERROR"}, | ||
{err: ErrCodeInternal, want: "INTERNAL_ERROR"}, | ||
{err: ErrCodeFlowControl, want: "FLOW_CONTROL_ERROR"}, | ||
{err: ErrCodeSettingsTimeout, want: "SETTINGS_TIMEOUT"}, | ||
{err: ErrCodeStreamClosed, want: "STREAM_CLOSED"}, | ||
{err: ErrCodeFrameSize, want: "FRAME_SIZE_ERROR"}, | ||
{err: ErrCodeRefusedStream, want: "REFUSED_STREAM"}, | ||
{err: ErrCodeCancel, want: "CANCEL"}, | ||
{err: ErrCodeCompression, want: "COMPRESSION_ERROR"}, | ||
{err: ErrCodeConnect, want: "CONNECT_ERROR"}, | ||
{err: ErrCodeEnhanceYourCalm, want: "ENHANCE_YOUR_CALM"}, | ||
{err: ErrCodeIndaequateSecurity, want: "INADEQUATE_SECURITY"}, | ||
{err: ErrCodeHTTP11Required, want: "HTTP_1_1_REQUIRED"}, | ||
// Type casting known error case | ||
{err: ErrCode(0x1), want: "PROTOCOL_ERROR"}, | ||
// Unknown error case | ||
{err: ErrCode(0xf), want: "unknown error code 0xf"}, | ||
} | ||
|
||
for _, test := range tests { | ||
got := test.err.String() | ||
if got != test.want { | ||
t.Errorf("ErrCode.String(%#x) = %q, want %q", int(test.err), got, test.want) | ||
} | ||
} | ||
} |
Oops, something went wrong.