Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加数据库报错返回优化 #15

Open
wants to merge 1 commit into
base: dev-single
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions genshin-map-commons/genshin-map-commons-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}