This commit is contained in:
wangkang
2017-07-29 10:18:34 +08:00
parent 05dab4ab9a
commit 496119f37e
11 changed files with 1303 additions and 0 deletions
@@ -0,0 +1,66 @@
/**
* AuthException.java
* Created at 2016-03-01
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception;
/**
* <p>
* ClassName: AuthException
* </p>
* <p>
* Description: 认证异常
* </p>
* <p>
* Author: wangkang
* </p>
* <p>
* Date: 2016年3月23日
* </p>
*/
public class AuthException extends RuntimeException {
/**
* <p>
* Field serialVersionUID: 序列号
* </p>
*/
private static final long serialVersionUID = 1L;
/**
* <p>
* Description: 构造函数
* </p>
*
* @param message 异常信息
*/
public AuthException(String message) {
super(message);
}
/**
* <p>
* Description: 构造函数
* </p>
*
* @param cause 堆栈
*/
public AuthException(Throwable cause) {
super(cause);
}
/**
* <p>
* Description: 构造函数
* </p>
*
* @param message 异常信息
* @param cause 堆栈
*/
public AuthException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -0,0 +1,69 @@
/**
* ErrorCode.java
* Created at 2016-09-27
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception;
/**
* 描述 : 错误代码枚举
*
* @author wangkang
*
*/
public enum ErrorCode {
/**
* 描述 : 系统内部错误
*/
ERROR("system internal error"),
/**
* 描述 : http服务错误
*/
HTTP_SERVER_ERROR("http server error"),
/**
* 描述 : 参数校验错误
*/
PARAMETER_VALID_ERROR("parameter check error"),
/**
* 描述 : 资源不存在
*/
NO_RESOURCE_FOUND("resource does not exist"),
/**
* 描述 : 认证错误
*/
AUTH_ERROR("auth error"),
/**
* 描述 : 权限错误
*/
PERMISSION_ERROR("Permission error");
/**
* <p>
* Field value: 参数值
* </p>
*/
private String value = null;
/**
* <p>
* Description: 构造函数
* </p>
*
* @param value 构造函数
*/
private ErrorCode(String value) {
this.value = value;
}
/**
* <p>
* Description: 放回int值
* </p>
*
* @return value 放回int值
*/
public String value() {
return this.value;
}
}
@@ -0,0 +1,277 @@
/**
* ErrorMessage.java
* Created at 2016-09-27
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception;
import java.io.Serializable;
import java.util.Date;
/**
* 描述 : 错误信息
*
* @author wangkang
*
*/
public class ErrorMessage implements Serializable {
/**
* 描述 : id
*/
private static final long serialVersionUID = 1L;
/**
* 描述 : 错误ID
*/
private String id;
/**
* 描述 : http状态
*/
private int httpStatus;
/**
* 描述 : 错误代码
*/
private String errorCode;
/**
* 描述 : 错误描述
*/
private String errorMsg;
/**
* 描述 : 错误发生时间
*/
private Date errorDate;
/**
* 描述 : 异常类名
*/
private String exceptionType;
/**
* 描述 : 异常信息
*/
private String exceptionMessage;
/**
* 描述 : 详细异常堆栈信息
*/
private String exceptionStackTrace;
/**
* 描述 : 资源部不在错误信息
*/
private NoResourceFoundMessage noResourceFoundMessage;
/**
* 描述 : 参数校验错误信息
*/
private ParameterValidErrorMessage parameterValidErrorMessage;
/**
* 描述 : http服务错误
*/
private HttpServerErrorMessage httpServerErrorMessage;
/**
* 描述 : 获取httpServerErrorMessage
*
* @return the httpServerErrorMessage
*/
public HttpServerErrorMessage getHttpServerErrorMessage() {
return httpServerErrorMessage;
}
/**
* 描述 : 设置httpServerErrorMessage
*
* @param httpServerErrorMessage the httpServerErrorMessage to set
*/
public void setHttpServerErrorMessage(HttpServerErrorMessage httpServerErrorMessage) {
this.httpServerErrorMessage = httpServerErrorMessage;
}
/**
* 描述 : 获取noResourceFoundMessage
*
* @return the noResourceFoundMessage
*/
public NoResourceFoundMessage getNoResourceFoundMessage() {
return noResourceFoundMessage;
}
/**
* 描述 : 设置noResourceFoundMessage
*
* @param noResourceFoundMessage the noResourceFoundMessage to set
*/
public void setNoResourceFoundMessage(NoResourceFoundMessage noResourceFoundMessage) {
this.noResourceFoundMessage = noResourceFoundMessage;
}
/**
* 描述 : 获取parameterValidErrorMessage
*
* @return the parameterValidErrorMessage
*/
public ParameterValidErrorMessage getParameterValidErrorMessage() {
return parameterValidErrorMessage;
}
/**
* 描述 : 设置parameterValidErrorMessage
*
* @param parameterValidErrorMessage the parameterValidErrorMessage to set
*/
public void setParameterValidErrorMessage(ParameterValidErrorMessage parameterValidErrorMessage) {
this.parameterValidErrorMessage = parameterValidErrorMessage;
}
/**
* 描述 : 获取id
*
* @return the id
*/
public String getId() {
return id;
}
/**
* 描述 : 设置id
*
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* 描述 : 获取errorDate
*
* @return the errorDate
*/
public Date getErrorDate() {
return errorDate;
}
/**
* 描述 : 设置errorDate
*
* @param errorDate the errorDate to set
*/
public void setErrorDate(Date errorDate) {
this.errorDate = errorDate;
}
/**
* 描述 : 获取httpStatus
*
* @return the httpStatus
*/
public int getHttpStatus() {
return httpStatus;
}
/**
* 描述 : 设置httpStatus
*
* @param httpStatus the httpStatus to set
*/
public void setHttpStatus(int httpStatus) {
this.httpStatus = httpStatus;
}
/**
* 描述 : 获取errorCode
*
* @return the errorCode
*/
public String getErrorCode() {
return errorCode;
}
/**
* 描述 : 设置errorCode
*
* @param errorCode the errorCode to set
*/
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
/**
* 描述 : 获取errorMsg
*
* @return the errorMsg
*/
public String getErrorMsg() {
return errorMsg;
}
/**
* 描述 : 设置errorMsg
*
* @param errorMsg the errorMsg to set
*/
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
/**
* 描述 : 获取exceptionType
*
* @return the exceptionType
*/
public String getExceptionType() {
return exceptionType;
}
/**
* 描述 : 设置exceptionType
*
* @param exceptionType the exceptionType to set
*/
public void setExceptionType(String exceptionType) {
this.exceptionType = exceptionType;
}
/**
* 描述 : 获取exceptionMessage
*
* @return the exceptionMessage
*/
public String getExceptionMessage() {
return exceptionMessage;
}
/**
* 描述 : 设置exceptionMessage
*
* @param exceptionMessage the exceptionMessage to set
*/
public void setExceptionMessage(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
}
/**
* 描述 : 获取exceptionStackTrace
*
* @return the exceptionStackTrace
*/
public String getExceptionStackTrace() {
return exceptionStackTrace;
}
/**
* 描述 : 设置exceptionStackTrace
*
* @param exceptionStackTrace the exceptionStackTrace to set
*/
public void setExceptionStackTrace(String exceptionStackTrace) {
this.exceptionStackTrace = exceptionStackTrace;
}
}
@@ -0,0 +1,183 @@
/**
* HttpServerErrorMessage.java
* Created at 2016-11-05
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception;
import java.io.IOException;
import java.io.Serializable;
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 com.egridcloud.udf.core.RestResponse;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* 描述 : HttpServerErrorMessage
*
* @author wangkang
*
*/
public class HttpServerErrorMessage implements Serializable {
/**
* 描述 : 日志
*/
private static final Logger LOGGER = LoggerFactory.getLogger(HttpServerErrorMessage.class);
/**
* 描述 : ID
*/
private static final long serialVersionUID = 1L;
/**
* 描述 : rawStatusCode
*/
private int rawStatusCode;
/**
* 描述 : statusText
*/
private String statusText;
/**
* 描述 : responseBody
*/
private RestResponse<ErrorMessage> responseBody;
/**
* 描述 : responseHeaders
*/
private HttpHeaders responseHeaders;
/**
* 描述 : statusCode
*/
private HttpStatus statusCode;
/**
* 描述 : 构造函数
*
* @param httpServerErrorException httpServerErrorException
* @param xssObjectMapper xssObjectMapper
*/
public HttpServerErrorMessage(HttpServerErrorException httpServerErrorException,
ObjectMapper xssObjectMapper) {
rawStatusCode = httpServerErrorException.getRawStatusCode();
responseHeaders = httpServerErrorException.getResponseHeaders();
statusCode = httpServerErrorException.getStatusCode();
statusText = httpServerErrorException.getStatusText();
try {
JavaType javaType = xssObjectMapper.getTypeFactory()
.constructParametricType(RestResponse.class, ErrorMessage.class);
responseBody =
xssObjectMapper.readValue(httpServerErrorException.getResponseBodyAsString(), javaType);
} catch (JsonParseException e) {
LOGGER.error("JsonParseException:", e);
} catch (JsonMappingException e1) {
LOGGER.error("JsonMappingException:", e1);
} catch (IOException e2) {
LOGGER.error("IOException:", e2);
}
}
/**
* 描述 : 获取responseBody
*
* @return the responseBody
*/
public RestResponse<ErrorMessage> getResponseBody() {
return responseBody;
}
/**
* 描述 : 设置responseBody
*
* @param responseBody the responseBody to set
*/
public void setResponseBody(RestResponse<ErrorMessage> responseBody) {
this.responseBody = responseBody;
}
/**
* 描述 : 获取rawStatusCode
*
* @return the rawStatusCode
*/
public int getRawStatusCode() {
return rawStatusCode;
}
/**
* 描述 : 设置rawStatusCode
*
* @param rawStatusCode the rawStatusCode to set
*/
public void setRawStatusCode(int rawStatusCode) {
this.rawStatusCode = rawStatusCode;
}
/**
* 描述 : 获取statusText
*
* @return the statusText
*/
public String getStatusText() {
return statusText;
}
/**
* 描述 : 设置statusText
*
* @param statusText the statusText to set
*/
public void setStatusText(String statusText) {
this.statusText = statusText;
}
/**
* 描述 : 获取responseHeaders
*
* @return the responseHeaders
*/
public HttpHeaders getResponseHeaders() {
return responseHeaders;
}
/**
* 描述 : 设置responseHeaders
*
* @param responseHeaders the responseHeaders to set
*/
public void setResponseHeaders(HttpHeaders responseHeaders) {
this.responseHeaders = responseHeaders;
}
/**
* 描述 : 获取statusCode
*
* @return the statusCode
*/
public HttpStatus getStatusCode() {
return statusCode;
}
/**
* 描述 : 设置statusCode
*
* @param statusCode the statusCode to set
*/
public void setStatusCode(HttpStatus statusCode) {
this.statusCode = statusCode;
}
}
@@ -0,0 +1,107 @@
/**
* NoResourceFoundMessage.java
* Created at 2016-11-05
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception;
import java.io.Serializable;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.NoHandlerFoundException;
/**
* 描述 : NoResourceFoundMessage
*
* @author wangkang
*
*/
public class NoResourceFoundMessage implements Serializable {
/**
* 描述 : ID
*/
private static final long serialVersionUID = 1L;
/**
* 描述 : httpMethod
*/
private String httpMethod;
/**
* 描述 : requestURL
*/
private String requestUrl;
/**
* 描述 : headers
*/
private HttpHeaders headers;
/**
* 描述 : 构造函数
*
* @param noHandlerFoundException noHandlerFoundException
*/
public NoResourceFoundMessage(NoHandlerFoundException noHandlerFoundException) {
this.headers = noHandlerFoundException.getHeaders();
this.httpMethod = noHandlerFoundException.getHttpMethod();
this.requestUrl = noHandlerFoundException.getRequestURL();
}
/**
* 描述 : 获取httpMethod
*
* @return the httpMethod
*/
public String getHttpMethod() {
return httpMethod;
}
/**
* 描述 : 设置httpMethod
*
* @param httpMethod the httpMethod to set
*/
public void setHttpMethod(String httpMethod) {
this.httpMethod = httpMethod;
}
/**
* 描述 : 获取requestUrl
*
* @return the requestUrl
*/
public String getRequestUrl() {
return requestUrl;
}
/**
* 描述 : 设置requestUrl
*
* @param requestUrl the requestUrl to set
*/
public void setRequestUrl(String requestUrl) {
this.requestUrl = requestUrl;
}
/**
* 描述 : 获取headers
*
* @return the headers
*/
public HttpHeaders getHeaders() {
return headers;
}
/**
* 描述 : 设置headers
*
* @param headers the headers to set
*/
public void setHeaders(HttpHeaders headers) {
this.headers = headers;
}
}
@@ -0,0 +1,59 @@
/**
* ParameterValidErrorMessage.java
* Created at 2016-11-05
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception;
import java.io.Serializable;
import java.util.List;
import org.springframework.validation.ObjectError;
/**
* 描述 : ParameterValidErrorMessage
*
* @author wangkang
*
*/
public class ParameterValidErrorMessage implements Serializable {
/**
* 描述 : ID
*/
private static final long serialVersionUID = 1L;
/**
* 描述 : 参数静态校验错误信息
*/
private List<ObjectError> allErrors;
/**
* 描述 : 构造函数
*
* @param parameterValidException parameterValidException
*/
public ParameterValidErrorMessage(ParameterValidException parameterValidException) {
allErrors = parameterValidException.getAllErrors();
}
/**
* 描述 : 获取allErrors
*
* @return the allErrors
*/
public List<ObjectError> getAllErrors() {
return allErrors;
}
/**
* 描述 : 设置allErrors
*
* @param allErrors the allErrors to set
*/
public void setAllErrors(List<ObjectError> allErrors) {
this.allErrors = allErrors;
}
}
@@ -0,0 +1,74 @@
/**
* ParameterValidException.java
* Created at 2016-09-27
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception;
import java.util.List;
import org.springframework.validation.ObjectError;
/**
* 描述 : 参数校验错误
*
* @author wangkang
*
*/
public class ParameterValidException extends RuntimeException {
/**
* 描述 : ID
*/
private static final long serialVersionUID = 1L;
/**
* 描述 : 参数静态校验错误信息
*/
private final List<ObjectError> allErrors;
/**
* 描述 : 构造函数
*
* @param message 异常信息
* @param allErrors 参数静态校验错误信息
*/
public ParameterValidException(String message, List<ObjectError> allErrors) {
super(message);
this.allErrors = allErrors;
}
/**
* 描述 : 构造函数
*
* @param cause 堆栈
* @param allErrors 参数静态校验错误信息
*/
public ParameterValidException(Throwable cause, List<ObjectError> allErrors) {
super(cause);
this.allErrors = allErrors;
}
/**
* 描述 : 构造函数
*
* @param message 异常信息
* @param cause 堆栈
* @param allErrors 参数静态校验错误信息
*/
public ParameterValidException(String message, Throwable cause, List<ObjectError> allErrors) {
super(message, cause);
this.allErrors = allErrors;
}
/**
* 描述 : 获取allErrors
*
* @return the allErrors
*/
public List<ObjectError> getAllErrors() {
return allErrors;
}
}
@@ -0,0 +1,66 @@
/**
* PermissionException.java
* Created at 2016-03-01
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception;
/**
* <p>
* ClassName: PermissionException
* </p>
* <p>
* Description: 权限异常
* </p>
* <p>
* Author: wangkang
* </p>
* <p>
* Date: 2016年3月23日
* </p>
*/
public class PermissionException extends RuntimeException {
/**
* <p>
* Field serialVersionUID: 序列号
* </p>
*/
private static final long serialVersionUID = 1L;
/**
* <p>
* Description: 构造函数
* </p>
*
* @param message 异常信息
*/
public PermissionException(String message) {
super(message);
}
/**
* <p>
* Description: 构造函数
* </p>
*
* @param cause 堆栈
*/
public PermissionException(Throwable cause) {
super(cause);
}
/**
* <p>
* Description: 构造函数
* </p>
*
* @param message 异常信息
* @param cause 堆栈
*/
public PermissionException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -0,0 +1,64 @@
/**
* SystemException.java
* Created at 2016-03-01
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception;
/**
* <p>
* ClassName: SystemException
* </p>
* <p>
* Description: 系统异常
* </p>
* <p>
* Author: wangkang
* </p>
* <p>
* Date: 2016年3月23日
* </p>
*/
public class SystemException extends Exception {
/**
* 描述 : id
*/
private static final long serialVersionUID = 1L;
/**
* <p>
* Description: 构造函数
* </p>
*
* @param message 异常信息
*/
public SystemException(String message) {
super(message);
}
/**
* <p>
* Description: 构造函数
* </p>
*
* @param cause 堆栈
*/
public SystemException(Throwable cause) {
super(cause);
}
/**
* <p>
* Description: 构造函数
* </p>
*
* @param message 异常信息
* @param cause 堆栈
*/
public SystemException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -0,0 +1,66 @@
/**
* SystemRuntimeException.java
* Created at 2016-03-01
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception;
/**
* <p>
* ClassName: SystemRuntimeException
* </p>
* <p>
* Description: 系统运行时异常
* </p>
* <p>
* Author: wangkang
* </p>
* <p>
* Date: 2016年3月23日
* </p>
*/
public class SystemRuntimeException extends RuntimeException {
/**
* <p>
* Field serialVersionUID: 序列号
* </p>
*/
private static final long serialVersionUID = 1L;
/**
* <p>
* Description: 构造函数
* </p>
*
* @param message 异常信息
*/
public SystemRuntimeException(String message) {
super(message);
}
/**
* <p>
* Description: 构造函数
* </p>
*
* @param cause 堆栈
*/
public SystemRuntimeException(Throwable cause) {
super(cause);
}
/**
* <p>
* Description: 构造函数
* </p>
*
* @param message 异常信息
* @param cause 堆栈
*/
public SystemRuntimeException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -0,0 +1,272 @@
/**
* ExceptionMapping.java
* Created at 2016-09-20
* Created by wangkang
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core.exception.handle;
import java.util.Date;
import java.util.UUID;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.servlet.NoHandlerFoundException;
import com.egridcloud.udf.core.ApplicationConfig;
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.NoResourceFoundMessage;
import com.egridcloud.udf.core.exception.ParameterValidErrorMessage;
import com.egridcloud.udf.core.exception.ParameterValidException;
import com.egridcloud.udf.core.exception.PermissionException;
import com.egridcloud.udf.core.exception.SystemException;
import com.egridcloud.udf.core.exception.SystemRuntimeException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* 描述 : 全局异常处理
*
* @author wangkang
*
*/
@ControllerAdvice
public class ExceptionMapping {
/**
* 描述 : 日志
*/
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionMapping.class);
/**
* 描述 : 系统配置
*/
@Autowired
private ApplicationConfig applicationConfig;
/**
* 描述 : json解析器
*/
@Autowired
private ObjectMapper xssObjectMapper;
/**
* 描述 : 构造函数
*
*/
public ExceptionMapping() {
LOGGER.info("ExceptionMapping initialize");
}
/**
* 描述 : 捕获PermissionException异常(权限异常)
*
* @param permissionException 异常
* @return 错误信息
*/
@ExceptionHandler(value = PermissionException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
@ResponseBody
public RestResponse<ErrorMessage> permissionException(PermissionException permissionException) {
String returnCode = ErrorCode.PERMISSION_ERROR.name();
String returnMessage = ErrorCode.PERMISSION_ERROR.value();
ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.FORBIDDEN.value(), returnCode,
returnMessage, applicationConfig.isOutputExceptionStackTrace(), permissionException);
LOGGER.error(em.getId(), permissionException);
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 捕获AuthException异常(认证异常)
*
* @param authException 异常
* @return 错误信息
*/
@ExceptionHandler(value = AuthException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ResponseBody
public RestResponse<ErrorMessage> authException(AuthException authException) {
String returnCode = ErrorCode.AUTH_ERROR.name();
String returnMessage = ErrorCode.AUTH_ERROR.value();
ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.UNAUTHORIZED.value(),
returnCode, returnMessage, applicationConfig.isOutputExceptionStackTrace(), authException);
LOGGER.error(em.getId(), authException);
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 捕获ParameterValidException异常(参数校验错误)
*
* @param parameterValidException 异常
* @return 错误信息
*/
@ExceptionHandler(value = ParameterValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public RestResponse<ErrorMessage> parameterValidException(
ParameterValidException parameterValidException) {
String returnCode = ErrorCode.PARAMETER_VALID_ERROR.name();
String returnMessage = ErrorCode.PARAMETER_VALID_ERROR.value();
ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.BAD_REQUEST.value(), returnCode,
returnMessage, applicationConfig.isOutputExceptionStackTrace(), parameterValidException);
em.setParameterValidErrorMessage(new ParameterValidErrorMessage(parameterValidException));
LOGGER.error(em.getId(), parameterValidException);
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 捕获HttpServerErrorException异常(http服务异常)
*
* @param httpServerErrorException 异常
* @return 错误信息
*/
@ExceptionHandler(value = HttpServerErrorException.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();
ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(),
returnCode, returnMessage, applicationConfig.isOutputExceptionStackTrace(),
httpServerErrorException);
em.setHttpServerErrorMessage(
new HttpServerErrorMessage(httpServerErrorException, xssObjectMapper));
LOGGER.error(em.getId(), httpServerErrorException);
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 捕获NoHandlerFoundException异常(资源不存在)
*
* @param noHandlerFoundException 异常
* @return 错误信息
*/
@ExceptionHandler(value = NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public RestResponse<ErrorMessage> noHandlerFoundException(
NoHandlerFoundException noHandlerFoundException) {
String returnCode = ErrorCode.NO_RESOURCE_FOUND.name();
String returnMessage = ErrorCode.NO_RESOURCE_FOUND.value();
ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.NOT_FOUND.value(), returnCode,
returnMessage, applicationConfig.isOutputExceptionStackTrace(), noHandlerFoundException);
em.setNoResourceFoundMessage(new NoResourceFoundMessage(noHandlerFoundException));
LOGGER.error(em.getId(), noHandlerFoundException);
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 捕获SystemException异常(系统检查型异常)
*
* @param systemException 异常
* @return 错误信息
*/
@ExceptionHandler(value = SystemException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public RestResponse<ErrorMessage> systemException(SystemException systemException) {
String returnCode = ErrorCode.ERROR.name();
String returnMessage = ErrorCode.ERROR.value();
ErrorMessage em =
ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), returnCode,
returnMessage, applicationConfig.isOutputExceptionStackTrace(), systemException);
LOGGER.error(em.getId(), systemException);
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 捕获SystemRuntimeException异常(系统运行时型异常)
*
* @param systemRuntimeException 异常
* @return 错误信息
*/
@ExceptionHandler(value = SystemRuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public RestResponse<ErrorMessage> systemRuntimeException(
SystemRuntimeException systemRuntimeException) {
String returnCode = ErrorCode.ERROR.name();
String returnMessage = ErrorCode.ERROR.value();
ErrorMessage em =
ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), returnCode,
returnMessage, applicationConfig.isOutputExceptionStackTrace(), systemRuntimeException);
LOGGER.error(em.getId(), systemRuntimeException);
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 捕获Exception异常(检查型异常)
*
* @param exception 异常
* @return 错误信息
*/
@ExceptionHandler(value = Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public RestResponse<ErrorMessage> exception(Exception exception) {
String returnCode = ErrorCode.ERROR.name();
String returnMessage = ErrorCode.ERROR.value();
ErrorMessage em = ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(),
returnCode, returnMessage, applicationConfig.isOutputExceptionStackTrace(), exception);
LOGGER.error(em.getId(), exception);
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 捕获RuntimeException异常(运行时异常)
*
* @param runtimeException 异常
* @return 错误信息
*/
@ExceptionHandler(value = RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public RestResponse<ErrorMessage> runtimeException(RuntimeException runtimeException) {
String returnCode = ErrorCode.ERROR.name();
String returnMessage = ErrorCode.ERROR.value();
ErrorMessage em =
ExceptionMapping.buildErrorMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), returnCode,
returnMessage, applicationConfig.isOutputExceptionStackTrace(), runtimeException);
LOGGER.error(em.getId(), runtimeException);
return new RestResponse<>(returnCode, returnMessage, em);
}
/**
* 描述 : 构造错误信息(部分)
*
* @param httpStatus http状态
* @param errorCode 错误代码
* @param errorMessage 错误描述
* @param outputExceptionStackTrace 是否输出异常堆栈
* @param exception 异常
* @return 错误信息
*/
public static ErrorMessage buildErrorMessage(int httpStatus, String errorCode,
String errorMessage, boolean outputExceptionStackTrace, Exception exception) {
ErrorMessage em = new ErrorMessage();
em.setId(UUID.randomUUID().toString());
em.setHttpStatus(httpStatus);
em.setErrorCode(errorCode);
em.setErrorMsg(errorMessage);
em.setExceptionType(exception.getClass().getName());
em.setExceptionMessage(ExceptionUtils.getMessage(exception));
em.setErrorDate(new Date());
if (outputExceptionStackTrace) {
em.setExceptionStackTrace(ExceptionUtils.getStackTrace(exception));
}
return em;
}
}