From f5fecd30590123d983486acf16cf56e78725a8ff Mon Sep 17 00:00:00 2001 From: yyb <1416014977@qq.com> Date: Tue, 13 Jan 2026 15:44:11 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=96=B0=E5=A2=9Esql=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 56 ++++++++++++++----- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/m2pool/lease/exception/GlobalExceptionHandler.java b/src/main/java/com/m2pool/lease/exception/GlobalExceptionHandler.java index 8e658a2..ab40b06 100644 --- a/src/main/java/com/m2pool/lease/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/m2pool/lease/exception/GlobalExceptionHandler.java @@ -1,16 +1,28 @@ package com.m2pool.lease.exception; -import cn.hutool.json.JSONUtil; 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.RestControllerAdvice; +import javax.servlet.http.HttpServletRequest; +import java.sql.SQLException; + /** * 全局异常处理器,用于捕获并处理应用中抛出的异常。 */ +@Order(Ordered.HIGHEST_PRECEDENCE) @RestControllerAdvice public class GlobalExceptionHandler { + + private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); + + /** * 处理商品已出售异常,返回统一的错误结果。 * @@ -18,49 +30,67 @@ public class GlobalExceptionHandler { * @return 包含错误信息的统一结果对象 */ @ExceptionHandler(ProductSoldOutException.class) - public Result handleProductSoldOutException(ProductSoldOutException e) { + public Result handleProductSoldOutException(ProductSoldOutException e, HttpServletRequest request) { return Result.fail(e.getMessage()); } @ExceptionHandler(MachineException.class) - public Result handleMachineException(MachineException e) { + public Result handleMachineException(MachineException e, HttpServletRequest request) { return Result.fail(e.getMessage()); } @ExceptionHandler(PaymentException.class) - public Result handlePaymentException(PaymentException e) { + public Result handlePaymentException(PaymentException e, HttpServletRequest request) { return Result.fail(e.getMessage()); } @ExceptionHandler(OrderException.class) - public Result handleOrderException(OrderException e) { + public Result handleOrderException(OrderException e, HttpServletRequest request) { return Result.fail(e.getMessage()); } @ExceptionHandler(PayRechargeException.class) - public Result handleOrderException(PayRechargeException e) { + public Result handleOrderException(PayRechargeException e, HttpServletRequest request) { return Result.fail(e.getMessage()); } @ExceptionHandler(RSAException.class) - public Result handleRSAException(RSAException e) { + public Result handleRSAException(RSAException e, HttpServletRequest request) { return Result.fail(e.getMessage()); } @ExceptionHandler(AuthException.class) - public Result handleAuthException(AuthException e) { + public Result handleAuthException(AuthException e, HttpServletRequest request) { return Result.fail(e.getMessage()); } + + @ExceptionHandler(SQLException.class) + public Result handleSQLException(SQLException e, HttpServletRequest request) + { + String requestURI = request.getRequestURI(); + log.error("请求地址'{}',发生sql执行异常.", requestURI, e); + return Result.fail("请求地址'{}',发生sql执行异常."); + } + + @ExceptionHandler(BadSqlGrammarException.class) + public Result handleBadSqlGrammarException(BadSqlGrammarException e, HttpServletRequest request) + { + String requestURI = request.getRequestURI(); + log.error("请求地址'{}',发生SQL异常.", requestURI, e); + return Result.fail("数据库异常!"); + } + /** * 处理其他未明确捕获的异常,返回统一的错误结果。 * * @param e 异常对象 * @return 包含错误信息的统一结果对象 */ - //@ExceptionHandler(Exception.class) - //public Result handleException(Exception e) { - // System.out.println(e); - // return Result.fail("系统异常,请稍后再试!"); - //} + @ExceptionHandler(Exception.class) + public Result handleException(Exception e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求地址'{}',发生系统异常.", requestURI,e); + return Result.fail("系统异常,请稍后再试!"); + } }