Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不太建议接口直接throw IOException,用起来很不方便。 #178

Open
Shiyajian opened this issue Apr 16, 2024 · 1 comment
Open

Comments

@Shiyajian
Copy link

我这边通过一个模板方式,对IM的所有接口进行了包装,如果方法显性的扔出IOException,代码不太优雅。
我目前是这样写的

   TxClientTemplate.call(room.getConfigId(),
                (imClient) -> imClient.group.sendGroupMsg(request));

然后 TxClientTemplate.call 的代码是这样的

 public static <T extends GenericResult> T call(Long configId, Function<ImClient, T> function) {
        ImClient imClient = CLIENTS.get(configId);
        if (imClient == null) {
            throw new RuntimeException("缺少" + configId + "的imClient的配置");
        }

        try {
            T result = function.apply(imClient);
            log.info("请求腾讯云直播接口,响应:{}", JsonUtil.toJsonString(result));
            if (!"OK".equals(result.getActionStatus()) || 0 != result.getErrorCode()) {
                log.error("请求腾讯云失败,状态非OK");
                throw BizException.create("103", result.getErrorInfo());
            }
            return result;
        } catch (Throwable throwable) {
            log.error("调用腾讯云发生异常,错误信息:{}", ExceptionUtil.getMessage(throwable), throwable);
            throw BizException.create("103", "调用腾讯云接口失败");
        }
    }

如果显性的有IOException,那我的代码就需要每个都需要 try - catch 了就变成了,感觉很不优雅。

  TxClientTemplate.call(room.getConfigId(),
                (imClient) -> {
                    try {
                        return imClient.group.sendGroupMsg(request);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
@Antenbabby
Copy link
Contributor

这个问题不大 打个@SneakyThrows就行了.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants