update 新增sql执行全局异常

This commit is contained in:
yyb
2026-01-13 15:44:11 +08:00
parent e7dc77fc45
commit f5fecd3059

View File

@@ -1,16 +1,28 @@
package com.m2pool.lease.exception; package com.m2pool.lease.exception;
import cn.hutool.json.JSONUtil;
import com.m2pool.lease.dto.Result; import com.m2pool.lease.dto.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
/** /**
* 全局异常处理器,用于捕获并处理应用中抛出的异常。 * 全局异常处理器,用于捕获并处理应用中抛出的异常。
*/ */
@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice @RestControllerAdvice
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/** /**
* 处理商品已出售异常,返回统一的错误结果。 * 处理商品已出售异常,返回统一的错误结果。
* *
@@ -18,49 +30,67 @@ public class GlobalExceptionHandler {
* @return 包含错误信息的统一结果对象 * @return 包含错误信息的统一结果对象
*/ */
@ExceptionHandler(ProductSoldOutException.class) @ExceptionHandler(ProductSoldOutException.class)
public Result<String> handleProductSoldOutException(ProductSoldOutException e) { public Result<String> handleProductSoldOutException(ProductSoldOutException e, HttpServletRequest request) {
return Result.fail(e.getMessage()); return Result.fail(e.getMessage());
} }
@ExceptionHandler(MachineException.class) @ExceptionHandler(MachineException.class)
public Result<String> handleMachineException(MachineException e) { public Result<String> handleMachineException(MachineException e, HttpServletRequest request) {
return Result.fail(e.getMessage()); return Result.fail(e.getMessage());
} }
@ExceptionHandler(PaymentException.class) @ExceptionHandler(PaymentException.class)
public Result<String> handlePaymentException(PaymentException e) { public Result<String> handlePaymentException(PaymentException e, HttpServletRequest request) {
return Result.fail(e.getMessage()); return Result.fail(e.getMessage());
} }
@ExceptionHandler(OrderException.class) @ExceptionHandler(OrderException.class)
public Result<String> handleOrderException(OrderException e) { public Result<String> handleOrderException(OrderException e, HttpServletRequest request) {
return Result.fail(e.getMessage()); return Result.fail(e.getMessage());
} }
@ExceptionHandler(PayRechargeException.class) @ExceptionHandler(PayRechargeException.class)
public Result<String> handleOrderException(PayRechargeException e) { public Result<String> handleOrderException(PayRechargeException e, HttpServletRequest request) {
return Result.fail(e.getMessage()); return Result.fail(e.getMessage());
} }
@ExceptionHandler(RSAException.class) @ExceptionHandler(RSAException.class)
public Result<String> handleRSAException(RSAException e) { public Result<String> handleRSAException(RSAException e, HttpServletRequest request) {
return Result.fail(e.getMessage()); return Result.fail(e.getMessage());
} }
@ExceptionHandler(AuthException.class) @ExceptionHandler(AuthException.class)
public Result<String> handleAuthException(AuthException e) { public Result<String> handleAuthException(AuthException e, HttpServletRequest request) {
return Result.fail(e.getMessage()); return Result.fail(e.getMessage());
} }
@ExceptionHandler(SQLException.class)
public Result<String> handleSQLException(SQLException e, HttpServletRequest request)
{
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生sql执行异常.", requestURI, e);
return Result.fail("请求地址'{}',发生sql执行异常.");
}
@ExceptionHandler(BadSqlGrammarException.class)
public Result<String> handleBadSqlGrammarException(BadSqlGrammarException e, HttpServletRequest request)
{
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生SQL异常.", requestURI, e);
return Result.fail("数据库异常!");
}
/** /**
* 处理其他未明确捕获的异常,返回统一的错误结果。 * 处理其他未明确捕获的异常,返回统一的错误结果。
* *
* @param e 异常对象 * @param e 异常对象
* @return 包含错误信息的统一结果对象 * @return 包含错误信息的统一结果对象
*/ */
//@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
//public Result<String> handleException(Exception e) { public Result<String> handleException(Exception e, HttpServletRequest request) {
// System.out.println(e); String requestURI = request.getRequestURI();
// return Result.fail("系统异常,请稍后再试!"); log.error("请求地址'{}',发生系统异常.", requestURI,e);
//} return Result.fail("系统异常,请稍后再试!");
}
} }