From ebf521705a61215044daefafae6bb02395a61b03 Mon Sep 17 00:00:00 2001 From: passage2016 Date: Mon, 27 Feb 2023 00:05:17 -0500 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E8=BF=94=E5=9B=9E=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../genshin-map-commons-web/pom.xml | 8 +++++++ .../common/web/exception/RestException.java | 21 ++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/genshin-map-commons/genshin-map-commons-web/pom.xml b/genshin-map-commons/genshin-map-commons-web/pom.xml index d9564f54..24e4e517 100644 --- a/genshin-map-commons/genshin-map-commons-web/pom.xml +++ b/genshin-map-commons/genshin-map-commons-web/pom.xml @@ -62,6 +62,14 @@ com.alibaba fastjson + + org.postgresql + postgresql + + + org.springframework + spring-tx + \ No newline at end of file diff --git a/genshin-map-commons/genshin-map-commons-web/src/main/java/site/yuanshen/common/web/exception/RestException.java b/genshin-map-commons/genshin-map-commons-web/src/main/java/site/yuanshen/common/web/exception/RestException.java index 8217bd86..dd7c891e 100644 --- a/genshin-map-commons/genshin-map-commons-web/src/main/java/site/yuanshen/common/web/exception/RestException.java +++ b/genshin-map-commons/genshin-map-commons-web/src/main/java/site/yuanshen/common/web/exception/RestException.java @@ -1,6 +1,8 @@ package site.yuanshen.common.web.exception; import lombok.extern.slf4j.Slf4j; +import org.postgresql.util.PSQLException; +import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; @@ -12,10 +14,11 @@ import site.yuanshen.common.web.response.RUtils; import site.yuanshen.common.web.utils.RequestUtils; -import javax.naming.AuthenticationException; import javax.servlet.http.HttpServletRequest; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; +import java.time.LocalDateTime; +import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; @@ -81,4 +84,20 @@ public R constraintViolationExceptionHandler(ConstraintViolationException e) { .collect(Collectors.toSet())); } + @ExceptionHandler({PSQLException.class, DataAccessResourceFailureException.class}) + public R databaseExceptionHandler(Throwable t) { + //获取当前请求 + HttpServletRequest request = null; + try { + request = RequestUtils.getHttpServletRequest(); + log.error("[Database-Exception]-[{}]-message: {}", request.getRequestURL(), t); + log.debug("[Database-Exception]-[{}]-debug error message: {} - cause:", request.getRequestURL(), t); + } catch (Exception e) { + log.error("[Database-Exception]-message: {}", t.getMessage()); + log.debug("[Database-Exception]-debug error message:: {} - cause {}", t.getMessage(), t.getStackTrace()); + } + return RUtils.create(Codes.FAIL, "数据库异常,可能因为当前服务压力过大,5分钟内得不到恢复,请携带报错信息,联系开发组;" + + "报错时间:" + LocalDateTime.now() + + (request == null ? "" : "; 报错接口:" + Objects.requireNonNull(request).getRequestURL()), null); + } }