Skip to content

改进错误处理:避免异常被静默吞掉或未正确传播 - #4083

Open
devin-ai-integration[bot] wants to merge 1 commit into
developfrom
devin/1785470517-improve-error-handling
Open

改进错误处理:避免异常被静默吞掉或未正确传播#4083
devin-ai-integration[bot] wants to merge 1 commit into
developfrom
devin/1785470517-improve-error-handling

Conversation

@devin-ai-integration

Copy link
Copy Markdown

Summary

排查各模块 main 源码中被静默吞掉的异常(空 catchprintStackTrace 后返回 null),改为向上传播或至少记录日志。重点是几个「失败时静默返回 null / 不完整数据」的路径,调用方无法区分成功与失败:

  • FileUtils.imageToBase64ByStreamIOException 之前被 printStackTrace 后返回 null;现改为 try-with-resources 并抛出 WxRuntimeException(同时修掉 finally 里再次吞异常的 close 逻辑)。
  • AesUtils.HMACSHA256:签名计算失败返回 null,会让 createSign 产出空签名后被当作正常请求发送;现抛出 WxRuntimeException
  • RequestUtils.readData(V3 回调读取请求体):IOException 被吞掉后返回已读到的部分内容,会导致验签/解析拿到截断报文;现抛出 WxRuntimeException
  • PartnerTransferServiceImpl.spDayEndBalancecatch (Exception) { printStackTrace(); } return null; 改为 catch (WxPayException e) { throw new WxRuntimeException(e); },接口 Javadoc 同步修正(接口签名未加 checked 异常,保持源码/二进制兼容)。
  • WxMpMemberCardServiceImpl.getActivatePluginParam:解析失败返回 null 改为抛 WxErrorException(方法本就声明该异常)。
  • DefaultApacheHttpClientBuilder 空闲连接监控线程:catch (InterruptedException ignore) {} 改为记录 debug 日志并 Thread.currentThread().interrupt() 恢复中断标志。

以下场景的「继续执行」是有意为之,仅补日志、不改变行为:WxPayOrderNotifyResultConverter.setFieldValue(单字段反射赋值失败)、WxMaCodeServiceImpl 删除临时二维码文件失败、VerifierBuilderpayBaseUrl 不是合法 URI、PublicCertificateVerifier 平台证书验签失败降级为公钥验签。

行为变更提示:上述前 5 处此前返回 null 或不完整数据,现在会抛出未检查异常 WxRuntimeExceptionWxErrorException 处为已声明的受检异常);这是把原本被掩盖的失败暴露出来,未改变任何公共方法签名。

测试

  • mvn -pl weixin-java-common,weixin-java-pay,weixin-java-mp,weixin-java-miniapp -am -DskipTests package:BUILD SUCCESS。
  • 新增/扩展回归测试:FileUtilsTest#testImageToBase64ByStreamThrowsOnIoError、新增 AesUtilsTest。仓库根 POM 中 surefire 被配置为 <skip>true</skip>,故通过 TestNG 命令行在对应模块 classpath 下执行:FileUtilsTest 4/4 通过,AesUtilsTest 2/2 通过。
  • 未运行需要真实微信凭据/网络的测试。

Link to Devin session: https://app.devin.ai/sessions/0e0c592526ac4c158640d8f62c2a902a
Requested by: @binarywang

- FileUtils.imageToBase64ByStream 读取失败时抛出 WxRuntimeException,不再 printStackTrace 后返回 null
- AesUtils.HMACSHA256 签名计算失败时抛出 WxRuntimeException,不再返回 null 导致签名为空
- RequestUtils.readData 读取回调请求体失败时抛出异常,不再静默返回不完整内容
- PartnerTransferServiceImpl.spDayEndBalance 传播查询异常,不再返回 null
- WxMpMemberCardServiceImpl.getActivatePluginParam 解析失败时抛出 WxErrorException
- DefaultApacheHttpClientBuilder 空闲连接监控线程被中断时恢复中断标志
- 支付回调转换器、临时二维码文件删除、payBaseUrl 解析失败改为记录日志

新增 FileUtilsTest、AesUtilsTest 回归测试

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@binarywang binarywang self-assigned this Jul 31, 2026
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant