-
Notifications
You must be signed in to change notification settings - Fork 831
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
240f4ab
commit 95da68d
Showing
21 changed files
with
1,483 additions
and
185 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
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
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,61 @@ | ||
/* | ||
* Copyright 2024 CloudWeGo 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 kerrors | ||
|
||
import "errors" | ||
|
||
var ( | ||
// ErrStreamingProtocol is the parent type of all streaming protocol(e.g. gRPC, TTHeader Streaming) | ||
// related but not user-aware errors. | ||
ErrStreamingProtocol = &basicError{"streaming protocol error"} | ||
|
||
// errStreamingTimeout is the parent type of all streaming timeout errors. | ||
errStreamingTimeout = &basicError{"streaming timeout error"} | ||
// ErrStreamTimeout denotes the timeout of the whole stream. | ||
ErrStreamTimeout = errStreamingTimeout.WithCause(errors.New("stream timeout")) | ||
|
||
// ErrStreamingCanceled is the parent type of all streaming canceled errors. | ||
ErrStreamingCanceled = &basicError{"streaming canceled error"} | ||
// ErrBizCanceled denotes the stream is canceled by the biz code invoking cancel(). | ||
ErrBizCanceled = ErrStreamingCanceled.WithCause(errors.New("business canceled")) | ||
// ErrGracefulShutdown denotes the stream is canceled due to graceful shutdown. | ||
ErrGracefulShutdown = ErrStreamingCanceled.WithCause(errors.New("graceful shutdown")) | ||
|
||
// errStreamingMeta is the parent type of all streaming meta errors. | ||
errStreamingMeta = &basicError{"streaming meta error"} | ||
// ErrMetaSizeExceeded denotes the streaming meta size exceeds the limit. | ||
ErrMetaSizeExceeded = errStreamingMeta.WithCause(errors.New("meta size exceeds limit")) | ||
// ErrMetaContentIllegal denotes the streaming meta content is illegal. | ||
ErrMetaContentIllegal = errStreamingMeta.WithCause(errors.New("meta content illegal")) | ||
|
||
streamingBasicErrors = []*basicError{ | ||
ErrStreamingProtocol, | ||
errStreamingTimeout, | ||
ErrStreamingCanceled, | ||
errStreamingMeta, | ||
} | ||
) | ||
|
||
// IsStreamingError reports whether the given err is a streaming err | ||
func IsStreamingError(err error) bool { | ||
for _, sErr := range streamingBasicErrors { | ||
if errors.Is(err, sErr) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,40 @@ | ||
/* | ||
* Copyright 2024 CloudWeGo 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 kerrors | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cloudwego/kitex/internal/test" | ||
) | ||
|
||
func TestIsStreamingError(t *testing.T) { | ||
errs := []error{ | ||
ErrStreamingProtocol, | ||
errStreamingTimeout, | ||
ErrStreamTimeout, | ||
ErrStreamingCanceled, | ||
ErrBizCanceled, | ||
ErrGracefulShutdown, | ||
errStreamingMeta, | ||
ErrMetaSizeExceeded, | ||
ErrMetaContentIllegal, | ||
} | ||
for _, err := range errs { | ||
test.Assert(t, IsStreamingError(err), 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
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,77 @@ | ||
/* | ||
* | ||
* 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. | ||
* | ||
* This file may have been modified by CloudWeGo authors. All CloudWeGo | ||
* Modifications are Copyright 2021 CloudWeGo Authors. | ||
*/ | ||
|
||
package errors | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/cloudwego/kitex/pkg/kerrors" | ||
) | ||
|
||
// This package contains all the errors suitable for Kitex errors model. | ||
// These errors should not be used by user directly. | ||
// If users need to perceive these errors, the pkg/streamx/provider/grpc/gerrors package should be used. | ||
var ( | ||
// stream error | ||
ErrHTTP2Stream = newErrType("HTTP2Stream err when parsing HTTP2 frame") | ||
ErrClosedWithoutTrailer = newErrType("client received Data frame with END_STREAM flag") | ||
ErrMiddleHeader = newErrType("Headers frame appeared in the middle of a stream") | ||
ErrDecodeHeader = newErrType("decoded Headers frame failed") | ||
ErrRecvRstStream = newErrType("received RstStream frame") | ||
ErrStreamDrain = newErrType("stream rejected by draining connection") | ||
ErrStreamFlowControl = newErrType("stream-level flow control") | ||
ErrIllegalHeaderWrite = newErrType("Headers frame has been already sent by server") | ||
ErrStreamIsDone = newErrType("stream is done") | ||
ErrMaxStreamExceeded = newErrType("max stream exceeded") | ||
|
||
// connection error | ||
ErrHTTP2Connection = newErrType("HTTP2Connection err when parsing HTTP2 frame") | ||
ErrEstablishConnection = newErrType("established connection failed") | ||
ErrHandleGoAway = newErrType("handled GoAway Frame failed") | ||
ErrKeepAlive = newErrType("keepalive failed") | ||
ErrOperateHeaders = newErrType("operated Headers Frame failed") | ||
ErrNoActiveStream = newErrType("no active stream") | ||
ErrControlBufFinished = newErrType("controlbuf finished") | ||
ErrNotReachable = newErrType("server transport is not reachable") | ||
ErrConnectionIsClosing = newErrType("connection is closing") | ||
) | ||
|
||
type errType struct { | ||
message string | ||
// parent errType | ||
basic error | ||
} | ||
|
||
func newErrType(message string) *errType { | ||
return &errType{message: message, basic: kerrors.ErrStreamingProtocol} | ||
} | ||
|
||
func (e *errType) Error() string { | ||
if e.basic == nil { | ||
return e.message | ||
} | ||
return fmt.Sprintf("%s - %s", e.basic.Error(), e.message) | ||
} | ||
|
||
func (e *errType) Is(target error) bool { | ||
return target == e || errors.Is(e.basic, target) | ||
} |
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,64 @@ | ||
/* | ||
* | ||
* 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. | ||
* | ||
* This file may have been modified by CloudWeGo authors. All CloudWeGo | ||
* Modifications are Copyright 2021 CloudWeGo Authors. | ||
*/ | ||
|
||
package errors | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cloudwego/kitex/internal/test" | ||
"github.com/cloudwego/kitex/pkg/kerrors" | ||
) | ||
|
||
var errs = []*errType{ | ||
// stream error | ||
ErrHTTP2Stream, | ||
ErrClosedWithoutTrailer, | ||
ErrMiddleHeader, | ||
ErrDecodeHeader, | ||
ErrRecvRstStream, | ||
ErrStreamDrain, | ||
ErrStreamFlowControl, | ||
ErrIllegalHeaderWrite, | ||
ErrStreamIsDone, | ||
ErrMaxStreamExceeded, | ||
// connection error | ||
ErrHTTP2Connection, | ||
ErrEstablishConnection, | ||
ErrHandleGoAway, | ||
ErrKeepAlive, | ||
ErrOperateHeaders, | ||
ErrNoActiveStream, | ||
ErrControlBufFinished, | ||
ErrNotReachable, | ||
ErrConnectionIsClosing, | ||
} | ||
|
||
func TestErrType(t *testing.T) { | ||
for _, err := range errs { | ||
test.Assert(t, errors.Is(err, kerrors.ErrStreamingProtocol), err) | ||
test.Assert(t, kerrors.IsKitexError(err), err) | ||
test.Assert(t, kerrors.IsStreamingError(err), err) | ||
test.Assert(t, strings.Contains(err.Error(), err.message), err) | ||
test.Assert(t, strings.Contains(err.Error(), err.basic.Error()), 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
Oops, something went wrong.