Commit b306d2c7 authored by wujipeng's avatar wujipeng
Browse files

全局异常处理

parent 1fe1fc8b
package com.platform.business.exception;
import com.platform.exception.BusinessException;
import com.platform.model.resp.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 业务服务统一业务异常处理器。
*
* <p>{@code BusinessException} 来自公共依赖的 {@code com.platform.exception} 包,
* 而业务服务默认只扫描 {@code com.platform.business} 及其子包。因此公共依赖中的全局
* 异常处理器不会自动注册到当前 Spring 容器,本处理器放在业务服务扫描范围内,确保
* Controller 或 Service 抛出的业务异常都能转换成项目统一的 {@link Result} 响应。</p>
*
* <p>业务异常使用 HTTP 200 返回,并通过响应体中的业务码表示失败。这样前端和网关
* 不会把它当作无响应体的服务端故障处理,同时仍可通过 {@code code} 和 {@code msg}
* 准确识别并展示业务错误。</p>
*/
@RestControllerAdvice
public class BusinessExceptionHandler {
private static final String DEFAULT_BUSINESS_ERROR_MESSAGE = "业务处理失败";
/**
* 将业务异常中的错误码、提示信息和附加数据原样写入统一响应。
*
* @param exception 业务层主动抛出的异常
* @return 包含明确业务错误信息的统一响应
*/
@ExceptionHandler(BusinessException.class)
public Result<Object> handleBusinessException(BusinessException exception) {
String message = exception.getMessage();
if (message == null || message.trim().isEmpty()) {
message = DEFAULT_BUSINESS_ERROR_MESSAGE;
}
// Result.data(...) 在 data 为 null 时会覆盖传入的提示,因此先创建失败结果,
// 再单独设置异常携带的附加数据,确保 msg 始终保留真实的业务错误信息。
Result<Object> result = Result.fail(exception.getCode(), message);
result.setData(exception.getData());
return result;
}
}
package com.platform.business.exception;
import com.platform.exception.BusinessException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* 验证业务异常经过 Spring MVC 后仍能向调用方返回业务码和可读提示。
*/
class BusinessExceptionHandlerTest {
private MockMvc mockMvc;
@BeforeEach
void setUp() {
// 使用独立 MockMvc,仅装配测试接口和本次新增的异常处理器,避免外部服务依赖
// 影响测试结果,同时覆盖真实的 Controller 异常解析及 JSON 序列化流程。
mockMvc = MockMvcBuilders.standaloneSetup(new TestController())
.setControllerAdvice(new BusinessExceptionHandler())
.build();
}
@Test
void shouldReturnBusinessCodeAndMessageInsteadOfBare500() throws Exception {
mockMvc.perform(get("/test/business-exception"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(12345))
.andExpect(jsonPath("$.msg").value("当前操作不允许"));
}
/**
* 模拟业务接口直接抛出带自定义错误码和提示的业务异常。
*/
@RestController
private static class TestController {
@GetMapping("/test/business-exception")
public void throwBusinessException() {
throw new BusinessException(12345, "当前操作不允许", null);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment