Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions internal/transport/grpchttp2/errors.go
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 0x%x", uint32(err))
}
51 changes: 51 additions & 0 deletions internal/transport/grpchttp2/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
*
* 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_test

import (
"testing"

"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/internal/transport/grpchttp2"
)

type s struct {
grpctest.Tester
}

func Test(t *testing.T) {
grpctest.RunSubTests(t, s{})
}

func (s) TestErrorCodeString(t *testing.T) {
err := grpchttp2.ErrCodeNoError
if err.String() != "NO_ERROR" {
t.Errorf("got %q, want %q", err.String(), "NO_ERROR")
}

err = grpchttp2.ErrCode(0x1)
if err.String() != "PROTOCOL_ERROR" {
t.Errorf("got %q, want %q", err.String(), "PROTOCOL_ERROR")
}

err = grpchttp2.ErrCode(0xf)
if err.String() != "unknown error code 0xf" {
t.Errorf("got %q, want %q", err.String(), "unknown error code 0xf")
}
}
Loading