修改http异常

This commit is contained in:
wangkang
2017-07-29 10:18:34 +08:00
parent 1ccb9d96c5
commit 697387cb27
4 changed files with 28 additions and 28 deletions
@@ -20,7 +20,7 @@ public enum ErrorCode {
/**
* 描述 : http服务错误
*/
HTTP_SERVER_ERROR("http server error"),
HTTP_ERROR("http error"),
/**
* 描述 : 参数校验错误
*/
@@ -74,14 +74,14 @@ public class ErrorMessage implements Serializable {
/**
* 描述 : http服务错误
*/
private HttpServerErrorMessage httpServerErrorMessage;
private HttpErrorMessage httpServerErrorMessage;
/**
* 描述 : 获取httpServerErrorMessage
*
* @return the httpServerErrorMessage
*/
public HttpServerErrorMessage getHttpServerErrorMessage() {
public HttpErrorMessage getHttpServerErrorMessage() {
return httpServerErrorMessage;
}
@@ -90,7 +90,7 @@ public class ErrorMessage implements Serializable {
*
* @param httpServerErrorMessage the httpServerErrorMessage to set
*/
public void setHttpServerErrorMessage(HttpServerErrorMessage httpServerErrorMessage) {
public void setHttpServerErrorMessage(HttpErrorMessage httpServerErrorMessage) {
this.httpServerErrorMessage = httpServerErrorMessage;
}
@@ -1,5 +1,5 @@
/**
* HttpServerErrorMessage.java
* HttpErrorMessage.java
* Created at 2016-11-05
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
@@ -13,7 +13,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientResponseException;
import com.egridcloud.udf.core.RestResponse;
import com.fasterxml.jackson.core.JsonParseException;
@@ -27,12 +27,12 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author wangkang
*
*/
public class HttpServerErrorMessage implements Serializable {
public class HttpErrorMessage implements Serializable {
/**
* 描述 : 日志
*/
private static final Logger LOGGER = LoggerFactory.getLogger(HttpServerErrorMessage.class);
private static final Logger LOGGER = LoggerFactory.getLogger(HttpErrorMessage.class);
/**
* 描述 : ID
@@ -67,20 +67,20 @@ public class HttpServerErrorMessage implements Serializable {
/**
* 描述 : 构造函数
*
* @param httpServerErrorException httpServerErrorException
* @param restClientResponseException restClientResponseException
* @param xssObjectMapper xssObjectMapper
*/
public HttpServerErrorMessage(HttpServerErrorException httpServerErrorException,
public HttpErrorMessage(RestClientResponseException restClientResponseException,
ObjectMapper xssObjectMapper) {
rawStatusCode = httpServerErrorException.getRawStatusCode();
responseHeaders = httpServerErrorException.getResponseHeaders();
statusCode = httpServerErrorException.getStatusCode();
statusText = httpServerErrorException.getStatusText();
rawStatusCode = restClientResponseException.getRawStatusCode();
responseHeaders = restClientResponseException.getResponseHeaders();
statusCode = HttpStatus.valueOf(rawStatusCode);
statusText = restClientResponseException.getStatusText();
try {
JavaType javaType = xssObjectMapper.getTypeFactory()
.constructParametricType(RestResponse.class, ErrorMessage.class);
responseBody =
xssObjectMapper.readValue(httpServerErrorException.getResponseBodyAsString(), javaType);
responseBody = xssObjectMapper
.readValue(restClientResponseException.getResponseBodyAsString(), javaType);
} catch (JsonParseException e) {
LOGGER.error("JsonParseException:", e);
} catch (JsonMappingException e1) {
@@ -19,7 +19,7 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.servlet.NoHandlerFoundException;
import com.egridcloud.udf.core.ApplicationConfig;
@@ -27,7 +27,7 @@ import com.egridcloud.udf.core.RestResponse;
import com.egridcloud.udf.core.exception.AuthException;
import com.egridcloud.udf.core.exception.ErrorCode;
import com.egridcloud.udf.core.exception.ErrorMessage;
import com.egridcloud.udf.core.exception.HttpServerErrorMessage;
import com.egridcloud.udf.core.exception.HttpErrorMessage;
import com.egridcloud.udf.core.exception.NoResourceFoundMessage;
import com.egridcloud.udf.core.exception.ParameterValidErrorMessage;
import com.egridcloud.udf.core.exception.ParameterValidException;
@@ -127,24 +127,24 @@ public class ExceptionMapping {
}
/**
* 描述 : 捕获HttpServerErrorException异常(http服务异常)
* 描述 : 捕获RestClientResponseException异常(http服务异常)
*
* @param httpServerErrorException 异常
* @param restClientResponseException 异常
* @return 错误信息
*/
@ExceptionHandler(value = HttpServerErrorException.class)
@ExceptionHandler(value = RestClientResponseException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public RestResponse<ErrorMessage> httpServerErrorException(
HttpServerErrorException httpServerErrorException) {
String returnCode = ErrorCode.HTTP_SERVER_ERROR.name();
String returnMessage = ErrorCode.HTTP_SERVER_ERROR.value();
public RestResponse<ErrorMessage> restClientResponseException(
RestClientResponseException restClientResponseException) {
String returnCode = ErrorCode.HTTP_ERROR.name();
String returnMessage = ErrorCode.HTTP_ERROR.value();
ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(),
returnCode, returnMessage, applicationConfig.isOutputExceptionStackTrace(),
httpServerErrorException);
restClientResponseException);
em.setHttpServerErrorMessage(
new HttpServerErrorMessage(httpServerErrorException, xssObjectMapper));
LOGGER.error(em.getId(), httpServerErrorException);
new HttpErrorMessage(restClientResponseException, xssObjectMapper));
LOGGER.error(em.getId(), restClientResponseException);
return new RestResponse<>(returnCode, returnMessage, em);
}