|
|
package com.yohoufo.common.controller.exceptionhandler;
|
|
|
|
|
|
import com.yoho.core.common.utils.HttpRequestUtils;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.error.exception.handler.ServiceGlobalExceptionHandler;
|
|
|
import com.yohoufo.common.exception.GatewayException;
|
|
|
import com.yohoufo.common.exception.SessionExpireException;
|
|
|
import com.yohoufo.common.utils.ServletUtils;
|
|
|
import org.apache.commons.httpclient.HttpStatus;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.text.MessageFormat;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Gateway全局异常处理。
|
|
|
* @author chunhua.zhang@yoho.cn
|
|
|
*
|
|
|
* 如果是 #{@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";
|
|
|
|
|
|
|
|
|
@ExceptionHandler(value = Exception.class)
|
|
|
public ModelAndView defaultErrorHandler(HttpServletRequest request, HttpServletResponse response, Exception e) throws Exception {
|
|
|
|
|
|
|
|
|
final String serviceName = ServletUtils.getServiceName(request);
|
|
|
final Map<String, Object> params = HttpRequestUtils.getRequestParams(request);
|
|
|
|
|
|
response.addHeader(YH_ERR_CODE_HEADER,"500");
|
|
|
|
|
|
//用户未登录,或登录 会话超时
|
|
|
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){
|
|
|
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) {
|
|
|
int code;
|
|
|
String desc;
|
|
|
if (e instanceof GatewayException) {
|
|
|
code = ((GatewayException) e).getErrorCode();
|
|
|
desc = ((GatewayException) e).getDesc();
|
|
|
} else { //服务异常,不能直接返回给客户端,必须映射一下
|
|
|
ServiceException serviceException = (ServiceException) e;
|
|
|
ServiceError serviceError = serviceException.getServiceError();
|
|
|
code = serviceError.getMappingGatewayError().getLeft();
|
|
|
desc = serviceError.getMappingGatewayError().getRight();
|
|
|
if (serviceException.getParams() != null) {
|
|
|
desc = MessageFormat.format(desc, serviceException.getParams());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
log.warn("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);
|
|
|
|
|
|
//其他异常,返回500
|
|
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
|
|
return new ModelAndView();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|