From 496119f37e221c5c1a0eef8ff4b5c45edae5bb5f Mon Sep 17 00:00:00 2001 From: wangkang Date: Mon, 24 Apr 2017 10:28:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../udf/core/exception/AuthException.java | 66 +++++ .../udf/core/exception/ErrorCode.java | 69 +++++ .../udf/core/exception/ErrorMessage.java | 277 ++++++++++++++++++ .../exception/HttpServerErrorMessage.java | 183 ++++++++++++ .../exception/NoResourceFoundMessage.java | 107 +++++++ .../exception/ParameterValidErrorMessage.java | 59 ++++ .../exception/ParameterValidException.java | 74 +++++ .../core/exception/PermissionException.java | 66 +++++ .../udf/core/exception/SystemException.java | 64 ++++ .../exception/SystemRuntimeException.java | 66 +++++ .../exception/handle/ExceptionMapping.java | 272 +++++++++++++++++ 11 files changed, 1303 insertions(+) create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/AuthException.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorCode.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorMessage.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/HttpServerErrorMessage.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/NoResourceFoundMessage.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidErrorMessage.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidException.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/PermissionException.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/SystemException.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/SystemRuntimeException.java create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/handle/ExceptionMapping.java diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/AuthException.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/AuthException.java new file mode 100644 index 0000000..7e64b69 --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/AuthException.java @@ -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; + +/** + *

+ * ClassName: AuthException + *

+ *

+ * Description: 认证异常 + *

+ *

+ * Author: wangkang + *

+ *

+ * Date: 2016年3月23日 + *

+ */ +public class AuthException extends RuntimeException { + + /** + *

+ * Field serialVersionUID: 序列号 + *

+ */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Description: 构造函数 + *

+ * + * @param message 异常信息 + */ + public AuthException(String message) { + super(message); + } + + /** + *

+ * Description: 构造函数 + *

+ * + * @param cause 堆栈 + */ + public AuthException(Throwable cause) { + super(cause); + } + + /** + *

+ * Description: 构造函数 + *

+ * + * @param message 异常信息 + * @param cause 堆栈 + */ + public AuthException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorCode.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorCode.java new file mode 100644 index 0000000..b10157b --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorCode.java @@ -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"); + + /** + *

+ * Field value: 参数值 + *

+ */ + private String value = null; + + /** + *

+ * Description: 构造函数 + *

+ * + * @param value 构造函数 + */ + private ErrorCode(String value) { + this.value = value; + } + + /** + *

+ * Description: 放回int值 + *

+ * + * @return value 放回int值 + */ + public String value() { + return this.value; + } +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorMessage.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorMessage.java new file mode 100644 index 0000000..5215b65 --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ErrorMessage.java @@ -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; + } + +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/HttpServerErrorMessage.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/HttpServerErrorMessage.java new file mode 100644 index 0000000..fa960d9 --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/HttpServerErrorMessage.java @@ -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 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 getResponseBody() { + return responseBody; + } + + /** + * 描述 : 设置responseBody + * + * @param responseBody the responseBody to set + */ + public void setResponseBody(RestResponse 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; + } + +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/NoResourceFoundMessage.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/NoResourceFoundMessage.java new file mode 100644 index 0000000..f107b7a --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/NoResourceFoundMessage.java @@ -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; + } + +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidErrorMessage.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidErrorMessage.java new file mode 100644 index 0000000..d0348b8 --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidErrorMessage.java @@ -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 allErrors; + + /** + * 描述 : 构造函数 + * + * @param parameterValidException parameterValidException + */ + public ParameterValidErrorMessage(ParameterValidException parameterValidException) { + allErrors = parameterValidException.getAllErrors(); + } + + /** + * 描述 : 获取allErrors + * + * @return the allErrors + */ + public List getAllErrors() { + return allErrors; + } + + /** + * 描述 : 设置allErrors + * + * @param allErrors the allErrors to set + */ + public void setAllErrors(List allErrors) { + this.allErrors = allErrors; + } + +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidException.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidException.java new file mode 100644 index 0000000..69155a0 --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/ParameterValidException.java @@ -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 allErrors; + + /** + * 描述 : 构造函数 + * + * @param message 异常信息 + * @param allErrors 参数静态校验错误信息 + */ + public ParameterValidException(String message, List allErrors) { + super(message); + this.allErrors = allErrors; + } + + /** + * 描述 : 构造函数 + * + * @param cause 堆栈 + * @param allErrors 参数静态校验错误信息 + */ + public ParameterValidException(Throwable cause, List allErrors) { + super(cause); + this.allErrors = allErrors; + } + + /** + * 描述 : 构造函数 + * + * @param message 异常信息 + * @param cause 堆栈 + * @param allErrors 参数静态校验错误信息 + */ + public ParameterValidException(String message, Throwable cause, List allErrors) { + super(message, cause); + this.allErrors = allErrors; + } + + /** + * 描述 : 获取allErrors + * + * @return the allErrors + */ + public List getAllErrors() { + return allErrors; + } + +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/PermissionException.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/PermissionException.java new file mode 100644 index 0000000..9bfdf14 --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/PermissionException.java @@ -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; + +/** + *

+ * ClassName: PermissionException + *

+ *

+ * Description: 权限异常 + *

+ *

+ * Author: wangkang + *

+ *

+ * Date: 2016年3月23日 + *

+ */ +public class PermissionException extends RuntimeException { + + /** + *

+ * Field serialVersionUID: 序列号 + *

+ */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Description: 构造函数 + *

+ * + * @param message 异常信息 + */ + public PermissionException(String message) { + super(message); + } + + /** + *

+ * Description: 构造函数 + *

+ * + * @param cause 堆栈 + */ + public PermissionException(Throwable cause) { + super(cause); + } + + /** + *

+ * Description: 构造函数 + *

+ * + * @param message 异常信息 + * @param cause 堆栈 + */ + public PermissionException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/SystemException.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/SystemException.java new file mode 100644 index 0000000..8172119 --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/SystemException.java @@ -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; + +/** + *

+ * ClassName: SystemException + *

+ *

+ * Description: 系统异常 + *

+ *

+ * Author: wangkang + *

+ *

+ * Date: 2016年3月23日 + *

+ */ +public class SystemException extends Exception { + + /** + * 描述 : id + */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Description: 构造函数 + *

+ * + * @param message 异常信息 + */ + public SystemException(String message) { + super(message); + } + + /** + *

+ * Description: 构造函数 + *

+ * + * @param cause 堆栈 + */ + public SystemException(Throwable cause) { + super(cause); + } + + /** + *

+ * Description: 构造函数 + *

+ * + * @param message 异常信息 + * @param cause 堆栈 + */ + public SystemException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/SystemRuntimeException.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/SystemRuntimeException.java new file mode 100644 index 0000000..951d824 --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/SystemRuntimeException.java @@ -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; + +/** + *

+ * ClassName: SystemRuntimeException + *

+ *

+ * Description: 系统运行时异常 + *

+ *

+ * Author: wangkang + *

+ *

+ * Date: 2016年3月23日 + *

+ */ +public class SystemRuntimeException extends RuntimeException { + + /** + *

+ * Field serialVersionUID: 序列号 + *

+ */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Description: 构造函数 + *

+ * + * @param message 异常信息 + */ + public SystemRuntimeException(String message) { + super(message); + } + + /** + *

+ * Description: 构造函数 + *

+ * + * @param cause 堆栈 + */ + public SystemRuntimeException(Throwable cause) { + super(cause); + } + + /** + *

+ * Description: 构造函数 + *

+ * + * @param message 异常信息 + * @param cause 堆栈 + */ + public SystemRuntimeException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/handle/ExceptionMapping.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/handle/ExceptionMapping.java new file mode 100644 index 0000000..cfead5a --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/exception/handle/ExceptionMapping.java @@ -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 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 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 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 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 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 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 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 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 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; + } + +}