优化
This commit is contained in:
@@ -15,6 +15,7 @@ import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.dao.CannotAcquireLockException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.MissingRequestHeaderException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
@@ -96,6 +97,14 @@ public class GlobalExceptionHandler {
|
||||
return failure(ResultCode.NOT_FOUND, ResultCode.NOT_FOUND.message(), request);
|
||||
}
|
||||
|
||||
@ExceptionHandler(CannotAcquireLockException.class)
|
||||
public ResponseEntity<ApiResponse<Void>> handleCannotAcquireLock(
|
||||
CannotAcquireLockException exception, HttpServletRequest request) {
|
||||
log.warn("Database lock acquisition conflict requestId={} traceId={}",
|
||||
CorrelationContext.requestId(request), CorrelationContext.traceId(request));
|
||||
return failure(ResultCode.CONFLICT, "资源正在处理中,请稍后重试", request);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ApiResponse<Void>> handleUnexpected(Exception exception, HttpServletRequest request) {
|
||||
log.error("Unhandled request failure requestId={} traceId={}",
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.springframework.web.servlet.resource.NoResourceFoundException;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.dao.CannotAcquireLockException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -116,6 +117,18 @@ class GlobalExceptionHandlerTest {
|
||||
assertThat(response.getBody().code()).isEqualTo(ResultCode.NOT_FOUND.code());
|
||||
}
|
||||
|
||||
@Test
|
||||
void lockWaitTimeoutMapsToCanonicalConflict() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setAttribute(CorrelationContext.REQUEST_ID, "lock-timeout");
|
||||
|
||||
var response = handler.handleCannotAcquireLock(
|
||||
new CannotAcquireLockException("Lock wait timeout exceeded"), request);
|
||||
|
||||
assertThat(response.getBody().code()).isEqualTo(ResultCode.CONFLICT.code());
|
||||
assertThat(response.getBody().requestId()).isEqualTo("lock-timeout");
|
||||
}
|
||||
|
||||
private void paging(long page) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user