Skip to content

Commit

Permalink
feat: add PrependError for thriftgo (#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaost committed Jul 23, 2024
1 parent af00c6a commit d1c956c
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 90 deletions.
105 changes: 53 additions & 52 deletions pkg/generic/thrift/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package thrift
import (
"fmt"

"github.com/cloudwego/kitex/pkg/protocol/bthrift"
thrift "github.com/cloudwego/kitex/pkg/protocol/bthrift/apache"
)

Expand Down Expand Up @@ -110,18 +111,18 @@ func (p *TrafficEnv) Read(iprot thrift.TProtocol) (err error) {

return nil
ReadStructBeginError:
return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
ReadFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
return bthrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
ReadFieldError:
return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_TrafficEnv[fieldId]), err)
return bthrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_TrafficEnv[fieldId]), err)
SkipFieldError:
return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)
return bthrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)

ReadFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err)
return bthrift.PrependError(fmt.Sprintf("%T read field end error", p), err)
ReadStructEndError:
return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}

func (p *TrafficEnv) ReadField1(iprot thrift.TProtocol) error {
Expand Down Expand Up @@ -166,13 +167,13 @@ func (p *TrafficEnv) Write(oprot thrift.TProtocol) (err error) {
}
return nil
WriteStructBeginError:
return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
WriteFieldError:
return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err)
return bthrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err)
WriteFieldStopError:
return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err)
WriteStructEndError:
return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err)
}

func (p *TrafficEnv) writeField1(oprot thrift.TProtocol) (err error) {
Expand All @@ -187,9 +188,9 @@ func (p *TrafficEnv) writeField1(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err)
}

func (p *TrafficEnv) writeField2(oprot thrift.TProtocol) (err error) {
Expand All @@ -204,9 +205,9 @@ func (p *TrafficEnv) writeField2(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 2 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 2 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 2 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 2 end error: ", p), err)
}

func (p *TrafficEnv) String() string {
Expand Down Expand Up @@ -408,18 +409,18 @@ func (p *Base) Read(iprot thrift.TProtocol) (err error) {

return nil
ReadStructBeginError:
return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
ReadFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
return bthrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
ReadFieldError:
return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_Base[fieldId]), err)
return bthrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_Base[fieldId]), err)
SkipFieldError:
return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)
return bthrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)

ReadFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err)
return bthrift.PrependError(fmt.Sprintf("%T read field end error", p), err)
ReadStructEndError:
return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}

func (p *Base) ReadField1(iprot thrift.TProtocol) error {
Expand Down Expand Up @@ -535,13 +536,13 @@ func (p *Base) Write(oprot thrift.TProtocol) (err error) {
}
return nil
WriteStructBeginError:
return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
WriteFieldError:
return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err)
return bthrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err)
WriteFieldStopError:
return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err)
WriteStructEndError:
return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err)
}

func (p *Base) writeField1(oprot thrift.TProtocol) (err error) {
Expand All @@ -556,9 +557,9 @@ func (p *Base) writeField1(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err)
}

func (p *Base) writeField2(oprot thrift.TProtocol) (err error) {
Expand All @@ -573,9 +574,9 @@ func (p *Base) writeField2(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 2 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 2 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 2 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 2 end error: ", p), err)
}

func (p *Base) writeField3(oprot thrift.TProtocol) (err error) {
Expand All @@ -590,9 +591,9 @@ func (p *Base) writeField3(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 3 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 3 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 3 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 3 end error: ", p), err)
}

func (p *Base) writeField4(oprot thrift.TProtocol) (err error) {
Expand All @@ -607,9 +608,9 @@ func (p *Base) writeField4(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 4 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 4 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 4 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 4 end error: ", p), err)
}

func (p *Base) writeField5(oprot thrift.TProtocol) (err error) {
Expand All @@ -626,9 +627,9 @@ func (p *Base) writeField5(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 5 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 5 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 5 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 5 end error: ", p), err)
}

func (p *Base) writeField6(oprot thrift.TProtocol) (err error) {
Expand Down Expand Up @@ -658,9 +659,9 @@ func (p *Base) writeField6(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 6 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 6 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 6 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 6 end error: ", p), err)
}

func (p *Base) String() string {
Expand Down Expand Up @@ -788,18 +789,18 @@ func (p *BaseResp) Read(iprot thrift.TProtocol) (err error) {

return nil
ReadStructBeginError:
return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
ReadFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
return bthrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
ReadFieldError:
return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_BaseResp[fieldId]), err)
return bthrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_BaseResp[fieldId]), err)
SkipFieldError:
return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)
return bthrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)

ReadFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err)
return bthrift.PrependError(fmt.Sprintf("%T read field end error", p), err)
ReadStructEndError:
return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}

func (p *BaseResp) ReadField1(iprot thrift.TProtocol) error {
Expand Down Expand Up @@ -877,13 +878,13 @@ func (p *BaseResp) Write(oprot thrift.TProtocol) (err error) {
}
return nil
WriteStructBeginError:
return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
WriteFieldError:
return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err)
return bthrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err)
WriteFieldStopError:
return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err)
WriteStructEndError:
return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err)
}

func (p *BaseResp) writeField1(oprot thrift.TProtocol) (err error) {
Expand All @@ -898,9 +899,9 @@ func (p *BaseResp) writeField1(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err)
}

func (p *BaseResp) writeField2(oprot thrift.TProtocol) (err error) {
Expand All @@ -915,9 +916,9 @@ func (p *BaseResp) writeField2(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 2 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 2 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 2 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 2 end error: ", p), err)
}

func (p *BaseResp) writeField3(oprot thrift.TProtocol) (err error) {
Expand Down Expand Up @@ -947,9 +948,9 @@ func (p *BaseResp) writeField3(oprot thrift.TProtocol) (err error) {
}
return nil
WriteFieldBeginError:
return thrift.PrependError(fmt.Sprintf("%T write field 3 begin error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 3 begin error: ", p), err)
WriteFieldEndError:
return thrift.PrependError(fmt.Sprintf("%T write field 3 end error: ", p), err)
return bthrift.PrependError(fmt.Sprintf("%T write field 3 end error: ", p), err)
}

func (p *BaseResp) String() string {
Expand Down
28 changes: 0 additions & 28 deletions pkg/protocol/bthrift/apache/exception.go

This file was deleted.

54 changes: 53 additions & 1 deletion pkg/protocol/bthrift/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
package bthrift

import (
"errors"
"fmt"

thrift "github.com/cloudwego/kitex/pkg/protocol/bthrift/apache"
)

// ApplicationException represents the application exception decoder for replacing apache.TApplicationException
// ApplicationException is for replacing apache.TApplicationException
// it implements ThriftMsgFastCodec interface.
type ApplicationException struct {
t int32
Expand Down Expand Up @@ -177,3 +178,54 @@ func (e *ApplicationException) Error() string {
}
return fmt.Sprintf("unknown exception type [%d]", e.t)
}

// TransportException is for replacing apache.TransportException
// it implements ThriftMsgFastCodec interface.
type TransportException struct {
ApplicationException // same implementation ...
}

// NewTransportException ...
func NewTransportException(t int32, m string) *TransportException {
ret := TransportException{}
ret.t = t
ret.m = m
return &ret
}

// ProtocolException is for replacing apache.ProtocolException
// it implements ThriftMsgFastCodec interface.
type ProtocolException struct {
ApplicationException // same implementation ...
}

// NewTransportException ...
func NewProtocolException(t int32, m string) *ProtocolException {
ret := ProtocolException{}
ret.t = t
ret.m = m
return &ret
}

// Generic Thrift exception with TypeId method
type tException interface {
Error() string
TypeId() int32
}

// Prepends additional information to an error without losing the Thrift exception interface
func PrependError(prepend string, err error) error {
if t, ok := err.(*TransportException); ok {
return NewTransportException(t.TypeID(), prepend+t.Error())
}
if t, ok := err.(*ProtocolException); ok {
return NewProtocolException(t.TypeID(), prepend+err.Error())
}
if t, ok := err.(*ApplicationException); ok {
return NewApplicationException(t.TypeID(), prepend+t.Error())
}
if t, ok := err.(tException); ok { // apache thrift exception?
return NewApplicationException(t.TypeId(), prepend+t.Error())
}
return errors.New(prepend + err.Error())
}
Loading

0 comments on commit d1c956c

Please sign in to comment.