|
@@ -1,69 +0,0 @@
|
|
|
-package com.imed.costaccount.common.exception;
|
|
|
-
|
|
|
-import com.imed.costaccount.common.constants.CommonConstant;
|
|
|
-import com.imed.costaccount.common.util.Result;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.dao.DataAccessException;
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
-import org.springframework.http.converter.HttpMessageConversionException;
|
|
|
-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.ResponseBody;
|
|
|
-import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
-
|
|
|
-/**
|
|
|
- * 自定义异常信息
|
|
|
- * TODO 建议加上@ResponseStatus
|
|
|
- */
|
|
|
-@ControllerAdvice
|
|
|
-@Slf4j
|
|
|
-public class ExceptionHandle {
|
|
|
-
|
|
|
- @ResponseBody
|
|
|
- @ExceptionHandler(value = MethodArgumentNotValidException.class)
|
|
|
- public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
|
|
- String msg = e.getBindingResult().getFieldError().getField() + CommonConstant.SEPARATOR_NUMBER + e.getBindingResult().getFieldError().getDefaultMessage();
|
|
|
- log.error("参数校验失败:[{}]", msg);
|
|
|
- return Result.errorMsg(500, msg);
|
|
|
- }
|
|
|
-
|
|
|
- @ResponseBody
|
|
|
- @ExceptionHandler(value = MissingServletRequestParameterException.class)
|
|
|
- public Result handleMethodArgumentNotValidException(MissingServletRequestParameterException e) {
|
|
|
- log.error("参数校验失败:[{}]", e.getParameterName());
|
|
|
- return Result.errorMsg(500, "参数错误" + CommonConstant.SEPARATOR_NUMBER + e.getParameterName());
|
|
|
- }
|
|
|
-
|
|
|
- @ResponseBody
|
|
|
- @ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
- @ExceptionHandler(value = HttpMessageConversionException.class)
|
|
|
- public Result handleMissingServletRequestParameter(HttpMessageConversionException e) {
|
|
|
- log.error("参数解析异常:{}", e.getMessage());
|
|
|
- return Result.errorMsg(500, "参数解析异常");
|
|
|
- }
|
|
|
-
|
|
|
- @ExceptionHandler(value = CostException.class)
|
|
|
- @ResponseBody
|
|
|
- public Result handleCostException(CostException e) {
|
|
|
- log.error("自定义异常:{}", e.getMessage(), e);
|
|
|
- return Result.errorMsg(e.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- @ExceptionHandler(DataAccessException.class)
|
|
|
- @ResponseBody
|
|
|
- public Result handleDataAccessException(DataAccessException e) {
|
|
|
- log.error("数据库错误:{}", e.getMessage(), e);
|
|
|
- return Result.errorMsg("数据库错误");
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @ExceptionHandler(Exception.class)
|
|
|
- @ResponseBody
|
|
|
- public Result handleException(Exception e) {
|
|
|
- log.error("系统异常:{}", e.getMessage(), e);
|
|
|
- return Result.errorMsg("系统异常");
|
|
|
- }
|
|
|
-
|
|
|
-}
|