Browse Source

异常拦截器 2023年2月24日 sxm

master
[li_she] 3 years ago
parent
commit
60f2fa8010
  1. 54
      xujie-pms/src/main/java/com/xujie/pms/common/exception/XJExceptionHandler.java

54
xujie-pms/src/main/java/com/xujie/pms/common/exception/XJExceptionHandler.java

@ -0,0 +1,54 @@
package com.xujie.pms.common.exception;
import com.xujie.common.exception.XJException;
import com.xujie.common.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
/**
* 异常处理器
*/
@RestControllerAdvice
public class XJExceptionHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 处理自定义异常
*/
@ExceptionHandler(XJException.class)
public R handleRRException(XJException e) {
R r = new R();
r.put("code", e.getCode());
r.put("msg", e.getMessage());
e.printStackTrace();
return r;
}
@ExceptionHandler(NoHandlerFoundException.class)
public R handlerNoFoundException(Exception e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
return R.error(404, "路径不存在,请检查路径是否正确");
}
@ExceptionHandler(DuplicateKeyException.class)
public R handleDuplicateKeyException(DuplicateKeyException e) {
logger.error(e.getMessage(), e);
String message = "";
message = "数据库存已在该记录!";
return R.error(message);
}
@ExceptionHandler(Exception.class)
public R handleException(Exception e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
return R.error(e.getMessage());
}
}
Loading…
Cancel
Save