pull/1/head
tanghc 5 years ago
parent cf28d4eab4
commit c240a6337a
  1. 24
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/GlobalExceptionHandler.java

@ -26,13 +26,17 @@ public class GlobalExceptionHandler {
/** /**
* 与网关约定好的状态码表示业务出错 * 与网关约定好的状态码表示业务出错
*/ */
public static final int BIZ_ERROR_CODE = 4000; private static final int BIZ_ERROR_CODE = 4000;
/** /**
* 系统错误 * 与网关约定好的系统错误状态码
*/ */
public static final int SYSTEM_ERROR_CODE = 5050; private static final int SYSTEM_ERROR_CODE = 5050;
public static final String X_SERVICE_ERROR_CODE = "x-service-error-code"; /**
* 返回错误信息headerName
*/
private static final String X_SERVICE_ERROR_HEADER_NAME = "x-service-error-code";
@RequestMapping("/error") @RequestMapping("/error")
@ResponseBody @ResponseBody
@ -44,15 +48,15 @@ public class GlobalExceptionHandler {
/** /**
* 捕获手动抛出的异常 * 捕获手动抛出的异常
* *
* @param request * @param request request
* @param response * @param response response
* @param exception * @param exception 异常信息
* @return * @return 返回提示信息
*/ */
@ExceptionHandler(ServiceException.class) @ExceptionHandler(ServiceException.class)
@ResponseBody @ResponseBody
public Object serviceExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) { public Object serviceExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
response.addHeader(X_SERVICE_ERROR_CODE, String.valueOf(BIZ_ERROR_CODE)); response.addHeader(X_SERVICE_ERROR_HEADER_NAME, String.valueOf(BIZ_ERROR_CODE));
return this.processError(request, response, exception); return this.processError(request, response, exception);
} }
@ -67,7 +71,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
@ResponseBody @ResponseBody
public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) { public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
response.addHeader(X_SERVICE_ERROR_CODE, String.valueOf(SYSTEM_ERROR_CODE)); response.addHeader(X_SERVICE_ERROR_HEADER_NAME, String.valueOf(SYSTEM_ERROR_CODE));
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext(); OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
log.error("系统错误, openContext:{}", openContext == null ? "null" : openContext, exception); log.error("系统错误, openContext:{}", openContext == null ? "null" : openContext, exception);
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();

Loading…
Cancel
Save