2026-07-30 18:01:36 +08:00
|
|
|
|
package com.baiyue.common.web;
|
|
|
|
|
|
|
|
|
|
|
|
import com.baiyue.common.core.api.ResultCode;
|
|
|
|
|
|
import com.baiyue.common.core.exception.BizException;
|
2026-08-01 10:17:20 +08:00
|
|
|
|
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
2026-07-31 16:53:07 +08:00
|
|
|
|
import org.junit.jupiter.api.DisplayName;
|
2026-07-30 18:01:36 +08:00
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
2026-07-31 14:55:34 +08:00
|
|
|
|
import org.springframework.core.MethodParameter;
|
2026-08-06 18:01:59 +08:00
|
|
|
|
import org.springframework.validation.BeanPropertyBindingResult;
|
|
|
|
|
|
import org.springframework.validation.BindException;
|
|
|
|
|
|
import org.springframework.validation.FieldError;
|
2026-07-31 14:55:34 +08:00
|
|
|
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
2026-08-01 10:17:20 +08:00
|
|
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
2026-07-31 14:55:34 +08:00
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
2026-07-30 18:01:36 +08:00
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
2026-07-31 16:53:07 +08:00
|
|
|
|
@DisplayName("全局异常响应")
|
2026-07-30 18:01:36 +08:00
|
|
|
|
class GlobalExceptionHandlerTest {
|
|
|
|
|
|
private final GlobalExceptionHandler handler = new GlobalExceptionHandler();
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
void businessExceptionKeepsBusinessCodeAndCorrelation() {
|
|
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
|
|
request.setAttribute(CorrelationContext.REQUEST_ID, "r1");
|
|
|
|
|
|
request.setAttribute(CorrelationContext.TRACE_ID, "t1");
|
|
|
|
|
|
var response = handler.handleBusiness(new BizException("inventory is locked"), request);
|
2026-08-01 10:17:20 +08:00
|
|
|
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
2026-07-30 18:01:36 +08:00
|
|
|
|
assertThat(response.getBody().code()).isEqualTo(ResultCode.BUSINESS_ERROR.code());
|
|
|
|
|
|
assertThat(response.getBody().requestId()).isEqualTo("r1");
|
|
|
|
|
|
}
|
2026-07-31 14:55:34 +08:00
|
|
|
|
|
|
|
|
|
|
@Test
|
2026-07-31 16:53:07 +08:00
|
|
|
|
void invalidQueryParameterTypeMapsToBadRequest() throws Exception {
|
2026-07-31 14:55:34 +08:00
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
|
|
Method method = GlobalExceptionHandlerTest.class.getDeclaredMethod("paging", long.class);
|
|
|
|
|
|
MethodParameter parameter = new MethodParameter(method, 0);
|
|
|
|
|
|
MethodArgumentTypeMismatchException exception = new MethodArgumentTypeMismatchException(
|
|
|
|
|
|
"abc", Long.class, "page", parameter, new NumberFormatException("abc"));
|
|
|
|
|
|
|
|
|
|
|
|
var response = handler.handleBadRequest(exception, request);
|
|
|
|
|
|
|
2026-08-01 10:17:20 +08:00
|
|
|
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
2026-07-31 14:55:34 +08:00
|
|
|
|
assertThat(response.getBody().code()).isEqualTo(ResultCode.BAD_REQUEST.code());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-06 18:01:59 +08:00
|
|
|
|
@Test
|
|
|
|
|
|
void validationDetailsAreIncludedInMessageAndData() {
|
|
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
|
|
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(new Object(), "request");
|
|
|
|
|
|
bindingResult.addError(new FieldError("request", "carrierId", "不能为null"));
|
|
|
|
|
|
bindingResult.addError(new FieldError("request", "carrierName", "不能为空"));
|
|
|
|
|
|
|
|
|
|
|
|
var response = handler.handleBinding(new BindException(bindingResult), request);
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(response.getBody().code()).isEqualTo(ResultCode.VALIDATION_FAILED.code());
|
|
|
|
|
|
assertThat(response.getBody().message()).isEqualTo("carrierId: 不能为null;carrierName: 不能为空");
|
|
|
|
|
|
assertThat(response.getBody().data()).containsEntry("carrierId", "不能为null")
|
|
|
|
|
|
.containsEntry("carrierName", "不能为空");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-01 10:17:20 +08:00
|
|
|
|
@Test
|
|
|
|
|
|
void recordsBusinessFailureMetricByFiniteResultCode() {
|
|
|
|
|
|
SimpleMeterRegistry registry = new SimpleMeterRegistry();
|
|
|
|
|
|
ReflectionTestUtils.setField(handler, "meterRegistry", registry);
|
|
|
|
|
|
|
|
|
|
|
|
handler.handleBusiness(new BizException(ResultCode.CONFLICT, "duplicate"),
|
|
|
|
|
|
new MockHttpServletRequest());
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(registry.get("baiyue.api.business.failures")
|
|
|
|
|
|
.tag("code", ResultCode.CONFLICT.code()).counter().count()).isEqualTo(1.0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-31 14:55:34 +08:00
|
|
|
|
private void paging(long page) {
|
|
|
|
|
|
}
|
2026-07-30 18:01:36 +08:00
|
|
|
|
}
|