Skip to content

Commit

Permalink
feat: better unwrapping of java exception
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima committed Dec 27, 2023
1 parent 6725aa7 commit d531564
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pkg/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package dubbo
import (
"context"
"fmt"

Check failure on line 24 in pkg/codec.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
hessian2_exception "github.com/kitex-contrib/codec-dubbo/pkg/hessian2/exception"

"github.com/kitex-contrib/codec-dubbo/registries"

Expand Down Expand Up @@ -182,12 +183,12 @@ func (m *DubboCodec) encodeExceptionPayload(ctx context.Context, message remote.
if !ok {
return nil, fmt.Errorf("%v exception does not implement Error", data)
}
if exception, ok := data.(hessian2.Throwabler); ok {
if exception, ok := data.(hessian2_exception.Throwabler); ok {
if err := encoder.Encode(exception); err != nil {
return nil, err
}
} else {
if err := encoder.Encode(hessian2.NewException(errRaw.Error())); err != nil {
if err := encoder.Encode(hessian2_exception.NewException(errRaw.Error())); err != nil {
return nil, err
}
}
Expand Down
30 changes: 28 additions & 2 deletions pkg/hessian2/exception.go → pkg/hessian2/exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
* limitations under the License.
*/

package hessian2
package exception

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

type Throwabler interface {
java_exception.Throwabler
Expand All @@ -28,3 +31,26 @@ type Throwabler interface {
func NewException(detailMessage string) Throwabler {
return java_exception.NewException(detailMessage)
}

// FromError extracts Throwabler from passed err.
//
// - 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 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()
}
if exception, ok := err.(Throwabler); ok {
return exception, true
}
return nil, false
}
68 changes: 68 additions & 0 deletions pkg/hessian2/exception/exception_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2023 CloudWeGo Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package exception

import (

Check failure on line 22 in pkg/hessian2/exception/exception_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/stretchr/testify/assert"
"testing"

Check failure on line 25 in pkg/hessian2/exception/exception_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
)

func TestFromError(t *testing.T) {
tests := []struct {
desc string
inputErr error
expected func(t *testing.T, exception Throwabler, ok bool)
}{
{
desc: "nil err",
inputErr: nil,
expected: func(t *testing.T, exception Throwabler, ok bool) {
assert.Nil(t, exception)
assert.False(t, ok)
},
},
{
desc: "Throwabler err",
inputErr: 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)
},
},
{
desc: "DetailedError wraps Throwabler",
inputErr: 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 {
t.Run(test.desc, func(t *testing.T) {
exception, ok := FromError(test.inputErr)
test.expected(t, exception, ok)
})
}
}

0 comments on commit d531564

Please sign in to comment.