...
|
...
|
@@ -23,16 +23,16 @@ import java.util.Map; |
|
|
|
|
|
/**
|
|
|
* Gateway全局异常处理。
|
|
|
* @author chunhua.zhang@yoho.cn
|
|
|
*
|
|
|
* @author chunhua.zhang@yoho.cn
|
|
|
* <p>
|
|
|
* 如果是 #{@link ServiceException} 或者 #{@link GatewayException}, 则返回200,并且返回json消息体
|
|
|
*
|
|
|
*/
|
|
|
@ControllerAdvice
|
|
|
public class GlobalDefaultExceptionHandler {
|
|
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
private static String YH_ERR_CODE_HEADER="X-YH-Code";
|
|
|
private static String YH_ERR_CODE_HEADER = "X-YH-Code";
|
|
|
|
|
|
|
|
|
@ExceptionHandler(value = Exception.class)
|
...
|
...
|
@@ -40,33 +40,37 @@ public class GlobalDefaultExceptionHandler { |
|
|
|
|
|
|
|
|
final String serviceName = ServletUtils.getServiceName(request);
|
|
|
final Map<String, Object> params = HttpRequestUtils.getRequestParams(request);
|
|
|
final Map<String, Object> params = HttpRequestUtils.getRequestParams(request);
|
|
|
|
|
|
response.addHeader(YH_ERR_CODE_HEADER,"500");
|
|
|
response.addHeader(YH_ERR_CODE_HEADER, "500");
|
|
|
|
|
|
//用户未登录,或登录 会话超时
|
|
|
if( e instanceof SessionExpireException){
|
|
|
if (e instanceof SessionExpireException) {
|
|
|
log.info("session expire at url:{}, params:{}", serviceName, params);
|
|
|
response.setStatus(401);
|
|
|
return new ModelAndView();
|
|
|
}
|
|
|
|
|
|
//如果是请求URL匹配不了,则返回400
|
|
|
if(e instanceof UnsatisfiedServletRequestParameterException){
|
|
|
if (e instanceof UnsatisfiedServletRequestParameterException) {
|
|
|
log.warn("can not find validate request mapping at {}", request.getRequestURI());
|
|
|
response.setStatus(HttpStatus.SC_BAD_REQUEST);
|
|
|
return new ModelAndView();
|
|
|
}
|
|
|
|
|
|
//如果是业务异常,则返回http 200,并且构造json消息体中错误码&错误内容
|
|
|
if (e instanceof GatewayException || e instanceof ServiceException
|
|
|
|| e instanceof UfoServiceException || e instanceof Exception) {
|
|
|
if (e instanceof GatewayException
|
|
|
|| e instanceof ServiceException
|
|
|
|| e instanceof UfoServiceException
|
|
|
|| e instanceof Exception) {
|
|
|
int code;
|
|
|
String desc;
|
|
|
if (e instanceof GatewayException) {
|
|
|
code = ((GatewayException) e).getErrorCode();
|
|
|
desc = ((GatewayException) e).getDesc();
|
|
|
} else if(e instanceof ServiceException) { //服务异常,不能直接返回给客户端,必须映射一下
|
|
|
log.info("gateway exception happened at:{}, code:{}, desc:{}, uri:{}, request: {}, params is: {}",
|
|
|
serviceName, code, desc, request.getRequestURI(), serviceName, params);
|
|
|
} else if (e instanceof ServiceException) { //服务异常,不能直接返回给客户端,必须映射一下
|
|
|
ServiceException serviceException = (ServiceException) e;
|
|
|
ServiceError serviceError = serviceException.getServiceError();
|
|
|
code = serviceError.getMappingGatewayError().getLeft();
|
...
|
...
|
@@ -74,20 +78,23 @@ public class GlobalDefaultExceptionHandler { |
|
|
if (serviceException.getParams() != null) {
|
|
|
desc = MessageFormat.format(desc, serviceException.getParams());
|
|
|
}
|
|
|
}else if(e instanceof UfoServiceException){
|
|
|
log.info("service exception happened at:{}, code:{}, desc:{}, uri:{}, request: {}, params is: {}",
|
|
|
serviceName, code, desc, request.getRequestURI(), serviceName, params);
|
|
|
} else if (e instanceof UfoServiceException) {
|
|
|
code = ((UfoServiceException) e).getCode();
|
|
|
desc = ((UfoServiceException) e).getErrorMessage();
|
|
|
} else{
|
|
|
log.info("ufo service exception happened at:{}, code:{}, desc:{}, uri:{}, request: {}, params is: {}",
|
|
|
serviceName, code, desc, request.getRequestURI(), serviceName, params);
|
|
|
} else {
|
|
|
code = 500;
|
|
|
desc = "服务暂时异常,请稍等";
|
|
|
log.warn("exception happened at:{}, code:{}, desc:{}, uri:{}, request: {}, params is: {}",
|
|
|
serviceName, code, desc, request.getRequestURI(), serviceName, params, e);
|
|
|
}
|
|
|
|
|
|
log.info("service exception happened at:{}, code:{}, desc:{}, uri:{}, request: {}, params is: {}",
|
|
|
serviceName, code, desc, request.getRequestURI(), serviceName, params);
|
|
|
ModelAndView mv = ServiceGlobalExceptionHandler.getErrorJsonView(code, desc);
|
|
|
return mv;
|
|
|
}
|
|
|
log.warn("gateway other exception happened at uri:{}, request: {}, params is: {}, e is:{}", request.getRequestURI(), serviceName, params, e);
|
|
|
log.warn("gateway other exception happened at uri:{}, request: {}, params is: {}, e is:{}", request.getRequestURI(), serviceName, params, e);
|
|
|
|
|
|
//其他异常,返回500
|
|
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
...
|
...
|
@@ -95,6 +102,4 @@ public class GlobalDefaultExceptionHandler { |
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|