diff --git a/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/exception/BaseGlobalExceptionHandle.java b/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/exception/BaseGlobalExceptionHandle.java index d773176d..80b0fa39 100644 --- a/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/exception/BaseGlobalExceptionHandle.java +++ b/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/exception/BaseGlobalExceptionHandle.java @@ -1,17 +1,18 @@ package com.nflg.product.base.core.exception; -import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; import nflg.product.common.constant.STATE; import nflg.product.common.vo.ResultVO; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DuplicateKeyException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; +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.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.servlet.NoHandlerFoundException; @@ -33,31 +34,35 @@ public class BaseGlobalExceptionHandle { */ @ExceptionHandler(value = NflgBusinessException.class) @ResponseBody - public ResultVO handleJeecgBootException(NflgBusinessException e) { - return ResultVO.error(e.getState(), e.getMessage()); - + public ResponseEntity> handleJeecgBootException(NflgBusinessException e) { + log.error(e.getMessage(), e); + //return ResultVO.error(e.getState(), e.getMessage()); + return errorResult(e.getState(),e.getMessage()); } @ExceptionHandler(value = NoHandlerFoundException.class) @ResponseBody - public ResultVO handlerNoFoundException(NoHandlerFoundException e) { + public ResponseEntity> handlerNoFoundException(NoHandlerFoundException e) { log.error(e.getMessage(), e); - return ResultVO.error(404, "路径不存在,请检查路径是否正确"); + //return ResultVO.error(404, "路径不存在,请检查路径是否正确"); + return errorResult(STATE.AddressNotFound,"路径不存在,请检查路径是否正确"); } @ExceptionHandler(value = DuplicateKeyException.class) @ResponseBody - public ResultVO handleDuplicateKeyException(DuplicateKeyException e) { + public ResponseEntity> handleDuplicateKeyException(DuplicateKeyException e) { log.error(e.getMessage(), e); - return ResultVO.error("已存在该记录,请勿重复操作"); + //return ResultVO.error("已存在该记录,请勿重复操作"); + return errorResult("已存在该记录,请勿重复操作"); } @ExceptionHandler(value = Exception.class) @ResponseBody - public ResultVO handleException(Exception e) { + public ResponseEntity> handleException(Exception e) { log.error(e.getMessage(), e); - return ResultVO.error("操作失败,请联系系统管理员"); + //return ResultVO.error("操作失败,请联系系统管理员"); + return errorResult("操作失败,请联系系统管理员"); } /** @@ -67,8 +72,9 @@ public class BaseGlobalExceptionHandle { */ @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) @ResponseBody - public ResultVO HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { - StringBuffer sb = new StringBuffer(); + public ResponseEntity> HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { + log.error(e.getMessage(), e); + StringBuilder sb = new StringBuilder(); sb.append("不支持"); sb.append(e.getMethod()); sb.append("请求方法,"); @@ -81,8 +87,8 @@ public class BaseGlobalExceptionHandle { } } log.error(sb.toString(), e); - return ResultVO.error(STATE.PermissionDenied, "登录已过期,请重新登录"); -// return ResultVO.error(STATE.PermissionDenied, sb.toString()); + //return ResultVO.error(STATE.PermissionDenied, "登录已过期,请重新登录"); + return errorResult(STATE.PermissionDenied,"不支持的请求方法:"+e.getMethod()); } /** @@ -90,53 +96,69 @@ public class BaseGlobalExceptionHandle { */ @ExceptionHandler(value = MaxUploadSizeExceededException.class) @ResponseBody - public ResultVO handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) { + public ResponseEntity> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) { log.error(e.getMessage(), e); - return ResultVO.error("文件大小超出10MB限制, 请压缩或降低文件质量! "); + //return ResultVO.error("文件大小超出10MB限制, 请压缩或降低文件质量! "); + return errorResult("文件大小超出10MB限制, 请压缩或降低文件质量! "); } @ExceptionHandler(value = DataIntegrityViolationException.class) @ResponseBody - public ResultVO handleDataIntegrityViolationException(DataIntegrityViolationException e) { + public ResponseEntity> handleDataIntegrityViolationException(DataIntegrityViolationException e) { log.error(e.getMessage(), e); - return ResultVO.error("字段太长,超出数据库字段的长度"); +// return ResultVO.error("字段太长,超出数据库字段的长度"); + return errorResult("字段太长,超出数据库字段的长度"); } @ExceptionHandler(value = SQLException.class) @ResponseBody - public ResultVO handleSQLException(SQLException e) { + public ResponseEntity> handleSQLException(SQLException e) { log.error(e.getMessage(),e); - return ResultVO.error("sql执行过程出现错误"); + //return ResultVO.error("sql执行过程出现错误"); + return errorResult("sql执行过程出现错误"); } @ExceptionHandler(value = RuntimeException.class) @ResponseBody - public ResultVO handleRuntimeExceptionException(RuntimeException e) { + public ResponseEntity> handleRuntimeExceptionException(RuntimeException e) { log.error(e.getMessage(),e); - throw new NflgBusinessException(STATE.Error, "系统错误,请联系管理员"); + //throw new NflgBusinessException(STATE.Error, "系统错误,请联系管理员"); + return errorResult("系统错误,请联系管理员"); } @ExceptionHandler(value = BadSqlGrammarException.class) @ResponseBody - public ResultVO handleDataAccessException(BadSqlGrammarException e) { + public ResponseEntity> handleDataAccessException(BadSqlGrammarException e) { log.error(e.getMessage(),e); - throw new NflgBusinessException(STATE.Error, "sql 语法错误,系统错误,请联系管理员"); + //throw new NflgBusinessException(STATE.Error, "sql 语法错误,系统错误,请联系管理员"); + return errorResult("sql 语法错误,系统错误,请联系管理员"); } @ExceptionHandler(value = MethodArgumentNotValidException.class) @ResponseBody - public ResultVO handleMethodArgumentException(MethodArgumentNotValidException e){ + public ResponseEntity> handleMethodArgumentException(MethodArgumentNotValidException e){ log.error(e.getMessage(),e); - return ResultVO.error(e.getBindingResult().getFieldError().getDefaultMessage()) ; + return errorResult(e.getBindingResult().getFieldError().getDefaultMessage()) ; } @ExceptionHandler(NullPointerException.class) - public ResultVO handleNullPointerException(NullPointerException ex) { - - return ResultVO.error(STATE.Error, "系统错误,请联系管理员:"+ex.getMessage()) ; + public ResponseEntity> handleNullPointerException(NullPointerException ex) { + log.error(ex.getMessage(),ex); + return errorResult(STATE.Error, "系统错误,请联系管理员:"+ex.getMessage()) ; } + @ExceptionHandler(MissingServletRequestParameterException.class) + public ResponseEntity> handleMissingServletRequestParameterException(MissingServletRequestParameterException ex) { + log.error(ex.getMessage(),ex); + return errorResult(STATE.ParamErr, ex.getParameterName()+"参数不能为空"); + } + private static ResponseEntity> errorResult(String msg) { + return ResponseEntity.status(HttpStatus.OK).body(ResultVO.error(STATE.Error,msg)); + } + private static ResponseEntity> errorResult(STATE code,String msg) { + return ResponseEntity.status(HttpStatus.OK).body(ResultVO.error(code,msg)); + } }