|
@@ -1,14 +1,24 @@
|
|
|
package com.mooctest.crowd.site.controller.advice;
|
|
|
|
|
|
+import com.mooctest.crowd.domain.domainobject.ExceptionLog;
|
|
|
import com.mooctest.crowd.domain.exception.*;
|
|
|
import com.mooctest.crowd.site.constants.ResponseConstant;
|
|
|
+import com.mooctest.crowd.site.service.ExceptionLogService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Enumeration;
|
|
|
|
|
|
/**
|
|
|
* @author: Diors.Po
|
|
@@ -18,11 +28,13 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
@Slf4j
|
|
|
@RestControllerAdvice
|
|
|
public class ExceptionAdvice {
|
|
|
+ @Autowired
|
|
|
+ private ExceptionLogService exceptionLogService;
|
|
|
|
|
|
@ExceptionHandler(BaseException.class)
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
public String handleException(Exception e){
|
|
|
- log.error("访问出错:"+e.getMessage(), e);
|
|
|
+ logException(e);
|
|
|
if (e instanceof AccountNotExistException){
|
|
|
return ResponseConstant.USER_NOT_EXISTS;
|
|
|
} else if (e instanceof NullPointerException){
|
|
@@ -63,14 +75,14 @@ public class ExceptionAdvice {
|
|
|
@ExceptionHandler(UnauthorizedException.class)
|
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
|
|
public String handleUnauthorized(UnauthorizedException e){
|
|
|
- log.info("401:未经认证的请求");
|
|
|
+ logException(e);
|
|
|
return e.getMessage();
|
|
|
}
|
|
|
|
|
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
|
|
|
public String handlMethodNotAllowdd(HttpRequestMethodNotSupportedException e){
|
|
|
- log.info("method not allowdd", e);
|
|
|
+ logException(e);
|
|
|
return e.getMessage();
|
|
|
}
|
|
|
|
|
@@ -78,7 +90,7 @@ public class ExceptionAdvice {
|
|
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
@ResponseBody
|
|
|
public String handleSystemException(Exception e){
|
|
|
- log.error("System Error: "+e.getMessage(), e);
|
|
|
+ logException(e);
|
|
|
// return "系统异常 " + e.getMessage();
|
|
|
return e.getMessage();
|
|
|
}
|
|
@@ -86,7 +98,39 @@ public class ExceptionAdvice {
|
|
|
@ExceptionHandler(Excel2ProjectException.class)
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
public String handleExcel2ProjectException(Excel2ProjectException e){
|
|
|
- log.error("Excel表中存在错误:"+e.getErrorLogs());
|
|
|
+ logException(e);
|
|
|
return "Excel表中存在错误:"+ e.getErrorLogs();
|
|
|
}
|
|
|
+
|
|
|
+ public void logException(Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ HttpServletRequest request = requestAttributes.getRequest();
|
|
|
+ String exception = ExceptionUtils.getStackTrace(e);
|
|
|
+ String uri = request.getRequestURI();
|
|
|
+ Enumeration<String> paramNames = request.getParameterNames();
|
|
|
+ StringBuilder paramSb = new StringBuilder();
|
|
|
+ while(paramNames.hasMoreElements()) {
|
|
|
+ String paramName = paramNames.nextElement();
|
|
|
+ String[] paramVals = request.getParameterValues(paramName);
|
|
|
+ if (paramName.equals("password")) {
|
|
|
+ paramVals[0] = "******";
|
|
|
+ }
|
|
|
+ String paramValues = StringUtils.joinWith("$$", paramVals);
|
|
|
+ paramSb.append(paramName).append(paramValues).append("&");
|
|
|
+ }
|
|
|
+ String params = "";
|
|
|
+ if (paramSb.length() > 1) {
|
|
|
+ params = paramSb.substring(0, paramSb.length() - 1);
|
|
|
+ }
|
|
|
+ ExceptionLog exceptionLog = new ExceptionLog();
|
|
|
+ exceptionLog.setException(exception);
|
|
|
+ exceptionLog.setUri(uri);
|
|
|
+ exceptionLog.setParams(params);
|
|
|
+ try {
|
|
|
+ exceptionLogService.save(exceptionLog);
|
|
|
+ } catch (Exception e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|