Browse Source

异常

yanyuan
zhizhi wu 4 years ago
parent
commit
54d6931921
  1. 5
      signin/src/main/java/com/ccsens/signin/api/UserController.java
  2. 23
      util/src/main/java/com/ccsens/util/config/ControllerExceptionHandler.java

5
signin/src/main/java/com/ccsens/signin/api/UserController.java

@ -258,8 +258,9 @@ public class UserController {
} catch (BaseException e) { } catch (BaseException e) {
if (e.getCode().intValue() == CodeEnum.MERGE_WX_PHONE.getCode().intValue()) { if (e.getCode().intValue() == CodeEnum.MERGE_WX_PHONE.getCode().intValue()) {
log.info("绑定手机号,需要合并:{},{}", phone, currentUserId); log.info("绑定手机号,需要合并:{},{}", phone, currentUserId);
JsonResponse ok = JsonResponse.newInstance().ok(e); JsonResponse ok = JsonResponse.newInstance().ok();
// ok.setMsg(e.getMessage()); ok.setCode(e.getCode());
ok.setMsg(e.getMessage());
UserVo.TokenBean tokenBean = new UserVo.TokenBean(); UserVo.TokenBean tokenBean = new UserVo.TokenBean();
tokenBean.setPhone(phone); tokenBean.setPhone(phone);
ok.setData(tokenBean); ok.setData(tokenBean);

23
util/src/main/java/com/ccsens/util/config/ControllerExceptionHandler.java

@ -1,10 +1,14 @@
package com.ccsens.util.config; package com.ccsens.util.config;
import com.alibaba.fastjson.JSONObject;
import com.ccsens.util.CodeEnum; import com.ccsens.util.CodeEnum;
import com.ccsens.util.HttpServletUtil;
import com.ccsens.util.JacksonUtil;
import com.ccsens.util.JsonResponse; import com.ccsens.util.JsonResponse;
import com.ccsens.util.exception.BaseException; import com.ccsens.util.exception.BaseException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
@ -13,8 +17,11 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; 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 javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
@Slf4j @Slf4j
@ControllerAdvice @ControllerAdvice
@ -67,13 +74,21 @@ public class ControllerExceptionHandler {
} }
@ExceptionHandler(value = BaseException.class) @ExceptionHandler(value = BaseException.class)
@ResponseBody // @ResponseBody
public JsonResponse jsonBaseExceptionHandler(HttpServletRequest req, BaseException e) { public void jsonBaseExceptionHandler(HttpServletRequest req, HttpServletResponse response, BaseException e) {
log.info("BaseException,code:{},message:{}", e.getCode(), e.getMessage()); log.info("BaseException,code:{},message:{}", e.getCode(), e.getMessage());
JsonResponse ok;
if (e.getCodeEnum() != null) { if (e.getCodeEnum() != null) {
return JsonResponse.newInstance().ok(e.getCodeEnum()); ok = JsonResponse.newInstance().ok(e.getCodeEnum());
} else {
ok = JsonResponse.newInstance().fail(e.getCode(),e.getMessage());
}
try {
HttpServletUtil.responseJson(response,
JacksonUtil.beanToJson(ok));
} catch (Exception e1) {
log.error("返回异常出错:", e);
} }
return JsonResponse.newInstance().fail(e.getCode(),e.getMessage());
} }
@ExceptionHandler(value = Exception.class) @ExceptionHandler(value = Exception.class)

Loading…
Cancel
Save