1
0
mirror of synced 2026-10-18 05:16:10 +08:00

改进错误处理:避免异常被静默吞掉

- 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>
This commit is contained in:
Binary Wang
2026-07-31 04:01:57 +00:00
parent 6014c833bc
commit 0c7b6713a8
13 changed files with 78 additions and 39 deletions

View File

@@ -1,5 +1,6 @@
package me.chanjar.weixin.common.util.fs;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.apache.commons.io.IOUtils;
import java.io.File;
@@ -55,24 +56,16 @@ public class FileUtils {
*
* @param in 文件流
* @return base64编码
* @throws WxRuntimeException 读取文件流失败时抛出
*/
public static String imageToBase64ByStream(InputStream in) {
// 读取图片字节数组
try {
try (InputStream input = in) {
// 返回Base64编码过的字节数组字符串
return Base64.getEncoder().encodeToString(IOUtils.toByteArray(in));
return Base64.getEncoder().encodeToString(IOUtils.toByteArray(input));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
throw new WxRuntimeException(e);
}
return null;
}
}

View File

@@ -318,7 +318,9 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
TimeUnit.MILLISECONDS);
}
}
} catch (InterruptedException ignore) {
} catch (InterruptedException e) {
log.debug("空闲连接监控线程被中断,退出监控", e);
Thread.currentThread().interrupt();
}
}

View File

@@ -1,5 +1,6 @@
package me.chanjar.weixin.common.util.fs;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.apache.commons.io.IOUtils;
import org.testng.annotations.Test;
@@ -13,6 +14,7 @@ import java.util.Base64;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class FileUtilsTest {
@@ -59,4 +61,17 @@ public class FileUtilsTest {
String result = FileUtils.imageToBase64ByStream(chunked);
assertThat(result).isEqualTo(Base64.getEncoder().encodeToString(original));
}
@Test
public void testImageToBase64ByStreamThrowsOnIoError() {
InputStream failing = new InputStream() {
@Override
public int read() throws IOException {
throw new IOException("read failed");
}
};
assertThatThrownBy(() -> FileUtils.imageToBase64ByStream(failing))
.isInstanceOf(WxRuntimeException.class)
.hasCauseInstanceOf(IOException.class);
}
}