From a046899dd6154090d25307e8ad9c85c206317c85 Mon Sep 17 00:00:00 2001 From: xiongxiaoyang <1179705413@qq.com> Date: Mon, 17 Mar 2025 10:12:53 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E9=80=9A=E7=94=A8=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/advice/CommonExceptionHandler.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 novel-common/src/main/java/com/java2nb/novel/core/advice/CommonExceptionHandler.java diff --git a/novel-common/src/main/java/com/java2nb/novel/core/advice/CommonExceptionHandler.java b/novel-common/src/main/java/com/java2nb/novel/core/advice/CommonExceptionHandler.java new file mode 100644 index 0000000..c564246 --- /dev/null +++ b/novel-common/src/main/java/com/java2nb/novel/core/advice/CommonExceptionHandler.java @@ -0,0 +1,36 @@ +package com.java2nb.novel.core.advice; + +import io.github.xxyopen.model.resp.RestResult; +import io.github.xxyopen.model.resp.SysResultCode; +import io.github.xxyopen.web.exception.BusinessException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.validation.BindException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class CommonExceptionHandler { + private static final Logger log = LoggerFactory.getLogger(io.github.xxyopen.web.exception.CommonExceptionHandler.class); + + public CommonExceptionHandler() { + } + + @ExceptionHandler({BindException.class}) + public RestResult handlerBindException(BindException e) { + log.error(e.getMessage(), e); + return RestResult.fail(SysResultCode.PARAM_ERROR); + } + + @ExceptionHandler({BusinessException.class}) + public RestResult handlerBusinessException(BusinessException e) { + log.error(e.getMessage(), e); + return RestResult.fail(e.getResultCode()); + } + + @ExceptionHandler({Exception.class}) + public RestResult handlerException(Exception e) { + log.error(e.getMessage(), e); + return RestResult.error(); + } +} \ No newline at end of file