Skip to content

Commit

Permalink
modify FromError
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima committed Dec 28, 2023
1 parent c856838 commit 8de92d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
15 changes: 9 additions & 6 deletions pkg/hessian2/exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package exception

import (
"github.com/apache/dubbo-go-hessian2/java_exception"
"github.com/cloudwego/kitex/pkg/kerrors"
)

type Throwabler interface {
Expand All @@ -36,18 +35,22 @@ func NewException(detailMessage string) Throwabler {
//
// - If err is nil, it returns nil and false
//
// - If err is of type *kerrors.DetailedError, it would unwrap err to get the
// real cause. Then it would check cause whether implementing Throwabler. If
// yes, it returns Throwabler and true.
// - If err implents Unwrap(), it would unwrap err until getting the real cause.

Check warning on line 38 in pkg/hessian2/exception/exception.go

View workflow job for this annotation

GitHub Actions / compliant

"implents" should be "implements".
// Then it would check cause whether implementing Throwabler. If yes, it returns
// Throwabler and true.
//
// If not, it checks err whether implementing Throwabler directly. If yes,
// it returns Throwabler and true.
func FromError(err error) (Throwabler, bool) {
if err == nil {
return nil, false
}
if detailedErr, ok := err.(*kerrors.DetailedError); ok {
err = detailedErr.Unwrap()
for {
if wrapper, ok := err.(interface{ Unwrap() error }); ok {
err = wrapper.Unwrap()
} else {
break
}
}
if exception, ok := err.(Throwabler); ok {
return exception, true
Expand Down
11 changes: 11 additions & 0 deletions pkg/hessian2/exception/exception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package exception
import (
"testing"

"github.com/cloudwego/kitex/pkg/remote"

"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -58,6 +60,15 @@ func TestFromError(t *testing.T) {
assert.True(t, ok)
},
},
{
desc: "TransError wraps DetailedError, DetailedError wraps Throwabler",
inputErr: remote.NewTransError(remote.InternalError, kerrors.ErrRemoteOrNetwork.WithCause(NewException("FromError test"))),
expected: func(t *testing.T, exception Throwabler, ok bool) {
assert.Equal(t, "java.lang.Exception", exception.JavaClassName())
assert.Equal(t, "FromError test", exception.Error())
assert.True(t, ok)
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit 8de92d0

Please sign in to comment.