|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2024 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package grpchttp2 |
| 20 | + |
| 21 | +import "fmt" |
| 22 | + |
| 23 | +// ErrCode represents an HTTP/2 Error Code. Error codes are 32-bit fields |
| 24 | +// that are used in [RST_STREAM] and [GOAWAY] frames to convey the reasons for |
| 25 | +// the stream or connection error. See [HTTP/2 Error Code] for definitions of |
| 26 | +// each of the following error codes. |
| 27 | +// |
| 28 | +// [HTTP/2 Error Code]: https://httpwg.org/specs/rfc7540.html#ErrorCodes |
| 29 | +// [RST_STREAM]: https://httpwg.org/specs/rfc7540.html#RST_STREAM |
| 30 | +// [GOAWAY]: https://httpwg.org/specs/rfc7540.html#GOAWAY |
| 31 | +type ErrCode uint32 |
| 32 | + |
| 33 | +const ( |
| 34 | + ErrCodeNoError ErrCode = 0x0 |
| 35 | + ErrCodeProtocol ErrCode = 0x1 |
| 36 | + ErrCodeInternal ErrCode = 0x2 |
| 37 | + ErrCodeFlowControl ErrCode = 0x3 |
| 38 | + ErrCodeSettingsTimeout ErrCode = 0x4 |
| 39 | + ErrCodeStreamClosed ErrCode = 0x5 |
| 40 | + ErrCodeFrameSize ErrCode = 0x6 |
| 41 | + ErrCodeRefusedStream ErrCode = 0x7 |
| 42 | + ErrCodeCancel ErrCode = 0x8 |
| 43 | + ErrCodeCompression ErrCode = 0x9 |
| 44 | + ErrCodeConnect ErrCode = 0xa |
| 45 | + ErrCodeEnhanceYourCalm ErrCode = 0xb |
| 46 | + ErrCodeIndaequateSecurity ErrCode = 0xc |
| 47 | + ErrCodeHTTP11Required ErrCode = 0xd |
| 48 | +) |
| 49 | + |
| 50 | +var errorCodeNames = map[ErrCode]string{ |
| 51 | + ErrCodeNoError: "NO_ERROR", |
| 52 | + ErrCodeProtocol: "PROTOCOL_ERROR", |
| 53 | + ErrCodeInternal: "INTERNAL_ERROR", |
| 54 | + ErrCodeFlowControl: "FLOW_CONTROL_ERROR", |
| 55 | + ErrCodeSettingsTimeout: "SETTINGS_TIMEOUT", |
| 56 | + ErrCodeStreamClosed: "STREAM_CLOSED", |
| 57 | + ErrCodeFrameSize: "FRAME_SIZE_ERROR", |
| 58 | + ErrCodeRefusedStream: "REFUSED_STREAM", |
| 59 | + ErrCodeCancel: "CANCEL", |
| 60 | + ErrCodeCompression: "COMPRESSION_ERROR", |
| 61 | + ErrCodeConnect: "CONNECT_ERROR", |
| 62 | + ErrCodeEnhanceYourCalm: "ENHANCE_YOUR_CALM", |
| 63 | + ErrCodeIndaequateSecurity: "INADEQUATE_SECURITY", |
| 64 | + ErrCodeHTTP11Required: "HTTP_1_1_REQUIRED", |
| 65 | +} |
| 66 | + |
| 67 | +func (err ErrCode) String() string { |
| 68 | + if v, ok := errorCodeNames[err]; ok { |
| 69 | + return v |
| 70 | + } |
| 71 | + return fmt.Sprintf("unknown error code %#x", uint32(err)) |
| 72 | +} |
0 commit comments