Authored by LUOXC

change log

@@ -23,16 +23,16 @@ import java.util.Map; @@ -23,16 +23,16 @@ import java.util.Map;
23 23
24 /** 24 /**
25 * Gateway全局异常处理。 25 * Gateway全局异常处理。
26 - * @author chunhua.zhang@yoho.cn  
27 * 26 *
  27 + * @author chunhua.zhang@yoho.cn
  28 + * <p>
28 * 如果是 #{@link ServiceException} 或者 #{@link GatewayException}, 则返回200,并且返回json消息体 29 * 如果是 #{@link ServiceException} 或者 #{@link GatewayException}, 则返回200,并且返回json消息体
29 - *  
30 */ 30 */
31 @ControllerAdvice 31 @ControllerAdvice
32 public class GlobalDefaultExceptionHandler { 32 public class GlobalDefaultExceptionHandler {
33 private final Logger log = LoggerFactory.getLogger(getClass()); 33 private final Logger log = LoggerFactory.getLogger(getClass());
34 34
35 - private static String YH_ERR_CODE_HEADER="X-YH-Code"; 35 + private static String YH_ERR_CODE_HEADER = "X-YH-Code";
36 36
37 37
38 @ExceptionHandler(value = Exception.class) 38 @ExceptionHandler(value = Exception.class)
@@ -42,31 +42,35 @@ public class GlobalDefaultExceptionHandler { @@ -42,31 +42,35 @@ public class GlobalDefaultExceptionHandler {
42 final String serviceName = ServletUtils.getServiceName(request); 42 final String serviceName = ServletUtils.getServiceName(request);
43 final Map<String, Object> params = HttpRequestUtils.getRequestParams(request); 43 final Map<String, Object> params = HttpRequestUtils.getRequestParams(request);
44 44
45 - response.addHeader(YH_ERR_CODE_HEADER,"500"); 45 + response.addHeader(YH_ERR_CODE_HEADER, "500");
46 46
47 //用户未登录,或登录 会话超时 47 //用户未登录,或登录 会话超时
48 - if( e instanceof SessionExpireException){ 48 + if (e instanceof SessionExpireException) {
49 log.info("session expire at url:{}, params:{}", serviceName, params); 49 log.info("session expire at url:{}, params:{}", serviceName, params);
50 response.setStatus(401); 50 response.setStatus(401);
51 return new ModelAndView(); 51 return new ModelAndView();
52 } 52 }
53 53
54 //如果是请求URL匹配不了,则返回400 54 //如果是请求URL匹配不了,则返回400
55 - if(e instanceof UnsatisfiedServletRequestParameterException){ 55 + if (e instanceof UnsatisfiedServletRequestParameterException) {
56 log.warn("can not find validate request mapping at {}", request.getRequestURI()); 56 log.warn("can not find validate request mapping at {}", request.getRequestURI());
57 response.setStatus(HttpStatus.SC_BAD_REQUEST); 57 response.setStatus(HttpStatus.SC_BAD_REQUEST);
58 return new ModelAndView(); 58 return new ModelAndView();
59 } 59 }
60 60
61 //如果是业务异常,则返回http 200,并且构造json消息体中错误码&错误内容 61 //如果是业务异常,则返回http 200,并且构造json消息体中错误码&错误内容
62 - if (e instanceof GatewayException || e instanceof ServiceException  
63 - || e instanceof UfoServiceException || e instanceof Exception) { 62 + if (e instanceof GatewayException
  63 + || e instanceof ServiceException
  64 + || e instanceof UfoServiceException
  65 + || e instanceof Exception) {
64 int code; 66 int code;
65 String desc; 67 String desc;
66 if (e instanceof GatewayException) { 68 if (e instanceof GatewayException) {
67 code = ((GatewayException) e).getErrorCode(); 69 code = ((GatewayException) e).getErrorCode();
68 desc = ((GatewayException) e).getDesc(); 70 desc = ((GatewayException) e).getDesc();
69 - } else if(e instanceof ServiceException) { //服务异常,不能直接返回给客户端,必须映射一下 71 + log.info("gateway exception happened at:{}, code:{}, desc:{}, uri:{}, request: {}, params is: {}",
  72 + serviceName, code, desc, request.getRequestURI(), serviceName, params);
  73 + } else if (e instanceof ServiceException) { //服务异常,不能直接返回给客户端,必须映射一下
70 ServiceException serviceException = (ServiceException) e; 74 ServiceException serviceException = (ServiceException) e;
71 ServiceError serviceError = serviceException.getServiceError(); 75 ServiceError serviceError = serviceException.getServiceError();
72 code = serviceError.getMappingGatewayError().getLeft(); 76 code = serviceError.getMappingGatewayError().getLeft();
@@ -74,16 +78,19 @@ public class GlobalDefaultExceptionHandler { @@ -74,16 +78,19 @@ public class GlobalDefaultExceptionHandler {
74 if (serviceException.getParams() != null) { 78 if (serviceException.getParams() != null) {
75 desc = MessageFormat.format(desc, serviceException.getParams()); 79 desc = MessageFormat.format(desc, serviceException.getParams());
76 } 80 }
77 - }else if(e instanceof UfoServiceException){ 81 + log.info("service exception happened at:{}, code:{}, desc:{}, uri:{}, request: {}, params is: {}",
  82 + serviceName, code, desc, request.getRequestURI(), serviceName, params);
  83 + } else if (e instanceof UfoServiceException) {
78 code = ((UfoServiceException) e).getCode(); 84 code = ((UfoServiceException) e).getCode();
79 desc = ((UfoServiceException) e).getErrorMessage(); 85 desc = ((UfoServiceException) e).getErrorMessage();
80 - } else{ 86 + log.info("ufo service exception happened at:{}, code:{}, desc:{}, uri:{}, request: {}, params is: {}",
  87 + serviceName, code, desc, request.getRequestURI(), serviceName, params);
  88 + } else {
81 code = 500; 89 code = 500;
82 desc = "服务暂时异常,请稍等"; 90 desc = "服务暂时异常,请稍等";
  91 + log.warn("exception happened at:{}, code:{}, desc:{}, uri:{}, request: {}, params is: {}",
  92 + serviceName, code, desc, request.getRequestURI(), serviceName, params, e);
83 } 93 }
84 -  
85 - log.info("service exception happened at:{}, code:{}, desc:{}, uri:{}, request: {}, params is: {}",  
86 - serviceName, code, desc, request.getRequestURI(), serviceName, params);  
87 ModelAndView mv = ServiceGlobalExceptionHandler.getErrorJsonView(code, desc); 94 ModelAndView mv = ServiceGlobalExceptionHandler.getErrorJsonView(code, desc);
88 return mv; 95 return mv;
89 } 96 }
@@ -95,6 +102,4 @@ public class GlobalDefaultExceptionHandler { @@ -95,6 +102,4 @@ public class GlobalDefaultExceptionHandler {
95 } 102 }
96 103
97 104
98 -  
99 -  
100 } 105 }