update ExceptionHandlerAdvice

pull/26/head
3y 2 years ago
parent 6d70ab60c1
commit 64a37b8a3c

@ -36,7 +36,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.util.*; import java.util.*;
/** /**
@ -196,16 +195,11 @@ public class OfficialAccountController {
@ApiOperation("/检查是否已经登录") @ApiOperation("/检查是否已经登录")
@AustinResult @AustinResult
public WxMpUser checkLogin(String sceneId) { public WxMpUser checkLogin(String sceneId) {
try {
String userInfo = redisTemplate.opsForValue().get(sceneId); String userInfo = redisTemplate.opsForValue().get(sceneId);
if (StrUtil.isBlank(userInfo)) { if (StrUtil.isBlank(userInfo)) {
throw new CommonException(RespStatusEnum.NO_LOGIN); throw new CommonException(RespStatusEnum.NO_LOGIN);
} }
return JSON.parseObject(userInfo, (Type) WxMpUser.class); return JSON.parseObject(userInfo, WxMpUser.class);
} catch (Exception e) {
log.error("OfficialAccountController#checkLogin fail:{}", Throwables.getStackTraceAsString(e));
return null;
}
} }
/** /**

@ -2,6 +2,7 @@ package com.java3y.austin.web.exception;
import com.java3y.austin.common.enums.RespStatusEnum; import com.java3y.austin.common.enums.RespStatusEnum;
import com.java3y.austin.common.vo.BasicResultVO; import com.java3y.austin.common.vo.BasicResultVO;
import org.assertj.core.util.Throwables;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -10,16 +11,13 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import java.io.PrintWriter;
import java.io.StringWriter;
/** /**
* @author kl * @author kl
* @version 1.0.0 * @version 1.0.0
* @description * @description
* @date 2023/2/9 19:03 * @date 2023/2/9 19:03
*/ */
@ControllerAdvice @ControllerAdvice(basePackages = "com.java3y.austin.web.controller")
@ResponseBody @ResponseBody
public class ExceptionHandlerAdvice { public class ExceptionHandlerAdvice {
private static final Logger log = LoggerFactory.getLogger(ExceptionHandlerAdvice.class); private static final Logger log = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
@ -30,19 +28,16 @@ public class ExceptionHandlerAdvice {
@ExceptionHandler({Exception.class}) @ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public BasicResultVO exceptionResponse(Exception e) { public BasicResultVO exceptionResponse(Exception e) {
StringWriter sw = new StringWriter(); BasicResultVO result = BasicResultVO.fail(RespStatusEnum.ERROR_500, "\r\n" + Throwables.getStackTrace(e) + "\r\n");
PrintWriter pw = new PrintWriter(sw); log.error(Throwables.getStackTrace(e));
e.printStackTrace(pw);
BasicResultVO result = BasicResultVO.fail(RespStatusEnum.ERROR_500, "\r\n" + sw + "\r\n");
log.error(e.getMessage(), e);
return result; return result;
} }
@ExceptionHandler({CommonException.class}) @ExceptionHandler({CommonException.class})
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public BasicResultVO commonResponse(CommonException ce) { public BasicResultVO commonResponse(CommonException ce) {
BasicResultVO result = BasicResultVO.fail(RespStatusEnum.ERROR_400, ce.getMessage()); BasicResultVO result = BasicResultVO.fail(RespStatusEnum.ERROR_400, Throwables.getStackTrace(ce));
log.error(ce.getMessage(), ce); log.error(Throwables.getStackTrace(ce));
return result; return result;
} }
} }

Loading…
Cancel
Save