Commit 470adad7 authored by 赵啸非's avatar 赵啸非

添加排除事项

parent a68213f8
package com.mortals.xhx.base.framework.exception; package com.mortals.xhx.base.framework.exception;
import org.slf4j.Logger; import cn.hutool.core.util.StrUtil;
import org.slf4j.LoggerFactory; import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageNotReadableException;
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 com.alibaba.fastjson.JSONObject; import javax.servlet.http.HttpServletRequest;
import com.mortals.framework.exception.AppException; import javax.servlet.http.HttpServletResponse;
/** /**
* 统一异常处理 * 统一异常处理
*/ */
@ControllerAdvice @ControllerAdvice
@Slf4j
public class ExceptionHandle { public class ExceptionHandle {
private final static Logger log = LoggerFactory.getLogger(ExceptionHandle.class); @Autowired
private HttpServletRequest request;
@Autowired
private HttpServletResponse response;
public static final String KEY_RESULT_CODE = "code"; public static final String KEY_RESULT_CODE = "code";
public static final String KEY_RESULT_MSG = "msg"; public static final String KEY_RESULT_MSG = "msg";
...@@ -33,9 +43,15 @@ public class ExceptionHandle { ...@@ -33,9 +43,15 @@ public class ExceptionHandle {
stack.getClassName(), stack.getMethodName(), stack.getLineNumber(), e.getClass().getName()); stack.getClassName(), stack.getMethodName(), stack.getLineNumber(), e.getClass().getName());
AppException ex = (AppException) e; AppException ex = (AppException) e;
ret.put(KEY_RESULT_MSG, ex.getMessage()); ret.put(KEY_RESULT_MSG, ex.getMessage());
}
if (e instanceof HttpMessageNotReadableException) {
log.error("[system error]", e);
response.setStatus(HttpStatus.HTTP_BAD_REQUEST);
ret.put(KEY_RESULT_MSG, "参数错误,"+ StrUtil.subBefore(e.getMessage(), ";", false));
} else { } else {
log.error("[system error]", e); log.error("[system error]", e);
ret.put(KEY_RESULT_MSG, "unknown exception!"); response.setStatus(HttpStatus.HTTP_INTERNAL_ERROR);
ret.put(KEY_RESULT_MSG, "未知错误!" + e.getMessage());
} }
return ret.toJSONString(); return ret.toJSONString();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment