diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/BaseExceptionHandler.java b/luoo_comment/src/main/java/com/luoo/comment/controller/BaseExceptionHandler.java new file mode 100644 index 0000000..196e251 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/BaseExceptionHandler.java @@ -0,0 +1,16 @@ +package com.luoo.comment.controller; + +import api.StatusCode; +import controller.AbstractBaseExceptionHandler; +import org.springframework.web.bind.annotation.ControllerAdvice; + +/** + * 统一异常处理类 + */ +@ControllerAdvice +public class BaseExceptionHandler extends AbstractBaseExceptionHandler { + @Override + protected StatusCode getCommonFailedStatusCode() { + return StatusCode.COMMENT_COMMON_FAILED; + } +} diff --git a/luoo_common/src/main/java/controller/AbstractBaseExceptionHandler.java b/luoo_common/src/main/java/controller/AbstractBaseExceptionHandler.java new file mode 100644 index 0000000..02aec9a --- /dev/null +++ b/luoo_common/src/main/java/controller/AbstractBaseExceptionHandler.java @@ -0,0 +1,58 @@ +package controller; + +import java.util.Arrays; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.util.CollectionUtils; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import api.Result; +import api.StatusCode; +import dto.UserLoginDto; +import exception.BizException; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public abstract class AbstractBaseExceptionHandler extends BaseController { + @ExceptionHandler(value = Exception.class) + @ResponseBody + public Result error(Exception e, HttpServletRequest request) { + log.error("{}user: {} 请求错误,请求地址: {}, 请求参数: {}, 错误信息: ", getDeviceType(request.getHeader("device_type")), + getUserId(request.getHeader("Authorization")), request.getRequestURL(), + getRequestParameter(request.getParameterMap()), e); + if (e instanceof BizException) { + BizException biz = (BizException) e; + StatusCode statusCode = null == biz.getCodeEnum() ? getCommonFailedStatusCode() : biz.getCodeEnum(); + return Result.failed(statusCode, biz.getMessage()); + } + return Result.failed(getCommonFailedStatusCode(), e.getMessage()); + } + + protected abstract StatusCode getCommonFailedStatusCode(); + + private String getRequestParameter(Map parameterMap) { + if (CollectionUtils.isEmpty(parameterMap)) { + return ""; + } + return parameterMap.entrySet().stream().map(this::getParameter).collect(Collectors.joining("|")); + } + + private String getParameter(Entry e) { + return e.getKey() + ":" + + (null == e.getValue() ? "" : Arrays.stream(e.getValue()).collect(Collectors.joining(","))); + } + + private String getDeviceType(String deviceType) { + return null == deviceType ? "" : (deviceType + " "); + } + + private String getUserId(String authorization) { + UserLoginDto userLoginDto = getUserLoginDto(authorization); + return null == userLoginDto ? "" : (userLoginDto.getUserId() + "_" + userLoginDto.getNickName()); + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java b/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java index d34876c..75bf1a6 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java @@ -1,32 +1,16 @@ package com.luoo.music.controller; -import api.Result; import api.StatusCode; -import exception.BizException; -import lombok.extern.slf4j.Slf4j; - +import controller.AbstractBaseExceptionHandler; import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; /** * 统一异常处理类 */ -@Slf4j @ControllerAdvice -public class BaseExceptionHandler { - @ExceptionHandler(value = Exception.class) - @ResponseBody - public Result error(Exception e) { - log.error("执行出错", e); - return Result.failed(StatusCode.MUSIC_COMMON_FAILED); - } - - @ExceptionHandler(value = BizException.class) - @ResponseBody - public Result error(BizException e) { - log.info("业务错误:{}", e.getMessage()); - StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.MUSIC_COMMON_FAILED : e.getCodeEnum(); - return Result.failed(statusCode, e.getMessage()); +public class BaseExceptionHandler extends AbstractBaseExceptionHandler { + @Override + protected StatusCode getCommonFailedStatusCode() { + return StatusCode.MUSIC_COMMON_FAILED; } } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java b/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java index 08f4aef..97e8cbd 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java @@ -1,32 +1,16 @@ package com.luoo.user.controller; -import exception.BizException; -import api.Result; import api.StatusCode; -import lombok.extern.slf4j.Slf4j; +import controller.AbstractBaseExceptionHandler; import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; /** * 统一异常处理类 */ -@Slf4j @ControllerAdvice -public class BaseExceptionHandler { - - @ExceptionHandler(value = Exception.class) - @ResponseBody - public Result error(Exception e) { - log.error("执行出错", e); - return Result.failed(StatusCode.USER_COMMON_FAILED, e.getMessage()); - } - - @ExceptionHandler(value = BizException.class) - @ResponseBody - public Result error(BizException e) { - log.info("业务错误:{}", e.getMessage()); - StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.USER_COMMON_FAILED : e.getCodeEnum(); - return Result.failed(statusCode, e.getMessage()); +public class BaseExceptionHandler extends AbstractBaseExceptionHandler { + @Override + protected StatusCode getCommonFailedStatusCode() { + return StatusCode.USER_COMMON_FAILED; } -} +} \ No newline at end of file