From cf28d4eab4477770ed878f603f7e3897dd73aaa5 Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 29 Aug 2019 17:03:27 +0800 Subject: [PATCH] 2.0 --- .../servercommon/bean/OpenContextImpl.java | 5 ++++ .../BaseServiceConfiguration.java | 6 ----- .../configuration/ErrorController.java | 27 ------------------- .../configuration/GlobalExceptionHandler.java | 24 ++++++++++------- 4 files changed, 20 insertions(+), 42 deletions(-) delete mode 100644 sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ErrorController.java diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java index 515b66e3..93efe8ea 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java @@ -114,4 +114,9 @@ public class OpenContextImpl implements OpenContext { } return JSON.parseObject(bizContent, clazz); } + + @Override + public String toString() { + return rootJsonObject.toString(); + } } diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServiceConfiguration.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServiceConfiguration.java index 11a196a9..102070cb 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServiceConfiguration.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServiceConfiguration.java @@ -76,12 +76,6 @@ public class BaseServiceConfiguration extends WebMvcConfigurationSupport return new GlobalExceptionHandler(); } - @Bean - @ConditionalOnMissingBean - ErrorController errorController() { - return new ErrorController(); - } - @Bean @ConditionalOnMissingBean ServiceRouteController serviceRouteInfoHandler() { diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ErrorController.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ErrorController.java deleted file mode 100644 index 2fdb30f6..00000000 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ErrorController.java +++ /dev/null @@ -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("系统繁忙")); - } -} diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/GlobalExceptionHandler.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/GlobalExceptionHandler.java index c5480e29..dda57c73 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/GlobalExceptionHandler.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/GlobalExceptionHandler.java @@ -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("
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) {