com.baomidou.mybatisplus.extension.exceptions.ApiException Java Examples

The following examples show how to use com.baomidou.mybatisplus.extension.exceptions.ApiException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GlobalExceptionHandler.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 自定义 REST 业务异常
 * <p>
 *
 * @param e 异常类型
 * @return
 */
@ExceptionHandler(value = Exception.class)
public R<Object> handleBadRequest(Exception e) {
    /*
     * 业务逻辑异常
     */
    if (e instanceof ApiException) {
        IErrorCode errorCode = ((ApiException) e).getErrorCode();
        if (null != errorCode) {
            log.debug("Rest request error, {}", errorCode.toString());
            return R.failed(errorCode);
        }
        log.debug("Rest request error, {}", e.getMessage());
        return R.failed(e.getMessage());
    }

    /*
     * 参数校验异常
     */
    if (e instanceof BindException) {
        BindingResult bindingResult = ((BindException) e).getBindingResult();
        if (null != bindingResult && bindingResult.hasErrors()) {
            List<Object> jsonList = new ArrayList<>();
            bindingResult.getFieldErrors().stream().forEach(fieldError -> {
                Map<String, Object> jsonObject = new HashMap<>(2);
                jsonObject.put("name", fieldError.getField());
                jsonObject.put("msg", fieldError.getDefaultMessage());
                jsonList.add(jsonObject);
            });
            return R.restResult(jsonList, ApiErrorCode.FAILED);
        }
    }

    /**
     * 系统内部异常,打印异常栈
     */
    log.error("Error: handleBadRequest StackTrace : {}", e);
    return R.failed(ApiErrorCode.FAILED);
}
 
Example #2
Source File: GlobalExceptionHandler.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 自定义 REST 业务异常
 * <p>
 *
 * @param e 异常类型
 * @return
 */
@ExceptionHandler(value = Exception.class)
public R<Object> handleBadRequest(Exception e) {
    /*
     * 业务逻辑异常
     */
    if (e instanceof ApiException) {
        IErrorCode errorCode = ((ApiException) e).getErrorCode();
        if (null != errorCode) {
            logger.debug("Rest request error, {}", errorCode.toString());
            return R.failed(errorCode);
        }
        logger.debug("Rest request error, {}", e.getMessage());
        return R.failed(e.getMessage());
    }

    /*
     * 参数校验异常
     */
    if (e instanceof BindException) {
        BindingResult bindingResult = ((BindException) e).getBindingResult();
        if (null != bindingResult && bindingResult.hasErrors()) {
            List<Object> jsonList = new ArrayList<>();
            bindingResult.getFieldErrors().stream().forEach(fieldError -> {
                Map<String, Object> jsonObject = new HashMap<>(2);
                jsonObject.put("name", fieldError.getField());
                jsonObject.put("msg", fieldError.getDefaultMessage());
                jsonList.add(jsonObject);
            });
            return R.restResult(jsonList, ApiErrorCode.FAILED);
        }
    }

    /**
     * 系统内部异常,打印异常栈
     */
    logger.error("Error: handleBadRequest StackTrace : {}", e);
    return R.failed(ApiErrorCode.FAILED);
}
 
Example #3
Source File: UserServiceImpl.java    From SpringBlade with Apache License 2.0 5 votes vote down vote up
@Override
public boolean submit(User user) {
	if (Func.isNotEmpty(user.getPassword())) {
		user.setPassword(DigestUtil.encrypt(user.getPassword()));
	}
	Integer cnt = baseMapper.selectCount(Wrappers.<User>query().lambda().eq(User::getTenantId, user.getTenantId()).eq(User::getAccount, user.getAccount()));
	if (cnt > 0) {
		throw new ApiException("当前用户已存在!");
	}
	return saveOrUpdate(user);
}
 
Example #4
Source File: DictServiceImpl.java    From SpringBlade with Apache License 2.0 5 votes vote down vote up
@Override
@CacheEvict(cacheNames = {DICT_LIST, DICT_VALUE}, allEntries = true)
public boolean submit(Dict dict) {
	LambdaQueryWrapper<Dict> lqw = Wrappers.<Dict>query().lambda().eq(Dict::getCode, dict.getCode()).eq(Dict::getDictKey, dict.getDictKey());
	Integer cnt = baseMapper.selectCount((Func.isEmpty(dict.getId())) ? lqw : lqw.notIn(Dict::getId, dict.getId()));
	if (cnt > 0) {
		throw new ApiException("当前字典键值已存在!");
	}
	return saveOrUpdate(dict);
}
 
Example #5
Source File: Assert.java    From supplierShop with MIT License 4 votes vote down vote up
public static void fail(String message) {
    throw new ApiException(message);
}
 
Example #6
Source File: Assert.java    From supplierShop with MIT License 2 votes vote down vote up
/**
 * <p>
 * 失败结果
 * </p>
 *
 * @param errorCode 异常错误码
 */
public static void fail(IErrorCode errorCode) {
    throw new ApiException(errorCode);
}