Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ftr: catch exceptions that user defined #208

Merged
merged 9 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@
package hessian

import (
"io/ioutil"
"log"
"os"
"os/exec"
"reflect"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go-hessian2/java_exception"
)

const (
hessianJar = "test_hessian/target/test_hessian-1.0.0.jar"
testString = "hello, world! 你好,世界!"
Expand Down Expand Up @@ -127,3 +136,28 @@ func testDecodeFrameworkFunc(t *testing.T, method string, expected func(interfac
}
expected(r)
}

func TestRuntimeException(t *testing.T) {
// this byte slice in the file is generated by java
c := getByteFromFile("RuntimeException.txt")
decoder := NewDecoder(c)
r, err := decoder.Decode()
assert.Nil(t, err)
_, j := r.(*java_exception.RuntimeException)
assert.True(t, j)
}

func TestBizException(t *testing.T) {
// this byte slice in the file is generated by java
c := getByteFromFile("BizDddException.txt")
decoder := NewDecoder(c)
r, err := decoder.Decode()
assert.Nil(t, err)
_, j := r.(*UnknownException)
assert.True(t, j)
}

func getByteFromFile(fname string) []byte {
c, _ := ioutil.ReadFile("test_resource/" + fname)
return c
}
6 changes: 3 additions & 3 deletions java_exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ func (e Exception) GetStackTrace() []StackTraceElement {
return e.StackTrace
}

////////////////////////////
/////////////////////////////
// StackTraceElement
////////////////////////////
/////////////////////////////

type StackTraceElement struct {
DeclaringClass string
MethodName string
FileName string
LineNumber int
LineNumber int32
}

//JavaClassName java fully qualified path
Expand Down
88 changes: 88 additions & 0 deletions java_unknown_exception.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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 hessian

import (
"fmt"
"sync"
)

import (
"github.com/apache/dubbo-go-hessian2/java_exception"
)

var mutex sync.Mutex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think should make this variable name more meaningful, like getExceptionMutex, checkExceptionMutex etc?


func checkAndGetException(cls classInfo) (structInfo, bool) {

if len(cls.fieldNameList) < 4 {
return structInfo{}, false
}
var (
throwable structInfo
ok bool
)
var count = 0
for _, item := range cls.fieldNameList {
if item == "detailMessage" || item == "suppressedExceptions" || item == "stackTrace" || item == "cause" {
count++
}
}
// if have these 4 fields, it is throwable struct
if count == 4 {
mutex.Lock()
defer mutex.Unlock()
if throwable, ok = getStructInfo(cls.javaName); ok {
wongoo marked this conversation as resolved.
Show resolved Hide resolved
return throwable, true
}
RegisterPOJO(newBizException(cls.javaName))
if throwable, ok = getStructInfo(cls.javaName); ok {
return throwable, true
}
}
return throwable, count == 4
}

type UnknownException struct {
SerialVersionUID int64
DetailMessage string
SuppressedExceptions []java_exception.Throwabler
StackTrace []java_exception.StackTraceElement
Cause java_exception.Throwabler
name string
}

// NewThrowable is the constructor
func newBizException(name string) *UnknownException {
return &UnknownException{name: name, StackTrace: []java_exception.StackTraceElement{}}
}

// Error output error message
func (e UnknownException) Error() string {
return fmt.Sprintf("throw %v : %v", e.name, e.DetailMessage)
}

//JavaClassName java fully qualified path
func (e UnknownException) JavaClassName() string {
return e.name
}

// equals to getStackTrace in java
func (e UnknownException) GetStackTrace() []java_exception.StackTraceElement {
return e.StackTrace
}
45 changes: 45 additions & 0 deletions java_unknown_exception_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 hessian

import (
"testing"
)
import (
"github.com/stretchr/testify/assert"
)

func TestCheckAndGetException(t *testing.T) {
clazzInfo1 := classInfo{
javaName: "com.test.UserDefinedException",
fieldNameList: []string{"detailMessage", "code", "suppressedExceptions", "stackTrace", "cause"},
}
s, b := checkAndGetException(clazzInfo1)
assert.True(t, b)

assert.Equal(t, s.javaName, "com.test.UserDefinedException")
assert.Equal(t, s.goName, "hessian.UnknownException")

clazzInfo2 := classInfo{
javaName: "com.test.UserDefinedException",
fieldNameList: []string{"detailMessage", "code", "suppressedExceptions", "cause"},
}
s, b = checkAndGetException(clazzInfo2)
assert.False(t, b)
assert.Equal(t, s, structInfo{})
}
5 changes: 5 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import (
import (
perrors "github.com/pkg/errors"
)
import (
"github.com/apache/dubbo-go-hessian2/java_exception"
)

var (
listTypeNameMapper = &sync.Map{}
Expand All @@ -46,6 +49,8 @@ var (
"date": reflect.TypeOf(time.Time{}),
"object": reflect.TypeOf([]Object{}).Elem(),
"java.lang.Object": reflect.TypeOf([]Object{}).Elem(),
// exception field StackTraceElement
"java.lang.StackTraceElement": reflect.TypeOf([]*java_exception.StackTraceElement{}).Elem(),
}
)

Expand Down
4 changes: 4 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ func (d *Decoder) getStructDefByIndex(idx int) (reflect.Type, classInfo, error)
cls = d.classInfoList[idx]
s, ok = getStructInfo(cls.javaName)
if !ok {
// exception
if s, ok = checkAndGetException(cls); ok {
return s.typ, cls, nil
}
if !d.isSkip {
err = perrors.Errorf("can not find go type name %s in registry", cls.javaName)
}
Expand Down
4 changes: 4 additions & 0 deletions test_resource/BizDddException.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
C0*com.alibaba.middleware.hsf.BizDddException�detailMessagecause
stackTracesuppressedExceptions`test biz ddd exceptQ�V[java.lang.StackTraceElement�?Cjava.lang.StackTraceElement�declaringClass
methodNamefileName
lineNumbera04com.alibaba.middleware.hsf.provider.HelloServiceImplthrowBizExcepHelloServiceImpl.java�5a0&sun.reflect.GeneratedMethodAccessor161invokeN�a0(sun.reflect.DelegatingMethodAccessorImplinvoke0!DelegatingMethodAccessorImpl.java�ajava.lang.reflect.Methodinvoke Method.java��a09com.taobao.hsf.remoting.provider.ReflectInvocationHandlerhandleRequest0ReflectInvocationHandler.java�Sa09com.taobao.hsf.remoting.provider.ReflectInvocationHandlerinvokeReflectInvocationHandler.javaȣa0Gcom.taobao.hsf2dubbo.DubboServerFilterAsyncInvocationHandlerInterceptorinvoke07DubboServerFilterAsyncInvocationHandlerInterceptor.java�6a0-com.taobao.hsf.filter.FilterInvocationHandlerinvokeFilterInvocationHandler.java�a0:com.taobao.hsf.invocation.filter.RPCFilterBuilder$TailNodeinvokeRPCFilterBuilder.javaȥa0&com.taobao.hsf.debug.DebugServerFilterinvokeDebugServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0<com.taobao.hsf.common.filter.BidirectionServerResponseFilterinvoke0$BidirectionServerResponseFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga01com.taobao.hsf.unit.service.impl.UnitServerFilterinvokeUnitServerFilter.java�Ba0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga05com.taobao.hsf.region.service.impl.RegionServerFilterinvokeRegionServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga02com.taobao.hsf.plugins.octopus.OctopusServerFilterinvokeOctopusServerFilter.java�Ha0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0,com.taobao.hsf.plugins.spas.SpasServerFilterinvokeSpasServerFilter.java�La0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0*com.taobao.hsf.plugins.txc.TXCServerFilterinvokeTXCServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0,com.taobao.hsf.tps.component.TPSServerFilterinvokeTPSServerFilter.java�Da0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0;com.taobao.hsf.invocation.stats.InvocationStatsServerFilterinvoke0 InvocationStatsServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga08com.taobao.hsf.monitor.log.filter.MonitorLogServerFilterinvokeMonitorLogServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0,com.taobao.hsf.profiler.ProfilerServerFilterinvokeProfilerServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga04com.taobao.hsf.plugins.eagleeye.EagleEyeServerFilterinvokeEagleEyeServerFilter.java�Da0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0%com.taobao.hsf.filter.QosServerFilterinvokeQosServerFilter.java�6a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0-com.taobao.hsf.rpc.server.MethodAbsenceFilterinvokeMethodAbsenceFilter.java�0a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0$com.taobao.hsf.rpc.server.EchoFilterinvokeEchoFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga08com.taobao.hsf.rpc.generic.GenericInvocationServerFilterinvoke0"GenericInvocationServerFilter.java�ka0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0-com.taobao.hsf2dubbo.DubboGenericServerFilterinvokeDubboGenericServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0"com.taobao.hsf.top.TopServerFilterinvokeTopServerFilter.java�Wa0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0.com.taobao.hsf.rpc.server.ServiceAbsenceFilterinvokeServiceAbsenceFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0/com.taobao.hsf.common.filter.CommonServerFilterinvokeCommonServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0;com.taobao.hsf.common.filter.BidirectionServerRequestFilterinvoke0#BidirectionServerRequestFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga08com.taobao.hsf2dubbo.context.DubboRPCContextServerFilterinvoke0 DubboRPCContextServerFilter.java�0a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0-com.taobao.hsf.context.RPCContextServerFilterinvokeRPCContextServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0:com.taobao.hsf.invocation.filter.RPCFilterBuilder$HeadNodeinvokeRPCFilterBuilder.javaȆa08com.taobao.hsf.invocation.filter.FilterInvocationHandlerinvokeFilterInvocationHandler.java�a0?com.taobao.hsf.remoting.provider.ServerContextInvocationHandlerinvoke0#ServerContextInvocationHandler.java�a02com.taobao.hsf.remoting.provider.ProviderProcessorhandleRequestProviderProcessor.java�Fa09com.taobao.hsf.io.remoting.hsf.message.HSFServerHandler$1runHSFServerHandler.javaȽa0'java.util.concurrent.ThreadPoolExecutor runWorkerThreadPoolExecutor.javàa0.java.util.concurrent.ThreadPoolExecutor$WorkerrunThreadPoolExecutor.java�sajava.lang.Threadrun Thread.java�Tp0&java.util.Collections$UnmodifiableList
Expand Down
5 changes: 5 additions & 0 deletions test_resource/RuntimeException.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Cjava.lang.RuntimeException�detailMessagecause
stackTracesuppressedExceptions` test exceptQ�V[java.lang.StackTraceElement�?Cjava.lang.StackTraceElement�declaringClass
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why display not normal ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

存储在里面是以byte数组形式。

methodNamefileName
lineNumbera04com.alibaba.middleware.hsf.provider.HelloServiceImpl
throwExcepHelloServiceImpl.java�0a0&sun.reflect.GeneratedMethodAccessor158invokeN�a0(sun.reflect.DelegatingMethodAccessorImplinvoke0!DelegatingMethodAccessorImpl.java�ajava.lang.reflect.Methodinvoke Method.java��a09com.taobao.hsf.remoting.provider.ReflectInvocationHandlerhandleRequest0ReflectInvocationHandler.java�Sa09com.taobao.hsf.remoting.provider.ReflectInvocationHandlerinvokeReflectInvocationHandler.javaȣa0Gcom.taobao.hsf2dubbo.DubboServerFilterAsyncInvocationHandlerInterceptorinvoke07DubboServerFilterAsyncInvocationHandlerInterceptor.java�6a0-com.taobao.hsf.filter.FilterInvocationHandlerinvokeFilterInvocationHandler.java�a0:com.taobao.hsf.invocation.filter.RPCFilterBuilder$TailNodeinvokeRPCFilterBuilder.javaȥa0&com.taobao.hsf.debug.DebugServerFilterinvokeDebugServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0<com.taobao.hsf.common.filter.BidirectionServerResponseFilterinvoke0$BidirectionServerResponseFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga01com.taobao.hsf.unit.service.impl.UnitServerFilterinvokeUnitServerFilter.java�Ba0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga05com.taobao.hsf.region.service.impl.RegionServerFilterinvokeRegionServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga02com.taobao.hsf.plugins.octopus.OctopusServerFilterinvokeOctopusServerFilter.java�Ha0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0,com.taobao.hsf.plugins.spas.SpasServerFilterinvokeSpasServerFilter.java�La0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0*com.taobao.hsf.plugins.txc.TXCServerFilterinvokeTXCServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0,com.taobao.hsf.tps.component.TPSServerFilterinvokeTPSServerFilter.java�Da0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0;com.taobao.hsf.invocation.stats.InvocationStatsServerFilterinvoke0 InvocationStatsServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga08com.taobao.hsf.monitor.log.filter.MonitorLogServerFilterinvokeMonitorLogServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0,com.taobao.hsf.profiler.ProfilerServerFilterinvokeProfilerServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga04com.taobao.hsf.plugins.eagleeye.EagleEyeServerFilterinvokeEagleEyeServerFilter.java�Da0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0%com.taobao.hsf.filter.QosServerFilterinvokeQosServerFilter.java�6a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0-com.taobao.hsf.rpc.server.MethodAbsenceFilterinvokeMethodAbsenceFilter.java�0a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0$com.taobao.hsf.rpc.server.EchoFilterinvokeEchoFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga08com.taobao.hsf.rpc.generic.GenericInvocationServerFilterinvoke0"GenericInvocationServerFilter.java�ka0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0-com.taobao.hsf2dubbo.DubboGenericServerFilterinvokeDubboGenericServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0"com.taobao.hsf.top.TopServerFilterinvokeTopServerFilter.java�Wa0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0.com.taobao.hsf.rpc.server.ServiceAbsenceFilterinvokeServiceAbsenceFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0/com.taobao.hsf.common.filter.CommonServerFilterinvokeCommonServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0;com.taobao.hsf.common.filter.BidirectionServerRequestFilterinvoke0#BidirectionServerRequestFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga08com.taobao.hsf2dubbo.context.DubboRPCContextServerFilterinvoke0 DubboRPCContextServerFilter.java�0a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0-com.taobao.hsf.context.RPCContextServerFilterinvokeRPCContextServerFilter.java�a0.com.taobao.hsf.invocation.filter.RPCFilterNodeinvokeRPCFilterNode.java�Ga0:com.taobao.hsf.invocation.filter.RPCFilterBuilder$HeadNodeinvokeRPCFilterBuilder.javaȆa08com.taobao.hsf.invocation.filter.FilterInvocationHandlerinvokeFilterInvocationHandler.java�a0?com.taobao.hsf.remoting.provider.ServerContextInvocationHandlerinvoke0#ServerContextInvocationHandler.java�a02com.taobao.hsf.remoting.provider.ProviderProcessorhandleRequestProviderProcessor.java�Fa09com.taobao.hsf.io.remoting.hsf.message.HSFServerHandler$1runHSFServerHandler.javaȽa0'java.util.concurrent.ThreadPoolExecutor runWorkerThreadPoolExecutor.javàa0.java.util.concurrent.ThreadPoolExecutor$WorkerrunThreadPoolExecutor.java�sajava.lang.Threadrun Thread.java�Tp0&java.util.Collections$UnmodifiableList
Expand Down