|
@@ -2,7 +2,7 @@ package com.gis.web.controller;
|
|
|
|
|
|
import com.gis.common.exception.BaseRuntimeException;
|
|
import com.gis.common.exception.BaseRuntimeException;
|
|
import com.gis.common.util.Result;
|
|
import com.gis.common.util.Result;
|
|
-import lombok.extern.log4j.Log4j2;
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.shiro.ShiroException;
|
|
import org.apache.shiro.ShiroException;
|
|
import org.apache.shiro.authz.UnauthorizedException;
|
|
import org.apache.shiro.authz.UnauthorizedException;
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
@@ -17,12 +17,14 @@ import org.springframework.web.servlet.NoHandlerFoundException;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.validation.ConstraintViolationException;
|
|
import javax.validation.ConstraintViolationException;
|
|
import javax.validation.ValidationException;
|
|
import javax.validation.ValidationException;
|
|
|
|
+import java.io.PrintWriter;
|
|
|
|
+import java.io.StringWriter;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 统一捕捉异常,自定义返回参数
|
|
* 统一捕捉异常,自定义返回参数
|
|
* 这里只可以捕获controller层的异常。
|
|
* 这里只可以捕获controller层的异常。
|
|
*/
|
|
*/
|
|
-@Log4j2
|
|
|
|
|
|
+@Slf4j
|
|
@RestControllerAdvice
|
|
@RestControllerAdvice
|
|
public class ExceptionController {
|
|
public class ExceptionController {
|
|
|
|
|
|
@@ -45,15 +47,19 @@ public class ExceptionController {
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
|
@ExceptionHandler(UnauthorizedException.class)
|
|
@ExceptionHandler(UnauthorizedException.class)
|
|
public Result handle401() {
|
|
public Result handle401() {
|
|
- log.error("没有授权2");
|
|
|
|
- return Result.failure(5001, "没有授权");
|
|
|
|
|
|
+ log.error("没有权限");
|
|
|
|
+ return Result.failure(5003, "没有权限");
|
|
}
|
|
}
|
|
|
|
|
|
// 捕捉其他所有异常
|
|
// 捕捉其他所有异常
|
|
@ExceptionHandler(Exception.class)
|
|
@ExceptionHandler(Exception.class)
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
public Result globalException(HttpServletRequest request, Throwable ex) {
|
|
public Result globalException(HttpServletRequest request, Throwable ex) {
|
|
- log.error(ex);
|
|
|
|
|
|
+// System.out.println("111111111111111111");
|
|
|
|
+// log.error(ex.toString());
|
|
|
|
+ StringWriter trace=new StringWriter();
|
|
|
|
+ ex.printStackTrace(new PrintWriter(trace));
|
|
|
|
+ log.error(trace.toString());
|
|
return Result.failure(getStatus(request).value(), ex.getMessage());
|
|
return Result.failure(getStatus(request).value(), ex.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -71,7 +77,10 @@ public class ExceptionController {
|
|
@ResponseStatus(HttpStatus.OK)
|
|
@ResponseStatus(HttpStatus.OK)
|
|
public Result runtimeExceptionHandler(HttpServletRequest request, BaseRuntimeException e) {
|
|
public Result runtimeExceptionHandler(HttpServletRequest request, BaseRuntimeException e) {
|
|
log.error(request.getRequestURI() + ":" + e.getMsg());
|
|
log.error(request.getRequestURI() + ":" + e.getMsg());
|
|
-// return Result.failure(e.getCode() == null ? Result.CODE_FAILURE : e.getCode(), e.getMsg());
|
|
|
|
|
|
+ // by owen 2022-3-28 显示错误日志详情
|
|
|
|
+ if (e.getCode() != 0){
|
|
|
|
+ printErrorMsg(e);
|
|
|
|
+ }
|
|
return Result.failure(e.getCode(), e.getMsg());
|
|
return Result.failure(e.getCode(), e.getMsg());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -120,10 +129,20 @@ public class ExceptionController {
|
|
@ExceptionHandler(DuplicateKeyException.class)
|
|
@ExceptionHandler(DuplicateKeyException.class)
|
|
public Result handleDuplicateKeyException(DuplicateKeyException e) {
|
|
public Result handleDuplicateKeyException(DuplicateKeyException e) {
|
|
log.error(e.getMessage(), e);
|
|
log.error(e.getMessage(), e);
|
|
- return Result.failure(60005, "数据重复,请检查后提交");
|
|
|
|
|
|
+ return Result.failure(60005, "数据重复,请检查后提交: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * by owen 2022-4-1
|
|
|
|
+ * 打印错误详情
|
|
|
|
+ * @param e
|
|
|
|
+ */
|
|
|
|
+ private void printErrorMsg(BaseRuntimeException e){
|
|
|
|
+ StringWriter trace=new StringWriter();
|
|
|
|
+ e.printStackTrace(new PrintWriter(trace));
|
|
|
|
+ log.error(trace.toString());
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|