The use of Exception tools in actual projects

1. Throw an exception in the project at ordinary times

if (CollectionUtils.isEmpty(templateModelList)) {
            throw new RuntimeException("评测信息不能为空!");
        }

Disadvantages : after each judgment, an exception class must be new, an exception is thrown, the code is repeated, and the new object is repeated

2. Really throw an exception in the project

1. Custom exception tool class

This tool class can determine when the collection, array, object, etc. are empty

/**
 * 断言工具类
 */
public class BizExceptionAssert {
    private BizExceptionAssert(){}

    public static void isTrue(boolean expression, BizErrorCodeEnum errorCode) {
        if (!expression) {
            throw new BizException(errorCode);
        }
    }

    public static void isTrue(boolean expression, BizErrorCodeEnum errorCode, String message) {
        if (!expression) {
            throw new BizException(errorCode, message);
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new BizException(message);
        }
    }

    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new BizException(message);
        }
    }

    public static void notBlank(String text, String message) {
        if (StringUtils.isBlank(text)) {
            throw new BizException(message);
        }
    }


    public static void notEmpty(Object[] array, String message) {
        if (ArrayUtils.isEmpty(array)) {
            throw new BizException(message);
        }
    }

    public static void noNullElements(Object[] array, String message) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw new BizException(message);
                }
            }
        }
    }

    public static void notEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BizException(message);
        }
    }

    public static void notEmpty(Map<?, ?> map, String message) {
        if (MapUtils.isEmpty(map)) {
            throw new BizException(message);
        }
    }

}

2. Use in the program

If skuList is empty, throw an exception message ""

BizExceptionAssert.notEmpty(skuList,"商品sku详情信息为空");

3. Catch the BizException exception in the class that defines the global exception

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
   
    /**
     * 全局异常捕捉处理
     *
     * @param e
     * @param request
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseResult errorHandler(Exception e, HttpServletRequest request) {

        e.printStackTrace();
        return ResultUtils.failure("系统繁忙");
    }
    
    // 捕获runtimeException异常
    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseResult errorHandler(RuntimeException e, HttpServletRequest request) {
    	return ResultUtils.failure(e.getMessage());
    }
    
    // 处理BizException异常
    @ResponseBody
    @ExceptionHandler(value = BizException.class)
    public ResponseResult errorHandler(BizException e, HttpServletRequest request) {
    	return ResultUtils.failure(e.getCode(),e.getMessage());
    }

    // 处理MethodArgumentNotValidException 处理参数异常
    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResponseResult handleValidException(MethodArgumentNotValidException e) {
        log.error("数据校验出现问题{},异常类型{}", e.getMessage(), e.getClass());
        BindingResult bindingResult = e.getBindingResult();
        Map<String, String> errorMap = new HashMap<>();
        bindingResult.getFieldErrors().forEach(fieldError -> {
            errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
        });
        return ResultUtils.failure(errorMap.toString());
    }

}

 

Guess you like

Origin blog.csdn.net/qq_39564710/article/details/115301299