Skip to content

参考:异常捕获与转换

捕获异常后降级处理

java
public OfficeApiDTO<String, Object> syncData(DataSyncDTO dto) {
    try {
        return dataService.sync(dto);
    } catch (BusinessException e) {
        log.error("数据同步失败", e);
        return OfficeApiDTO.<String>builder()
            .resultStatus("F")
            .resultCode(e.beBindExceptionCodeEnum().code())
            .resultMsg(CollectionUtils.isEmpty(e.getArgs())
                ? e.beBindExceptionCodeEnum().defaultMsg()
                : e.getArgs().toString())
            .build();
    }
}

异常类型转换

java
// Controller 层将业务异常转换为提示异常
@GetMapping("/public/info")
public BaseResponse<String, Object> getPublicInfo() {
    try {
        return BaseResponse.success(service.getInfo());
    } catch (BusinessException e) {
        // 转换为 Hint 异常,避免在公开接口记录过多错误日志
        throw BusinessHintException.error(e.beBindExceptionCodeEnum(), e.getMessage());
    }
}

Dubbo/Feign 调用中的异常传递

跨服务调用时,消费者端捕获到的异常可能是 RuntimeException 的包装形式。建议直接抛出,由消费端的全局处理器统一处理:

java
// Provider 端:直接抛出业务异常
public User getUser(String userId) {
    if (StringUtils.isBlank(userId)) {
        throw BusinessException.error(XxxOfBusinessExceptionCodeEnum.ERR_INVALID_PARAMS);
    }
    // ...
}
java
// Consumer 端:不需要额外 catch,让全局处理器接管
try {
    return userService.getUser(userId);
} catch (Exception e) {
    // 如需降级处理,可在此捕获;否则直接抛出即可
    throw new BusinessException(XxxOfBusinessExceptionCodeEnum.ERR_RPC_FAIL, e.getMessage(), e);
}

捕获后包装为外部接口格式

java
try {
    return service.save(data);
} catch (BusinessException e) {
    return ExternalApiDTO.builder()
        .resultCode(e.beBindExceptionCodeEnum().code())
        .resultMsg(e.getMessage())
        .build();
}

Power By 数字海南