From 21e259860e97aae5dbbadb7a788210962d1b7376 Mon Sep 17 00:00:00 2001 From: alice <90381261+alice-yyds@users.noreply.github.com> Date: Mon, 13 Jan 2025 14:21:35 +0800 Subject: [PATCH] doc: sync Stream Error Handling (#1194) --- .../Kitex+StreamX+-+Stream+Error+Handling.md | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 content/en/docs/kitex/Tutorials/basic-feature/Kitex+StreamX+-+Stream+Error+Handling.md diff --git a/content/en/docs/kitex/Tutorials/basic-feature/Kitex+StreamX+-+Stream+Error+Handling.md b/content/en/docs/kitex/Tutorials/basic-feature/Kitex+StreamX+-+Stream+Error+Handling.md new file mode 100644 index 0000000000..aab5ce99fc --- /dev/null +++ b/content/en/docs/kitex/Tutorials/basic-feature/Kitex+StreamX+-+Stream+Error+Handling.md @@ -0,0 +1,83 @@ +--- +title: "Stream Error Handling" +date: 2025-01-13 +weight: 1 +keywords: ["Stream Error Handling"] +description: "" +--- + +## Preface + +Unlike RPC, stream errors can occur at any time during stream processing. For example, a server can return an error after sending multiple messages. However, once a stream has sent an error, it cannot send any more messages. + +## Error type + +### Business exception + +**Usage example** : For example, in the ChatGPT scenario, we need to constantly check whether the user account balance can continue to call the large model to generate returns. + +Server implementation: + +```go +func (si *streamingService) ServerStreamWithErr(ctx context.Context, req *Request, stream streamx.ServerStreamingServer[Response]) error { + // 检查用户账户余额 + for isHasBalance (req.UserId) { + stream.Send(ctx, res) + } + // 返回用户余额不足错误 + bizErr := kerrors.NewBizStatusErrorWithExtra( + 10001, "insufficient user balance", map[string]string{"testKey": "testVal"}, + ) + return bizErr +} +``` + +Client implementation: + +```go +svrStream, err = streamClient.ServerStreamWithErr(ctx, req) + +var err error +for { + res, err = stream.Recv(ctx) + if err != nil { + break + } +} +bizErr, ok := kerrors.FromBizStatusError(err) +if ok { + println(bizErr.BizStatusCode(), bizErr.BizMessage(), bizErr.BizExtra()) +} +``` + +### Other errors + +If the Error returned by the Server is a non-business exception, the framework will be uniformly encapsulated as `(* thrift.ApplicationException) `. At this time, only the error Message can be obtained. + +Server implementation: + +```go +func (si *streamingService) ServerStreamWithErr(ctx context.Context, req *Request, stream streamx.ServerStreamingServer[Response]) error { + // ... + return errors.New("test error") +} +``` + +Client implementation: + +```go +svrStream, err = streamClient.ServerStreamWithErr(ctx, req) +test.Assert(t, err == nil, err) + +var err error +for { + res, err = stream.Recv(ctx) + if err != nil { + break + } +} +ex, ok := err.(*thrift.ApplicationException) +if ok { + println(ex.TypeID(), ex.Msg()) +} +```