Search in sources :

Example 6 with BizException

use of com.actionworks.flashsale.app.exception.BizException in project flash-sale by ThoughtsBeta.

the class BadRequestExceptionHandler method handleConflict.

@ExceptionHandler(value = { BizException.class, FlowException.class, AuthException.class, DomainException.class })
protected ResponseEntity<Object> handleConflict(RuntimeException ex, WebRequest request) {
    ExceptionResponse exceptionResponse = new ExceptionResponse();
    if (ex instanceof UndeclaredThrowableException) {
        if (((UndeclaredThrowableException) ex).getUndeclaredThrowable() instanceof FlowException) {
            exceptionResponse.setErrorCode(LIMIT_BLOCK.getCode());
            exceptionResponse.setErrorMessage(LIMIT_BLOCK.getDesc());
        }
    } else if (ex instanceof BizException || ex instanceof DomainException) {
        exceptionResponse.setErrorCode(BIZ_ERROR.getCode());
        exceptionResponse.setErrorMessage(ex.getMessage());
    } else if (ex instanceof AuthException) {
        exceptionResponse.setErrorCode(AUTH_ERROR.getCode());
        exceptionResponse.setErrorMessage(AUTH_ERROR.getDesc());
    }
    logger.error("expectedException|预期错误|{},{}", ex.getMessage(), ex);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    return handleExceptionInternal(ex, JSON.toJSONString(exceptionResponse), httpHeaders, HttpStatus.BAD_REQUEST, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) BizException(com.actionworks.flashsale.app.exception.BizException) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 7 with BizException

use of com.actionworks.flashsale.app.exception.BizException in project flash-sale by ThoughtsBeta.

the class DefaultFlashItemAppService method onlineFlashItem.

@Override
public AppResult onlineFlashItem(Long userId, Long activityId, Long itemId) {
    logger.info("itemOnline|上线秒杀品|{},{},{}", userId, activityId, itemId);
    if (userId == null || activityId == null || itemId == null) {
        throw new BizException(INVALID_PARAMS);
    }
    AuthResult authResult = authorizationService.auth(userId, FLASH_ITEM_MODIFICATION);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    DistributedLock itemModificationLock = lockFactoryService.getDistributedLock(getItemModificationLockKey(userId));
    try {
        boolean isLockSuccess = itemModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(LOCK_FAILED_ERROR);
        }
        flashItemDomainService.onlineFlashItem(itemId);
        logger.info("itemOnline|秒杀品已上线");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("itemOnline|秒杀品已上线失败|{}", userId, e);
        throw new BizException("秒杀品已上线失败");
    } finally {
        itemModificationLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 8 with BizException

use of com.actionworks.flashsale.app.exception.BizException in project flash-sale by ThoughtsBeta.

the class DefaultFlashItemAppService method offlineFlashItem.

@Override
public AppResult offlineFlashItem(Long userId, Long activityId, Long itemId) {
    logger.info("itemOffline|下线秒杀品|{},{},{}", userId, activityId, itemId);
    AuthResult authResult = authorizationService.auth(userId, FLASH_ITEM_MODIFICATION);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    if (userId == null || activityId == null || itemId == null) {
        throw new BizException(INVALID_PARAMS);
    }
    DistributedLock itemModificationLock = lockFactoryService.getDistributedLock(getItemModificationLockKey(userId));
    try {
        boolean isLockSuccess = itemModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(LOCK_FAILED_ERROR);
        }
        flashItemDomainService.offlineFlashItem(itemId);
        logger.info("itemOffline|秒杀品已下线");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("itemOffline|秒杀品已下线失败|{}", userId, e);
        throw new BizException("秒杀品已下线失败");
    } finally {
        itemModificationLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 9 with BizException

use of com.actionworks.flashsale.app.exception.BizException in project flash-sale by ThoughtsBeta.

the class DefaultFlashOrderAppService method cancelOrder.

@Override
@Transactional
public AppResult cancelOrder(Long userId, Long orderId) {
    logger.info("cancelOrder|取消订单|{},{}", userId, orderId);
    if (userId == null || orderId == null) {
        throw new BizException(INVALID_PARAMS);
    }
    FlashOrder flashOrder = flashOrderDomainService.getOrder(userId, orderId);
    if (flashOrder == null) {
        throw new BizException(ORDER_NOT_FOUND);
    }
    boolean cancelSuccess = flashOrderDomainService.cancelOrder(userId, orderId);
    if (!cancelSuccess) {
        logger.info("cancelOrder|订单取消失败|{}", orderId);
        return AppResult.buildFailure(ORDER_CANCEL_FAILED);
    }
    StockDeduction stockDeduction = new StockDeduction().setItemId(flashOrder.getItemId()).setQuantity(flashOrder.getQuantity());
    boolean stockRecoverSuccess = stockDeductionDomainService.increaseItemStock(stockDeduction);
    if (!stockRecoverSuccess) {
        logger.info("cancelOrder|库存恢复失败|{}", orderId);
        throw new BizException(ORDER_CANCEL_FAILED);
    }
    boolean stockInRedisRecoverSuccess = itemStockCacheService.increaseItemStock(stockDeduction);
    if (!stockInRedisRecoverSuccess) {
        logger.info("cancelOrder|Redis库存恢复失败|{}", orderId);
        throw new BizException(ORDER_CANCEL_FAILED);
    }
    logger.info("cancelOrder|订单取消成功|{}", orderId);
    return AppResult.buildSuccess();
}
Also used : FlashOrder(com.actionworks.flashsale.domain.model.entity.FlashOrder) BizException(com.actionworks.flashsale.app.exception.BizException) StockDeduction(com.actionworks.flashsale.domain.model.StockDeduction) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with BizException

use of com.actionworks.flashsale.app.exception.BizException in project flash-sale by ThoughtsBeta.

the class NormalPlaceOrderService method doPlaceOrder.

@Override
public PlaceOrderResult doPlaceOrder(Long userId, FlashPlaceOrderCommand placeOrderCommand) {
    logger.info("placeOrder|开始下单|{},{}", userId, JSON.toJSONString(placeOrderCommand));
    if (userId == null || placeOrderCommand == null || !placeOrderCommand.validateParams()) {
        throw new BizException(INVALID_PARAMS);
    }
    boolean isActivityAllowPlaceOrder = flashActivityAppService.isAllowPlaceOrderOrNot(placeOrderCommand.getActivityId());
    if (!isActivityAllowPlaceOrder) {
        logger.info("placeOrder|秒杀活动下单规则校验未通过|{},{}", userId, placeOrderCommand.getActivityId());
        return PlaceOrderResult.failed(PLACE_ORDER_FAILED);
    }
    boolean isItemAllowPlaceOrder = flashItemAppService.isAllowPlaceOrderOrNot(placeOrderCommand.getItemId());
    if (!isItemAllowPlaceOrder) {
        logger.info("placeOrder|秒杀品下单规则校验未通过|{},{}", userId, placeOrderCommand.getActivityId());
        return PlaceOrderResult.failed(PLACE_ORDER_FAILED);
    }
    AppSimpleResult<FlashItemDTO> flashItemResult = flashItemAppService.getFlashItem(placeOrderCommand.getItemId());
    if (!flashItemResult.isSuccess() || flashItemResult.getData() == null) {
        return PlaceOrderResult.failed(ITEM_NOT_FOUND);
    }
    FlashItemDTO flashItem = flashItemResult.getData();
    Long orderId = orderNoGenerateService.generateOrderNo(new OrderNoGenerateContext());
    FlashOrder flashOrderToPlace = toDomain(placeOrderCommand);
    flashOrderToPlace.setItemTitle(flashItem.getItemTitle());
    flashOrderToPlace.setFlashPrice(flashItem.getFlashPrice());
    flashOrderToPlace.setUserId(userId);
    flashOrderToPlace.setId(orderId);
    StockDeduction stockDeduction = new StockDeduction().setItemId(placeOrderCommand.getItemId()).setQuantity(placeOrderCommand.getQuantity()).setUserId(userId);
    boolean preDecreaseStockSuccess = false;
    try {
        preDecreaseStockSuccess = itemStockCacheService.decreaseItemStock(stockDeduction);
        if (!preDecreaseStockSuccess) {
            logger.info("placeOrder|库存预扣减失败|{},{}", userId, JSON.toJSONString(placeOrderCommand));
            return PlaceOrderResult.failed(PLACE_ORDER_FAILED.getErrCode(), PLACE_ORDER_FAILED.getErrDesc());
        }
        boolean decreaseStockSuccess = stockDeductionDomainService.decreaseItemStock(stockDeduction);
        if (!decreaseStockSuccess) {
            logger.info("placeOrder|库存扣减失败|{},{}", userId, JSON.toJSONString(placeOrderCommand));
            return PlaceOrderResult.failed(PLACE_ORDER_FAILED.getErrCode(), PLACE_ORDER_FAILED.getErrDesc());
        }
        boolean placeOrderSuccess = flashOrderDomainService.placeOrder(userId, flashOrderToPlace);
        if (!placeOrderSuccess) {
            throw new BizException(PLACE_ORDER_FAILED.getErrDesc());
        }
    } catch (Exception e) {
        if (preDecreaseStockSuccess) {
            boolean recoverStockSuccess = itemStockCacheService.increaseItemStock(stockDeduction);
            if (!recoverStockSuccess) {
                logger.error("placeOrder|预扣库存恢复失败|{},{}", userId, JSON.toJSONString(placeOrderCommand), e);
            }
        }
        logger.error("placeOrder|下单失败|{},{}", userId, JSON.toJSONString(placeOrderCommand), e);
        throw new BizException(PLACE_ORDER_FAILED.getErrDesc());
    }
    logger.info("placeOrder|下单成功|{},{}", userId, orderId);
    return PlaceOrderResult.ok(orderId);
}
Also used : FlashOrder(com.actionworks.flashsale.domain.model.entity.FlashOrder) BizException(com.actionworks.flashsale.app.exception.BizException) OrderNoGenerateContext(com.actionworks.flashsale.app.util.OrderNoGenerateContext) FlashItemDTO(com.actionworks.flashsale.app.model.dto.FlashItemDTO) StockDeduction(com.actionworks.flashsale.domain.model.StockDeduction) BizException(com.actionworks.flashsale.app.exception.BizException)

Aggregations

BizException (com.actionworks.flashsale.app.exception.BizException)16 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)9 AuthResult (com.actionworks.flashsale.app.auth.model.AuthResult)7 AuthException (com.actionworks.flashsale.controller.exception.AuthException)7 StockDeduction (com.actionworks.flashsale.domain.model.StockDeduction)3 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)3 FlashOrder (com.actionworks.flashsale.domain.model.entity.FlashOrder)3 Transactional (org.springframework.transaction.annotation.Transactional)3 FlashItemDTO (com.actionworks.flashsale.app.model.dto.FlashItemDTO)2 OrderNoGenerateContext (com.actionworks.flashsale.app.util.OrderNoGenerateContext)2 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)2 AppException (com.actionworks.flashsale.app.exception.AppException)1 FlashActivityDTO (com.actionworks.flashsale.app.model.dto.FlashActivityDTO)1 OrderTaskHandleResult (com.actionworks.flashsale.app.model.result.OrderTaskHandleResult)1 PlaceOrderResult (com.actionworks.flashsale.app.model.result.PlaceOrderResult)1 FlashActivityCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)1 FlashItemCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache)1 QueuedPlaceOrderService (com.actionworks.flashsale.app.service.placeorder.queued.QueuedPlaceOrderService)1 DomainException (com.actionworks.flashsale.domain.exception.DomainException)1 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)1