diff --git a/pkg/client/client.go b/pkg/client/client.go index d77a07b..f7dc8e7 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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 } @@ -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) diff --git a/pkg/client/thrift_generic.go b/pkg/client/thrift_generic.go index 369b9cf..528cf78 100644 --- a/pkg/client/thrift_generic.go +++ b/pkg/client/thrift_generic.go @@ -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" @@ -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...)) diff --git a/pkg/log/log.go b/pkg/log/log.go index 438902a..9fb1d62 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -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...) } @@ -58,6 +62,10 @@ func Fail(data ...interface{}) { defaultLogger.Fail(data...) } +func Print(data ...interface{}) { + defaultLogger.Print(data...) +} + // For file output type File struct{}