1.add more info for error log

main
Gary 7 months ago
parent 427fc9e4d1
commit 456c87d158

@ -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,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<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());
public class BaseExceptionHandler extends AbstractBaseExceptionHandler {
@Override
protected StatusCode getCommonFailedStatusCode() {
return StatusCode.MUSIC_COMMON_FAILED;
}
}

@ -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<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());
public class BaseExceptionHandler extends AbstractBaseExceptionHandler {
@Override
protected StatusCode getCommonFailedStatusCode() {
return StatusCode.USER_COMMON_FAILED;
}
}
}
Loading…
Cancel
Save