From c7b4f275d231555e4a383b0a59d29b0bc093a2bc Mon Sep 17 00:00:00 2001 From: AprilWind <2100166581@qq.com> Date: Sun, 26 May 2024 20:40:45 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=96=B0=E5=A2=9ESMS=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sms/config/SmsAutoConfiguration.java | 9 ++++++ .../sms/handler/SmsExceptionHandler.java | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/handler/SmsExceptionHandler.java diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsAutoConfiguration.java b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsAutoConfiguration.java index 0346f11a..7159550a 100644 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsAutoConfiguration.java +++ b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsAutoConfiguration.java @@ -2,6 +2,7 @@ package org.dromara.common.sms.config; import org.dromara.common.redis.config.RedisConfiguration; import org.dromara.common.sms.core.dao.PlusSmsDao; +import org.dromara.common.sms.handler.SmsExceptionHandler; import org.dromara.sms4j.api.dao.SmsDao; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.context.annotation.Bean; @@ -21,4 +22,12 @@ public class SmsAutoConfiguration { return new PlusSmsDao(); } + /** + * 异常处理器 + */ + @Bean + public SmsExceptionHandler smsExceptionHandler() { + return new SmsExceptionHandler(); + } + } diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/handler/SmsExceptionHandler.java b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/handler/SmsExceptionHandler.java new file mode 100644 index 00000000..2c619a36 --- /dev/null +++ b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/handler/SmsExceptionHandler.java @@ -0,0 +1,30 @@ +package org.dromara.common.sms.handler; + +import cn.hutool.http.HttpStatus; +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.dromara.common.core.domain.R; +import org.dromara.sms4j.comm.exception.SmsBlendException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * SMS异常处理器 + * + * @author AprilWind + */ +@Slf4j +@RestControllerAdvice +public class SmsExceptionHandler { + + /** + * sms异常 + */ + @ExceptionHandler(SmsBlendException.class) + public R handleSmsBlendException(SmsBlendException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求地址'{}',发生sms短信异常.", requestURI, e); + return R.fail(HttpStatus.HTTP_INTERNAL_ERROR, "短信发送失败,请稍后再试..."); + } + +}