fix
This commit is contained in:
+33
-7
@@ -1,7 +1,6 @@
|
||||
package com.sunyard.ssp.common.exception;
|
||||
|
||||
import com.sunyard.chsm.model.R;
|
||||
import com.sunyard.chsm.utils.ThrowableUtils;
|
||||
import com.sunyard.ssp.common.Result;
|
||||
import com.sunyard.ssp.utils.ResultUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -13,13 +12,15 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
import java.util.IllegalFormatException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 全局controller异常处理
|
||||
* 全局controller异常处理
|
||||
*
|
||||
* @auther: ZS
|
||||
* @description:
|
||||
* @param:
|
||||
@@ -80,24 +81,49 @@ public class GlobalExceptionResolver {
|
||||
UnsupportedOperationException.class);
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public R<?> exceptionHandler(Exception ex) {
|
||||
String errorMessage = ThrowableUtils.buildErrorMessage(ex);
|
||||
SspwebException serviceException = ThrowableUtils.findException(SspwebException.class, ex);
|
||||
public R<?> exceptionHandler(Exception ex, HttpServletRequest request) {
|
||||
String errorMessage = buildErrorMessage(ex);
|
||||
SspwebException serviceException = findException(SspwebException.class, ex);
|
||||
if (serviceException != null) {
|
||||
log.warn("Validation failed -> {}, errorMessage: \n {}", serviceException.getMessage(), errorMessage);
|
||||
return R.error(400, serviceException.getMessage());
|
||||
}
|
||||
|
||||
for (Class<? extends Exception> eClass : EX_CLASS) {
|
||||
Exception exception = ThrowableUtils.findException(eClass, ex);
|
||||
Exception exception = findException(eClass, ex);
|
||||
if (exception == null) {
|
||||
continue;
|
||||
}
|
||||
log.warn("Validation failed -> {} ", errorMessage);
|
||||
return R.error(400, exception.getMessage());
|
||||
}
|
||||
log.error("系统异常 -> ", ex);
|
||||
log.error("request: {} - {}, 系统异常 -> ", request.getMethod(), request.getRequestURI(), ex);
|
||||
return R.error("系统异常");
|
||||
}
|
||||
|
||||
public static <T extends Exception> T findException(Class<T> exClass, Throwable ex) {
|
||||
for (int i = 0; ex != null && i < 3; i++) {
|
||||
if (exClass.isAssignableFrom(ex.getClass())) {
|
||||
//noinspection unchecked
|
||||
return (T) ex;
|
||||
}
|
||||
ex = ex.getCause();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String buildErrorMessage(Throwable ex) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(ex.getMessage());
|
||||
StackTraceElement[] stackTrace = ex.getStackTrace();
|
||||
int i = 0;
|
||||
for (StackTraceElement stackTraceElement : stackTrace) {
|
||||
if (i++ >= 5) {
|
||||
break;
|
||||
}
|
||||
sb.append("\n\t").append(stackTraceElement.toString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user