Skip to content

Commit

Permalink
fix biz_error response parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzhiter committed May 1, 2024
1 parent 7e4f427 commit 47b6250
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
11 changes: 6 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package client

import (
"fmt"

"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/kitex-contrib/kitexcall/pkg/config"
"github.com/kitex-contrib/kitexcall/pkg/errors"
"github.com/kitex-contrib/kitexcall/pkg/log"
)

type Client interface {
Init(conf *config.Config) error
Call() error
HandleBizError(bizErr kerrors.BizStatusErrorIface) error
Output() error
}

Expand All @@ -21,12 +19,15 @@ func InvokeRPC(c Client, conf *config.Config) error {
return errors.New(errors.ClientError, "client init failed: %v", err)
}

// var isBizErr bool
// var bizErr kerrors.BizStatusErrorIface
if err := c.Call(); err != nil {
// Handle Biz error
bizErr, isBizErr := kerrors.FromBizStatusError(err)
if isBizErr {
log.Success(fmt.Sprintf("Biz error: StatusCode: %v, Message: %v",
bizErr.BizStatusCode(), bizErr.BizMessage()))
if err := c.HandleBizError(bizErr); err != nil {
errors.New(errors.OutputError, "Response parse error")
}
return nil
}
return errors.New(errors.ServerError, "RPC call failed: %v", err)
Expand Down
13 changes: 13 additions & 0 deletions pkg/client/thrift_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package client

import (
"context"
"fmt"

"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/client/callopt"
"github.com/cloudwego/kitex/client/genericclient"
"github.com/cloudwego/kitex/pkg/generic"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/transport"
"github.com/kitex-contrib/kitexcall/pkg/config"
"github.com/kitex-contrib/kitexcall/pkg/log"
Expand Down Expand Up @@ -77,6 +79,17 @@ func (c *ThriftGeneric) Output() error {
return nil
}

func (c *ThriftGeneric) HandleBizError(bizErr kerrors.BizStatusErrorIface) error {
result, err := log.Format(log.JF, bizErr.BizMessage())
if err != nil {
return err
}
log.Success(fmt.Sprintf("Biz error: \nStatusCode: %v\nMessage: \n",
bizErr.BizStatusCode()))
log.Print(result)
return nil
}

func (c *ThriftGeneric) BuildClientOptions() ([]client.Option, error) {
var opts []client.Option
opts = append(opts, client.WithHostPorts(c.conf.Endpoint...))
Expand Down
14 changes: 11 additions & 3 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,28 @@ func SetDefaultLogger(l Logger) {
defaultLogger = l
}

func (c *Logger) Success(data ...interface{}) error {
func (l *Logger) Success(data ...interface{}) error {
defaultLogger.Println(os.Stdout, "\033[32mCall Status: Success\033[0m")
defaultLogger.Println(os.Stdout, data...)
return nil
}

func (c *Logger) Fail(data ...interface{}) error {
func (l *Logger) Fail(data ...interface{}) error {
defaultLogger.Println(os.Stderr, "\033[31mCall Status: Failed\033[0m")
defaultLogger.Println(os.Stderr, data...)
return nil
}

func (c *Logger) Info(data ...interface{}) {
func (l *Logger) Info(data ...interface{}) {
if Verbose {
defaultLogger.Printf(os.Stdout, "[INFO]:%v\n", data...)
}
}

func (l *Logger) Print(data ...interface{}) {
defaultLogger.Println(os.Stdout, data...)
}

func Info(data ...interface{}) {
defaultLogger.Info(data...)
}
Expand All @@ -58,6 +62,10 @@ func Fail(data ...interface{}) {
defaultLogger.Fail(data...)
}

func Print(data ...interface{}) {
defaultLogger.Print(data...)
}

// For file output
type File struct{}

Expand Down

0 comments on commit 47b6250

Please sign in to comment.