Kaynağa Gözat

全局异常代理

wcc 1 yıl önce
ebeveyn
işleme
5f1f2f42ad

+ 100 - 0
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/handler/GlobalExceptionHandler.java

@@ -0,0 +1,100 @@
+package com.zanxiang.game.data.serve.handler;
+
+import com.zanxiang.erp.security.exception.NoPermissionException;
+import com.zanxiang.module.util.DateUtil;
+import com.zanxiang.module.util.exception.BaseException;
+import com.zanxiang.module.util.pojo.ResultVO;
+import com.zanxiang.module.web.util.ServletUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.converter.HttpMessageNotReadableException;
+import org.springframework.validation.BindException;
+import org.springframework.web.HttpRequestMethodNotSupportedException;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
+
+import java.time.LocalDateTime;
+
+/**
+ * 全局异常代理
+ *
+ * @author bilingfeng
+ */
+@Slf4j
+@RestControllerAdvice
+public class GlobalExceptionHandler {
+
+    /**
+     * 业务异常
+     */
+    @ExceptionHandler(BaseException.class)
+    public ResultVO<?> baseException(BaseException e) {
+        return ResultVO.fail(e.getMessage());
+    }
+
+    /**
+     * http method错误
+     *
+     * @param e
+     * @return
+     */
+    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
+    public ResultVO<?> methodNotSupportedException(HttpRequestMethodNotSupportedException e) {
+        log.error("URL:{}, 请求 method:{}, 支持的 method:{}", ServletUtil.getRequest().getRequestURL(), ServletUtil.getRequest().getMethod(), e.getSupportedMethods());
+        return ResultVO.fail("http method错误!");
+    }
+
+    /**
+     * 自定义参数校验错误
+     */
+    @ExceptionHandler(BindException.class)
+    public ResultVO<?> validatedBindException(BindException e) {
+        String message = e.getAllErrors().get(0).getDefaultMessage();
+        return ResultVO.fail(message);
+    }
+
+    /**
+     * 自定义参数校验错误
+     */
+    @ExceptionHandler(MethodArgumentNotValidException.class)
+    public ResultVO<?> validExceptionHandler(MethodArgumentNotValidException e) {
+        String message = e.getBindingResult().getFieldError().getDefaultMessage();
+        return ResultVO.fail(message);
+    }
+
+    /**
+     * 自定义参数校验错误
+     */
+    @ExceptionHandler(HttpMessageNotReadableException.class)
+    public ResultVO<?> validExceptionHandler(HttpMessageNotReadableException e) {
+        log.error(e.getMessage(), e);
+        return ResultVO.fail("参数异常,请勿非法操作");
+    }
+
+    /**
+     * 参数类型不匹配导致转换异常
+     *
+     * @param e
+     * @return
+     */
+    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
+    public ResultVO<?> mismatchErrorHandler(MethodArgumentTypeMismatchException e) {
+        log.error("方法:{},字段:{},参数:{},错误信息:{}", e.getParameter().getMethod(), e.getName(), e.getValue(), e.getMessage());
+        return ResultVO.fail("请求参数异常,请勿非法操作");
+    }
+
+    /**
+     * 权限异常
+     */
+    @ExceptionHandler(NoPermissionException.class)
+    public ResultVO<?> preAuthorizeException(NoPermissionException e) {
+        return ResultVO.fail("没有权限,请联系管理员授权");
+    }
+
+    @ExceptionHandler(Exception.class)
+    public ResultVO<?> handleException(Exception e) {
+        log.error(e.getMessage(), e);
+        return ResultVO.fail(DateUtil.formatLocalDateTime(LocalDateTime.now()) + ": 服务异常,请联系管理员");
+    }
+}

+ 76 - 0
game-data/game-data-serve/src/main/resources/logback.xml

@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration scan="false" scanPeriod="60 seconds" debug="false">
+    <!-- 日志存放路径 -->
+    <property name="log.path" value="${user.home}/logs"/>
+    <!-- 日志输出格式 -->
+    <property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
+    :ss} %-5level ${springAppName:-} %thread %logger %msg%n"/>
+
+    <!-- 控制台输出 -->
+    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>${log.pattern}</pattern>
+        </encoder>
+    </appender>
+
+    <!-- 系统日志输出 -->
+    <appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
+        <file>${log.path}/info.log</file>
+        <!-- 循环政策:基于时间创建日志文件 -->
+        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <!-- 日志文件名格式 -->
+            <fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
+            <!-- 日志最大的历史 60天 -->
+            <maxHistory>60</maxHistory>
+        </rollingPolicy>
+        <encoder>
+            <pattern>${log.pattern}</pattern>
+        </encoder>
+        <filter class="ch.qos.logback.classic.filter.LevelFilter">
+            <!-- 过滤的级别 -->
+            <level>INFO</level>
+            <!-- 匹配时的操作:接收(记录) -->
+            <onMatch>ACCEPT</onMatch>
+            <!-- 不匹配时的操作:拒绝(不记录) -->
+            <onMismatch>DENY</onMismatch>
+        </filter>
+    </appender>
+
+    <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
+        <file>${log.path}/error.log</file>
+        <!-- 循环政策:基于时间创建日志文件 -->
+        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <!-- 日志文件名格式 -->
+            <fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
+            <!-- 日志最大的历史 60天 -->
+            <maxHistory>60</maxHistory>
+        </rollingPolicy>
+        <encoder>
+            <pattern>${log.pattern}</pattern>
+        </encoder>
+        <filter class="ch.qos.logback.classic.filter.LevelFilter">
+            <!-- 过滤的级别 -->
+            <level>ERROR</level>
+            <!-- 匹配时的操作:接收(记录) -->
+            <onMatch>ACCEPT</onMatch>
+            <!-- 不匹配时的操作:拒绝(不记录) -->
+            <onMismatch>DENY</onMismatch>
+        </filter>
+    </appender>
+
+    <!-- 系统模块日志级别控制  -->
+    <logger name="com.zanxiang" level="info"/>
+    <!-- Spring日志级别控制  -->
+    <logger name="org.springframework" level="warn"/>
+
+    <root level="info">
+        <appender-ref ref="console"/>
+    </root>
+
+    <!--系统操作日志-->
+    <root level="info">
+        <appender-ref ref="file_info"/>
+        <appender-ref ref="file_error"/>
+    </root>
+
+</configuration>