如何使用Arthas 分析 java.lang.reflect.UndeclaredThrowableException 异常来源

后端 潘老师 5个月前 (12-07) 116 ℃ (0) 扫码查看

本文主要讲解关于如何使用Arthas 分析 java.lang.reflect.UndeclaredThrowableException 异常来源相关内容,让我们来一起学习下吧!

背景

使用了https://square.github.io/retrofit/ 包装接口,响应值不正常的时候抛出一个异常堆栈
异常堆栈从哪里来的?不应该是 com.fasterxml.jackson.core.JsonParseException 异常? 怎么会被包装成了 java.lang.reflect.UndeclaredThrowableException?

Java.lang.reflect.UndeclaredThrowableException
    at com.sun.proxy.$Proxy60.tokenAuth(Unknown Source)
    at com.example.controller.AccountTestController.auth(AccountTestController.java:114)
....
Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'ok': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (okhttp3.ResponseBody$BomAwareReader); line: 1, column: 3]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2391)
....

探索思路

模拟不正常的响应值导致反序列化失败.. 自己写一个mock 服务,eg 返回对象 返回一个string

断点跟踪

JacksonResponseBodyConverter.convert 确实是 抛出了一个异常 com.fasterxml.jackson.core.JsonParseException,之后就没有之后了.. 这个方法不可行,这里面临一个问题,由于动态代理的代码是动态生成的没办法进行调试。

java.lang.reflect.UndeclaredThrowableException retrofit 内部哪里有使用

根据推断 内部只有一个地方使用,设置断点发现没有调用实例进入.. 发现这个思路走错了..

突破口 “动态代理“

at com.sun.proxy.$Proxy60.tokenAuth(Unknown Source) Unknown Source ? 利用工具反编译查看一下源码ok?

curl -L "https://arthas.aliyun.com/as.sh" >as.sh; chmod a+x as.sh; ./as.sh 安装arthas 执行命令 jad com.sun.proxy.$Proxy60 tokenAuth

反编译的结果

public final BaseResponse tokenAuth(AccountContextAccountContext,TokenAuthRequestTokenAuthRequest) {
        try {
            return (BaseResponse)this.h.invoke(this, m5, new Object[]{AccountContext,TokenAuthRequest});
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
    }
}

原来是动态代理这里进行了包装处理了RuntimeException 原样返回,Throwable 会进行包装

查询了一下相关bolg java.lang.reflect.UndeclaredThrowableException的解决 基本上就是动态代理的问题

总结

通过对于异常的深入探究,跟踪了解到了实现原理, “工欲善其事,必先利其器”。

以上就是关于如何使用Arthas 分析 java.lang.reflect.UndeclaredThrowableException 异常来源相关的全部内容,希望对你有帮助。欢迎持续关注潘子夜个人博客(www.panziye.com),学习愉快哦!


版权声明:本站文章,如无说明,均为本站原创,转载请注明文章来源。如有侵权,请联系博主删除。
本文链接:https://www.panziye.com/back/12196.html
喜欢 (0)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】