Browse Source

1.增加全局超时异常拦截

dist.1.3.0
zhenghaoyu 7 months ago
parent
commit
d09f872fd3
  1. 27
      blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/GlobalExceptionHandler.java
  2. 28
      blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/GlobalExceptionHandler.java

27
blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/GlobalExceptionHandler.java

@ -0,0 +1,27 @@
package com.logpm.distribution.config;
import org.springblade.core.tool.api.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleCustomException(Exception e) {
// 从CustomException中获取errorCode和errorMessage
String errorMessage = e.getMessage();
// 创建一个自定义的响应体来封装错误信息
if(errorMessage.contains("Communications link failure")){
// 返回带有错误信息的HTTP响应
return new ResponseEntity<>(R.fail(405,"请求超时,请重试"), HttpStatus.BAD_REQUEST);
}
// 返回带有错误信息的HTTP响应
return new ResponseEntity<>(R.fail(400,errorMessage), HttpStatus.BAD_REQUEST);
}
}

28
blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/GlobalExceptionHandler.java

@ -0,0 +1,28 @@
package com.logpm.trunkline.config;
import com.mysql.cj.jdbc.exceptions.CommunicationsException;
import org.springblade.core.tool.api.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(CommunicationsException.class)
public ResponseEntity<Object> handleCustomException(CommunicationsException e) {
// 从CustomException中获取errorCode和errorMessage
String errorMessage = e.getMessage();
// 创建一个自定义的响应体来封装错误信息
if(errorMessage.contains("Communications link failure")){
// 返回带有错误信息的HTTP响应
return new ResponseEntity<>(R.fail(405,"请求超时,请重试"), HttpStatus.BAD_REQUEST);
}
// 返回带有错误信息的HTTP响应
return new ResponseEntity<>(R.fail(e.getErrorCode(),errorMessage), HttpStatus.BAD_REQUEST);
}
}
Loading…
Cancel
Save