|
|
@@ -0,0 +1,165 @@
|
|
|
+package com.shkpr.service.customgateway.core.exception;
|
|
|
+
|
|
|
+import com.global.base.log.LogLevelFlag;
|
|
|
+import com.global.base.log.LogPrintMgr;
|
|
|
+import com.shkpr.service.customgateway.core.constants.ResponseCode;
|
|
|
+import com.shkpr.service.customgateway.core.domain.ResultResponse;
|
|
|
+import org.apache.catalina.connector.ClientAbortException;
|
|
|
+import org.springframework.beans.ConversionNotSupportedException;
|
|
|
+import org.springframework.beans.TypeMismatchException;
|
|
|
+import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
|
+import org.springframework.http.converter.HttpMessageNotWritableException;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
|
|
+import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Controller的全局异常捕获类
|
|
|
+ * 前置异常增强(在Controller的方法调用之前若有异常进行捕获),处理结果以JSON的形式返回给客服端
|
|
|
+ * 异常增强类型:NullPointerException,RunTimeException,ClassCastException,
|
|
|
+ * NoSuchMethodException,IOException,IndexOutOfBoundsException
|
|
|
+ * 以及springmvc自定义异常等,如下:
|
|
|
+ * SpringMVC自定义异常对应的status code
|
|
|
+ * Exception HTTP Status Code
|
|
|
+ * ConversionNotSupportedException 500 (Internal Server Error)
|
|
|
+ * HttpMessageNotWritableException 500 (Internal Server Error)
|
|
|
+ * HttpMediaTypeNotSupportedException 415 (Unsupported Media Type)
|
|
|
+ * HttpMediaTypeNotAcceptableException 406 (Not Acceptable)
|
|
|
+ * HttpRequestMethodNotSupportedException 405 (Method Not Allowed)
|
|
|
+ * NoSuchRequestHandlingMethodException 404 (Not Found)
|
|
|
+ * TypeMismatchException 400 (Bad Request)
|
|
|
+ * HttpMessageNotReadableException 400 (Bad Request)
|
|
|
+ * MissingServletRequestParameterException 400 (Bad Request)
|
|
|
+ *
|
|
|
+ */
|
|
|
+@ControllerAdvice
|
|
|
+@Component
|
|
|
+public class GlobalSelfExceptionHandler {
|
|
|
+
|
|
|
+ public static ResultResponse<String> createResParam(String strCode, String strMsg) {
|
|
|
+ ResultResponse<String> resResult = new ResultResponse<String>();
|
|
|
+ resResult.setTimestamp(System.currentTimeMillis());
|
|
|
+ resResult.setRescode(strCode);
|
|
|
+ resResult.setResmsg(strMsg);
|
|
|
+ resResult.setResdata("");
|
|
|
+ return resResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ResultResponse<String> createResParam(String strCode, String strMsg, String strData) {
|
|
|
+ ResultResponse<String> resResult = new ResultResponse<String>();
|
|
|
+ resResult.setTimestamp(System.currentTimeMillis());
|
|
|
+ resResult.setRescode(strCode);
|
|
|
+ resResult.setResmsg(strMsg);
|
|
|
+ resResult.setResdata((strData == null) ? "" : strData);
|
|
|
+ return resResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ //自定义异常
|
|
|
+ @ExceptionHandler(value = SelfException.class)
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> jsonErrorHandler(HttpServletRequest req, SelfException ex) throws Exception {
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_WARN
|
|
|
+ , "Self Exception"
|
|
|
+ , "GlobalSelfExceptionHandler"
|
|
|
+ , String.format("url:{%s}, code(%s):{%s}", req.getRequestURI(), ex.getStrErrorCode(), ex.getMessage()));
|
|
|
+ return createResParam(ex.getStrErrorCode(), ex.getMessage(), ex.getStrErrorData());
|
|
|
+ }
|
|
|
+
|
|
|
+ //运行时异常
|
|
|
+ @ExceptionHandler(RuntimeException.class)
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> runtimeExceptionHandler(RuntimeException runtimeException) {
|
|
|
+ LogPrintMgr.getInstance().printExceptionObject(LogLevelFlag.LOG_ERROR, runtimeException);
|
|
|
+ return createResParam(ResponseCode.STATUS_SERVER_RUN_ERROR.getCode() + "", ResponseCode.STATUS_SERVER_RUN_ERROR.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ //空指针异常
|
|
|
+ @ExceptionHandler(NullPointerException.class)
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> nullPointerExceptionHandler(NullPointerException ex) {
|
|
|
+ LogPrintMgr.getInstance().printExceptionObject(LogLevelFlag.LOG_ERROR, ex);
|
|
|
+ return createResParam(ResponseCode.STATUS_NULL_POINT_EXCEPTION.getCode() + "", ResponseCode.STATUS_NULL_POINT_EXCEPTION.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ //类型转换异常
|
|
|
+ @ExceptionHandler(ClassCastException.class)
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> classCastExceptionHandler(ClassCastException ex) {
|
|
|
+ LogPrintMgr.getInstance().printExceptionObject(LogLevelFlag.LOG_ERROR, ex);
|
|
|
+ return createResParam(ResponseCode.STATUS_ERROR_DATA_TYPE.getCode() + "", ResponseCode.STATUS_ERROR_DATA_TYPE.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ //IO异常
|
|
|
+ @ExceptionHandler({IOException.class, ClientAbortException.class})
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> iOExceptionHandler() {
|
|
|
+ return createResParam(ResponseCode.STATUS_IO_EXCEPTION.getCode() + "", ResponseCode.STATUS_IO_EXCEPTION.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ //未知方法异常
|
|
|
+ @ExceptionHandler(NoSuchMethodException.class)
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> noSuchMethodExceptionHandler(NoSuchMethodException ex) {
|
|
|
+ return createResParam(ResponseCode.STATUS_NO_SUCH_METHOD.getCode() + "", ResponseCode.STATUS_NO_SUCH_METHOD.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ //数组越界异常
|
|
|
+ @ExceptionHandler(IndexOutOfBoundsException.class)
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
|
|
|
+ LogPrintMgr.getInstance().printExceptionObject(LogLevelFlag.LOG_ERROR, ex);
|
|
|
+ return createResParam(ResponseCode.STATUS_INDEX_OUT_OF_BOUNDS.getCode() + "", ResponseCode.STATUS_INDEX_OUT_OF_BOUNDS.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ //400错误
|
|
|
+ @ExceptionHandler({HttpMessageNotReadableException.class})
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> requestNotReadable(HttpMessageNotReadableException ex) {
|
|
|
+ return createResParam(ResponseCode.STATUS_BAD_REQUEST.getCode() + "", "Request not readable, please check [URI or Param].");
|
|
|
+ }
|
|
|
+
|
|
|
+ //400错误
|
|
|
+ @ExceptionHandler({TypeMismatchException.class})
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> requestTypeMismatch(TypeMismatchException ex) {
|
|
|
+ return createResParam(ResponseCode.STATUS_BAD_REQUEST.getCode() + "", "Type mismatch exception, please check [URI or Param].");
|
|
|
+ }
|
|
|
+
|
|
|
+ //400错误
|
|
|
+ @ExceptionHandler({MissingServletRequestParameterException.class})
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> requestMissingServletRequest(MissingServletRequestParameterException ex) {
|
|
|
+ return createResParam(ResponseCode.STATUS_BAD_REQUEST.getCode() + "", "Missing servlet request, please check [URI or Param].");
|
|
|
+ }
|
|
|
+
|
|
|
+ //405错误
|
|
|
+ @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> request405() {
|
|
|
+ return createResParam(ResponseCode.STATUS_METHOD_NOT_ALLOWED.getCode() + ""
|
|
|
+ , ResponseCode.STATUS_METHOD_NOT_ALLOWED.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ //406错误
|
|
|
+ @ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> request406() {
|
|
|
+ return createResParam(ResponseCode.STATUS_HTTP_TYPE_NOT_ACCEPTABLE.getCode() + ""
|
|
|
+ , ResponseCode.STATUS_HTTP_TYPE_NOT_ACCEPTABLE.getMessage() + "Please check [URI or Param].");
|
|
|
+ }
|
|
|
+
|
|
|
+ //500错误
|
|
|
+ @ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
|
|
|
+ @ResponseBody
|
|
|
+ public ResultResponse<String> server500(RuntimeException runtimeException) {
|
|
|
+ return createResParam(ResponseCode.STATUS_HTTP_TYPE_NOT_ACCEPTABLE.getCode() + ""
|
|
|
+ , ResponseCode.STATUS_HTTP_TYPE_NOT_ACCEPTABLE.getMessage() + "Please check [URI or Param].");
|
|
|
+ }
|
|
|
+}
|