From d09f872fd38e7106e1fea14b6518b6cd4298428f Mon Sep 17 00:00:00 2001 From: zhenghaoyu Date: Wed, 12 Jun 2024 11:32:15 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E5=85=A8=E5=B1=80=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E5=BC=82=E5=B8=B8=E6=8B=A6=E6=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/GlobalExceptionHandler.java | 27 ++++++++++++++++++ .../config/GlobalExceptionHandler.java | 28 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/GlobalExceptionHandler.java create mode 100644 blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/GlobalExceptionHandler.java diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/GlobalExceptionHandler.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/config/GlobalExceptionHandler.java new file mode 100644 index 000000000..92519bd18 --- /dev/null +++ b/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 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); + } + +} diff --git a/blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/GlobalExceptionHandler.java b/blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/config/GlobalExceptionHandler.java new file mode 100644 index 000000000..14fb2ffee --- /dev/null +++ b/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 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); + } + +}