pull/1/head
tanghc 5 years ago
parent ec32e8b39f
commit cf28d4eab4
  1. 5
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java
  2. 6
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServiceConfiguration.java
  3. 27
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ErrorController.java
  4. 24
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/GlobalExceptionHandler.java

@ -114,4 +114,9 @@ public class OpenContextImpl<T> implements OpenContext<T> {
}
return JSON.parseObject(bizContent, clazz);
}
@Override
public String toString() {
return rootJsonObject.toString();
}
}

@ -76,12 +76,6 @@ public class BaseServiceConfiguration extends WebMvcConfigurationSupport
return new GlobalExceptionHandler();
}
@Bean
@ConditionalOnMissingBean
ErrorController errorController() {
return new ErrorController();
}
@Bean
@ConditionalOnMissingBean
ServiceRouteController serviceRouteInfoHandler() {

@ -1,27 +0,0 @@
package com.gitee.sop.servercommon.configuration;
import com.gitee.sop.servercommon.bean.ServiceConfig;
import com.gitee.sop.servercommon.exception.ServiceException;
import com.gitee.sop.servercommon.result.ServiceResultBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author tanghc
*/
@RestController
public class ErrorController {
@RequestMapping("/error")
public Object error(HttpServletRequest request, HttpServletResponse response) {
return getResult(request, response);
}
protected Object getResult(HttpServletRequest request, HttpServletResponse response) {
ServiceResultBuilder serviceResultBuilder = ServiceConfig.getInstance().getServiceResultBuilder();
return serviceResultBuilder.buildError(request, response, new ServiceException("系统繁忙"));
}
}

@ -1,6 +1,8 @@
package com.gitee.sop.servercommon.configuration;
import com.gitee.sop.servercommon.bean.OpenContext;
import com.gitee.sop.servercommon.bean.ServiceConfig;
import com.gitee.sop.servercommon.bean.ServiceContext;
import com.gitee.sop.servercommon.exception.ServiceException;
import com.gitee.sop.servercommon.result.ServiceResultBuilder;
import lombok.extern.slf4j.Slf4j;
@ -14,6 +16,7 @@ import javax.servlet.http.HttpServletResponse;
/**
* 全局异常处理
*
* @author tanghc
*/
@ControllerAdvice
@ -40,6 +43,7 @@ public class GlobalExceptionHandler {
/**
* 捕获手动抛出的异常
*
* @param request
* @param response
* @param exception
@ -54,22 +58,24 @@ public class GlobalExceptionHandler {
/**
* 捕获未知异常
* @param request
* @param response
* @param exception
* @return
*
* @param request request
* @param response response
* @param exception 异常信息
* @return 返回提示信息
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
response.addHeader(X_SERVICE_ERROR_CODE, String.valueOf(SYSTEM_ERROR_CODE));
log.error("系统错误", exception);
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
log.error("系统错误, openContext:{}", openContext == null ? "null" : openContext, exception);
StringBuilder msg = new StringBuilder();
msg.append(exception.getMessage());
StackTraceElement[] stackTrace = exception.getStackTrace();
// 取5行错误内容
int lineCount = 5;
for (int i = 0; i < stackTrace.length && i < lineCount ; i++) {
for (int i = 0; i < stackTrace.length && i < lineCount; i++) {
StackTraceElement stackTraceElement = stackTrace[i];
msg.append("<br> at ").append(stackTraceElement.toString());
}
@ -80,9 +86,9 @@ public class GlobalExceptionHandler {
/**
* 处理异常
*
* @param request
* @param response
* @param exception
* @param request request
* @param response response
* @param exception 异常信息
* @return 返回最终结果
*/
protected Object processError(HttpServletRequest request, HttpServletResponse response, Exception exception) {

Loading…
Cancel
Save