|
@@ -0,0 +1,128 @@
|
|
|
|
|
+package com.chuanghai.ihotel.common.exception;
|
|
|
|
|
+
|
|
|
|
|
+import com.chuanghai.ihotel.common.utils.CommonResult;
|
|
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
|
|
+import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
|
|
|
+import org.springframework.web.HttpMediaTypeNotSupportedException;
|
|
|
|
|
+import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
|
+import org.springframework.web.bind.MissingRequestHeaderException;
|
|
|
|
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
+import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartException;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @Author: codingliang
|
|
|
|
|
+ * @Description: 统一异常处理
|
|
|
|
|
+ * @Date: 2021-01-06 10:07
|
|
|
|
|
+ * @Version: V1.0
|
|
|
|
|
+ **/
|
|
|
|
|
+@RestControllerAdvice
|
|
|
|
|
+public class MyExceptionHandler {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 处理参数校验异常
|
|
|
|
|
+ * @param e
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
|
|
+ public CommonResult handleValidException(MethodArgumentNotValidException e) {
|
|
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
|
|
+ e.getBindingResult().getFieldErrors().forEach(item -> {
|
|
|
|
|
+ String errMessage = item.getDefaultMessage();
|
|
|
|
|
+ String errField = item.getField();
|
|
|
|
|
+ map.put(errField, errMessage);
|
|
|
|
|
+ });
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(BizCodeEnume.INVALID_PARAM.getCode()), BizCodeEnume.INVALID_PARAM.getMsg()).setResult(map);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 数据库索引重复
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(DuplicateKeyException.class)
|
|
|
|
|
+ public CommonResult handleSQLIntegrityConstraintViolationException() {
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(BizCodeEnume.DATA_IS_EXIST.getCode()), BizCodeEnume.DATA_IS_EXIST.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 请求方法不支持
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
|
|
+ public CommonResult handleRequestMethodNotSupported() {
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(BizCodeEnume.METHOD_NOT_SUPPORT.getCode()), BizCodeEnume.METHOD_NOT_SUPPORT.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 缺少请求头
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(MissingRequestHeaderException.class)
|
|
|
|
|
+ public CommonResult handleRequestHeaderMissing() {
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(BizCodeEnume.REQUEST_HEADER_MISSING.getCode()), BizCodeEnume.REQUEST_HEADER_MISSING.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 参数缺失
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(MissingServletRequestParameterException.class)
|
|
|
|
|
+ public CommonResult handleMissingServletRequestParameterException() {
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(BizCodeEnume.PARAMETER_ERROR.getCode()), BizCodeEnume.PARAMETER_ERROR.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 文件过大
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(MaxUploadSizeExceededException.class)
|
|
|
|
|
+ public CommonResult handleMaxUploadSizeExceededException() {
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(BizCodeEnume.FILE_IS_TOO_BIG.getCode()), BizCodeEnume.FILE_IS_TOO_BIG.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ExceptionHandler(MultipartException.class)
|
|
|
|
|
+ public CommonResult handleMultipartException() {
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(BizCodeEnume.PARAMETER_ERROR.getCode()), "文件上传异常");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * body为空
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(HttpMessageNotReadableException.class)
|
|
|
|
|
+ public CommonResult handleMessageNotReadableException() {
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(BizCodeEnume.BODY_IS_EMPTY.getCode()), BizCodeEnume.BODY_IS_EMPTY.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
|
|
|
|
|
+ public CommonResult handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(BizCodeEnume.PARAMETER_ERROR.getCode()), "参数类型错误:" + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 自定义异常
|
|
|
|
|
+ * @param e
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(RRException.class)
|
|
|
|
|
+ public CommonResult handleRRException(RRException e) {
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(e.getCode()), e.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 未知异常
|
|
|
|
|
+ * @param throwable
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(Throwable.class)
|
|
|
|
|
+ public CommonResult handleException(Throwable throwable) {
|
|
|
|
|
+ throwable.printStackTrace();
|
|
|
|
|
+ return CommonResult.fail(Integer.toString(BizCodeEnume.UNKNOW_EXCEPTION.getCode()), BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|