zhenghaoyu
7 months ago
2 changed files with 55 additions and 0 deletions
@ -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); |
||||
} |
||||
|
||||
} |
@ -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…
Reference in new issue