feat(exception): GlobalExceptionHandler 新增 WechatApiException handler
解决问题: - WechatApiException 抛出后未统一处理,会冒泡到 Spring 默认错误页(500 HTML) - 前端拿不到结构化错误信息 改动: - 新增 @ExceptionHandler(WechatApiException.class) 方法 - code=400 -> Result.badRequest()(微信业务错误) - code=502 -> Result.error(502, message)(上游/网络错误) - 其他(code=500) -> Result.error(message)(内部 JSON 解析错误) - 日志记录 code 和 message(已确保不含敏感信息) - 风格与现有 BusinessException handler 保持一致
This commit is contained in:
+72
@@ -0,0 +1,72 @@
|
||||
package com.emotion.exception;
|
||||
|
||||
import com.emotion.common.Result;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* GlobalExceptionHandler 的 WechatApiException handler 单元测试
|
||||
*
|
||||
* 验证:
|
||||
* 1. 400 业务错误 → "微信授权失败,请重试"
|
||||
* 2. 500 内部错误 → "微信登录失败,请重试"
|
||||
* 3. 502 上游错误 → "微信服务暂不可用,请稍后重试"
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class GlobalExceptionHandlerWechatTest {
|
||||
|
||||
@InjectMocks
|
||||
private GlobalExceptionHandler handler;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.setRequestURI("/api/wechat/login");
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleWechatApiException_Code400_ReturnsBadRequestWithFriendlyMessage() {
|
||||
WechatApiException ex = new WechatApiException(400, "errcode=40029, errmsg=invalid code (internal detail)", "");
|
||||
|
||||
Result<?> result = handler.handleWechatApiException(ex, request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("微信授权失败,请重试", result.getMessage());
|
||||
// Ensure internal detail is NOT exposed to frontend
|
||||
assertFalse(result.getMessage().contains("errcode"));
|
||||
assertFalse(result.getMessage().contains("40029"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleWechatApiException_Code500_ReturnsInternalErrorWithFriendlyMessage() {
|
||||
WechatApiException ex = new WechatApiException(500, "JSON parse failure at line 1 (internal detail)", "");
|
||||
|
||||
Result<?> result = handler.handleWechatApiException(ex, request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("微信登录失败,请重试", result.getMessage());
|
||||
assertFalse(result.getMessage().contains("JSON"));
|
||||
assertFalse(result.getMessage().contains("parse"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleWechatApiException_Code502_ReturnsBadGatewayWithFriendlyMessage() {
|
||||
WechatApiException ex = new WechatApiException(502, "Connection refused: api.weixin.qq.com (internal detail)", "");
|
||||
|
||||
Result<?> result = handler.handleWechatApiException(ex, request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("微信服务暂不可用,请稍后重试", result.getMessage());
|
||||
assertFalse(result.getMessage().contains("Connection"));
|
||||
assertFalse(result.getMessage().contains("refused"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user