From 9b2ae6f3e510ea9a329e89e8b67d039175d895f2 Mon Sep 17 00:00:00 2001 From: p5j9vn3t2 <2596686495@qq.com> Date: Sun, 30 Apr 2023 08:11:15 +0800 Subject: [PATCH] ADD file via upload --- .../com/exception/GlobalExceptionHandler.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/com/exception/GlobalExceptionHandler.java diff --git a/src/main/java/com/exception/GlobalExceptionHandler.java b/src/main/java/com/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..d4bb5ae --- /dev/null +++ b/src/main/java/com/exception/GlobalExceptionHandler.java @@ -0,0 +1,32 @@ +package com.example.exception; + +import cn.hutool.log.Log; +import cn.hutool.log.LogFactory; +import com.example.common.Result; +import com.example.common.ResultCode; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.servlet.http.HttpServletRequest; + +@ControllerAdvice(basePackages="com.example.controller") +public class GlobalExceptionHandler { + + private static final Log log = LogFactory.get(); + + + //统一异常处理@ExceptionHandler,主要用于Exception + @ExceptionHandler(Exception.class) + @ResponseBody//返回json串 + public Result error(HttpServletRequest request, Exception e){ + log.error("异常信息:",e); + return Result.error(); + } + + @ExceptionHandler(CustomException.class) + @ResponseBody//返回json串 + public Result customError(HttpServletRequest request, CustomException e){ + return Result.error(e.getCode(), e.getMsg()); + } +}