# Conflicts: # luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java # luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.javarelease-2024-08-08
commit
35ee47b668
@ -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;
|
||||
}
|
||||
}
|
@ -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<String> 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<String, String[]> parameterMap) {
|
||||
if (CollectionUtils.isEmpty(parameterMap)) {
|
||||
return "";
|
||||
}
|
||||
return parameterMap.entrySet().stream().map(this::getParameter).collect(Collectors.joining("|"));
|
||||
}
|
||||
|
||||
private String getParameter(Entry<String, String[]> 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());
|
||||
}
|
||||
}
|
@ -1,61 +1,16 @@
|
||||
package com.luoo.music.controller;
|
||||
|
||||
import api.Result;
|
||||
import api.StatusCode;
|
||||
import exception.BizException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import controller.AbstractBaseExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* 统一异常处理类
|
||||
*/
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
public class BaseExceptionHandler {
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
@ResponseBody
|
||||
public Result<Void> error(Exception e) {
|
||||
log.error("执行出错", e);
|
||||
return Result.failed(StatusCode.MUSIC_COMMON_FAILED);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = BizException.class)
|
||||
@ResponseBody
|
||||
public Result<String> error(BizException e) {
|
||||
log.info("业务错误:{}", e.getMessage());
|
||||
StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.MUSIC_COMMON_FAILED : e.getCodeEnum();
|
||||
return Result.failed(statusCode, e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数校检异常
|
||||
*
|
||||
* @param e 实体类校验
|
||||
*/
|
||||
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||
@ResponseBody
|
||||
public Result<?> handle(MethodArgumentNotValidException e) {
|
||||
BindingResult bindingResult = e.getBindingResult();
|
||||
|
||||
StringJoiner joiner = new StringJoiner(";");
|
||||
|
||||
for (ObjectError error : bindingResult.getAllErrors()) {
|
||||
String code = error.getCode();
|
||||
String[] codes = error.getCodes();
|
||||
|
||||
String property = codes[1];
|
||||
property = property.replace(code, "").replaceFirst(".", "");
|
||||
|
||||
String defaultMessage = error.getDefaultMessage();
|
||||
joiner.add(property + defaultMessage);
|
||||
}
|
||||
return Result.failed(StatusCode.MUSIC_COMMON_FAILED, joiner.toString());
|
||||
}
|
||||
public class BaseExceptionHandler extends AbstractBaseExceptionHandler {
|
||||
@Override
|
||||
protected StatusCode getCommonFailedStatusCode() {
|
||||
return StatusCode.MUSIC_COMMON_FAILED;
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +1,16 @@
|
||||
package com.luoo.user.controller;
|
||||
|
||||
import exception.BizException;
|
||||
import api.Result;
|
||||
import api.StatusCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import controller.AbstractBaseExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* 统一异常处理类
|
||||
*/
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
public class BaseExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
@ResponseBody
|
||||
public Result<Void> error(Exception e) {
|
||||
log.error("执行出错", e);
|
||||
return Result.failed(StatusCode.USER_COMMON_FAILED, e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = BizException.class)
|
||||
@ResponseBody
|
||||
public Result<String> error(BizException e) {
|
||||
log.info("业务错误:{}", e.getMessage());
|
||||
StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.USER_COMMON_FAILED : e.getCodeEnum();
|
||||
return Result.failed(statusCode, e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数校检异常
|
||||
*
|
||||
* @param e 实体类校验
|
||||
*/
|
||||
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||
@ResponseBody
|
||||
public Result<?> handle(MethodArgumentNotValidException e) {
|
||||
BindingResult bindingResult = e.getBindingResult();
|
||||
|
||||
StringJoiner joiner = new StringJoiner(";");
|
||||
|
||||
for (ObjectError error : bindingResult.getAllErrors()) {
|
||||
String code = error.getCode();
|
||||
String[] codes = error.getCodes();
|
||||
|
||||
String property = codes[1];
|
||||
property = property.replace(code, "").replaceFirst(".", "");
|
||||
|
||||
String defaultMessage = error.getDefaultMessage();
|
||||
joiner.add(property + defaultMessage);
|
||||
}
|
||||
return Result.failed(StatusCode.MUSIC_COMMON_FAILED, joiner.toString());
|
||||
public class BaseExceptionHandler extends AbstractBaseExceptionHandler {
|
||||
@Override
|
||||
protected StatusCode getCommonFailedStatusCode() {
|
||||
return StatusCode.USER_COMMON_FAILED;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue