| | |
| | | import org.springframework.web.bind.annotation.ControllerAdvice; |
| | | import org.springframework.web.bind.annotation.ExceptionHandler; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.springframework.web.context.request.async.AsyncRequestNotUsableException; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | |
| | | return R.error(out); |
| | | } |
| | | |
| | | @ExceptionHandler(AsyncRequestNotUsableException.class) |
| | | public void asyncRequestNotUsableExceptionHandler(AsyncRequestNotUsableException e) { |
| | | logger.warn("Client connection aborted: {}", resolveAbortMessage(e)); |
| | | } |
| | | |
| | | @ResponseBody |
| | | @ExceptionHandler(RuntimeException.class) |
| | | public R runtimeExceptionHandler(RuntimeException e, HttpServletResponse response) { |
| | | if (isClientAbortException(e)) { |
| | | logger.warn("Client connection aborted: {}", resolveAbortMessage(e)); |
| | | return null; |
| | | } |
| | | CommonUtil.addCrossHeaders(response); |
| | | Throwable cause = e.getCause(); |
| | | if (cause instanceof CoolException) { |
| | | return R.error(cause.getMessage()); |
| | | } |
| | | logger.error(e.getMessage(), e); |
| | | return R.error(Constants.RESULT_ERROR_MSG); |
| | | } |
| | | |
| | | @ResponseBody |
| | | @ExceptionHandler(Throwable.class) |
| | | public R exceptionHandler(Throwable e, HttpServletResponse response) { |
| | | if (isClientAbortException(e)) { |
| | | logger.warn("Client connection aborted: {}", resolveAbortMessage(e)); |
| | | return null; |
| | | } |
| | | logger.error(e.getMessage(), e); |
| | | CommonUtil.addCrossHeaders(response); |
| | | return R.error(Constants.RESULT_ERROR_MSG); |
| | | } |
| | | |
| | | private boolean isClientAbortException(Throwable throwable) { |
| | | Throwable current = throwable; |
| | | while (current != null) { |
| | | String message = current.getMessage(); |
| | | if (message != null) { |
| | | String normalized = message.toLowerCase(); |
| | | if (normalized.contains("broken pipe") |
| | | || normalized.contains("connection reset") |
| | | || normalized.contains("forcibly closed") |
| | | || normalized.contains("abort")) { |
| | | return true; |
| | | } |
| | | } |
| | | current = current.getCause(); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | private String resolveAbortMessage(Throwable throwable) { |
| | | Throwable current = throwable; |
| | | while (current != null) { |
| | | String message = current.getMessage(); |
| | | if (message != null && !message.isBlank()) { |
| | | return message; |
| | | } |
| | | current = current.getCause(); |
| | | } |
| | | return "client aborted connection"; |
| | | } |
| | | |
| | | } |
| | | |