Search in sources :

Example 21 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class DefaultExceptionHandler method handleException.

/**
 * 统一异常处理类
 * @param request
 * @param e
 * @return
 */
@ExceptionHandler(Exception.class)
public Object handleException(HttpServletRequest request, Exception e) {
    HttpStatus status = getStatus(request);
    Map<String, Object> map;
    if (e instanceof BusinessException) {
        map = ((BusinessException) e).toMap();
    } else if (e.getCause() instanceof BusinessException) {
        map = ((BusinessException) e.getCause()).toMap();
    } else if (e instanceof InvalidUsageException) {
        map = ((InvalidUsageException) e).toMap();
    } else if (e.getCause() instanceof InvalidUsageException) {
        map = ((InvalidUsageException) e.getCause()).toMap();
    } else {
        map = new HashMap<>(8);
        map.put("code", status.value());
        String msg = buildMsg(status, e);
        map.put("msg", msg);
    }
    log.warn("请求处理异常", e);
    return new ResponseEntity<>(map, HttpStatus.OK);
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) HashMap(java.util.HashMap) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 22 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class DictionaryServiceExtImpl method createDictAndChildren.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean createDictAndChildren(DictionaryVO dictVO) {
    Dictionary dictionary = dictVO;
    dictionary.setIsDeletable(true).setIsEditable(true);
    if (!super.createEntity(dictionary)) {
        log.warn("新建数据字典定义失败,type=" + dictVO.getType());
        return false;
    }
    List<Dictionary> children = dictVO.getChildren();
    this.buildSortId(children);
    if (V.notEmpty(children)) {
        for (Dictionary dict : children) {
            dict.setParentId(dictionary.getId()).setType(dictionary.getType()).setIsDeletable(dictionary.getIsDeletable()).setIsEditable(dictionary.getIsEditable());
        }
        // 批量保存
        boolean success = super.createEntities(children);
        if (!success) {
            String errorMsg = "新建数据字典子项失败,type=" + dictVO.getType();
            log.warn(errorMsg);
            throw new BusinessException(Status.FAIL_OPERATION, errorMsg);
        }
    }
    return true;
}
Also used : Dictionary(com.diboot.core.entity.Dictionary) BusinessException(com.diboot.core.exception.BusinessException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 23 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class DictionaryServiceExtImpl method updateDictAndChildren.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateDictAndChildren(DictionaryVO dictVO) {
    Dictionary oldDictionary = super.getEntity(dictVO.getId());
    // 将DictionaryVO转化为Dictionary
    Dictionary dictionary = dictVO;
    dictionary.setIsDeletable(oldDictionary.getIsDeletable()).setIsEditable(oldDictionary.getIsEditable());
    if (!super.updateEntity(dictionary)) {
        log.warn("更新数据字典定义失败,type=" + dictVO.getType());
        return false;
    }
    // 获取原 子数据字典list
    QueryWrapper<Dictionary> queryWrapper = new QueryWrapper();
    queryWrapper.lambda().eq(Dictionary::getParentId, dictVO.getId());
    List<Dictionary> oldDictList = super.getEntityList(queryWrapper);
    List<Dictionary> newDictList = dictVO.getChildren();
    Set<Long> dictItemIds = new HashSet<>();
    this.buildSortId(newDictList);
    if (V.notEmpty(newDictList)) {
        for (Dictionary dict : newDictList) {
            dict.setType(dictVO.getType()).setParentId(dictVO.getId()).setIsDeletable(dictionary.getIsDeletable()).setIsEditable(dictionary.getIsEditable());
            if (V.notEmpty(dict.getId())) {
                dictItemIds.add(dict.getId());
                if (!super.updateEntity(dict)) {
                    log.warn("更新字典子项失败,itemName=" + dict.getItemName());
                    throw new BusinessException(Status.FAIL_EXCEPTION, "更新字典子项异常");
                }
            } else {
                if (!super.createEntity(dict)) {
                    log.warn("新建字典子项失败,itemName=" + dict.getItemName());
                    throw new BusinessException(Status.FAIL_EXCEPTION, "新建字典子项异常");
                }
            }
        }
    }
    if (V.notEmpty(oldDictList)) {
        for (Dictionary dict : oldDictList) {
            if (!dictItemIds.contains(dict.getId())) {
                if (!super.deleteEntity(dict.getId())) {
                    log.warn("删除子数据字典失败,itemName=" + dict.getItemName());
                    throw new BusinessException(Status.FAIL_EXCEPTION, "删除字典子项异常");
                }
            }
        }
    }
    return true;
}
Also used : Dictionary(com.diboot.core.entity.Dictionary) BusinessException(com.diboot.core.exception.BusinessException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class SSOAuthServiceImpl method applyToken.

@Override
public String applyToken(AuthCredential credential) {
    BaseJwtAuthToken authToken = initBaseJwtAuthToken(credential);
    try {
        Subject subject = SecurityUtils.getSubject();
        subject.login(authToken);
        if (subject.isAuthenticated()) {
            log.debug("申请token成功!authtoken={}", authToken.getCredentials());
            saveLoginTrace(authToken, true);
            // 跳转到首页
            return (String) authToken.getCredentials();
        } else {
            log.error("认证失败");
            saveLoginTrace(authToken, false);
            throw new BusinessException(Status.FAIL_OPERATION, "认证失败");
        }
    } catch (Exception e) {
        log.error("登录异常", e);
        saveLoginTrace(authToken, false);
        throw new BusinessException(Status.FAIL_OPERATION, e.getMessage());
    }
}
Also used : BaseJwtAuthToken(com.diboot.iam.jwt.BaseJwtAuthToken) BusinessException(com.diboot.core.exception.BusinessException) Subject(org.apache.shiro.subject.Subject) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) BusinessException(com.diboot.core.exception.BusinessException) AuthenticationException(org.apache.shiro.authc.AuthenticationException)

Example 25 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class EncryptCredential method getAuthCredential.

/**
 * 获取认证信息
 *
 * @return
 */
public <T extends AuthCredential> T getAuthCredential(IEncryptStrategy encryptor, Class<? extends AuthCredential> authCredentialCls) {
    try {
        String decryptContent = encryptor.decrypt(ciphertext);
        T result = (T) JSON.parseObject(decryptContent, authCredentialCls);
        String errMsg = V.validateBeanErrMsg(result);
        if (V.notEmpty(errMsg)) {
            throw new BusinessException(Status.FAIL_INVALID_PARAM, errMsg);
        }
        return result;
    } catch (Exception e) {
        log.error("获取认证信息失败!", e);
        throw new BusinessException(e.getMessage());
    }
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) BusinessException(com.diboot.core.exception.BusinessException)

Aggregations

BusinessException (com.diboot.core.exception.BusinessException)29 Transactional (org.springframework.transaction.annotation.Transactional)8 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)5 IamAccount (com.diboot.iam.entity.IamAccount)5 InvalidUsageException (com.diboot.core.exception.InvalidUsageException)4 IamMember (com.diboot.mobile.entity.IamMember)4 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)3 Dictionary (com.diboot.core.entity.Dictionary)3 UploadFile (com.diboot.file.entity.UploadFile)3 IamResourcePermission (com.diboot.iam.entity.IamResourcePermission)3 BaseJwtAuthToken (com.diboot.iam.jwt.BaseJwtAuthToken)3 IamResourcePermissionListVO (com.diboot.iam.vo.IamResourcePermissionListVO)3 ScheduleJob (com.diboot.scheduler.entity.ScheduleJob)3 HashMap (java.util.HashMap)3 WxOAuth2AccessToken (me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken)3 AuthenticationException (org.apache.shiro.authc.AuthenticationException)3 Subject (org.apache.shiro.subject.Subject)3 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)2 BaseService (com.diboot.core.service.BaseService)2 DictionaryService (com.diboot.core.service.DictionaryService)2