Skip to content

部分请求方法会造成无限递归 #1591

Closed
@shuxuefu

Description

@shuxuefu

简要描述

在开发过程中发现 BaseWxMpServiceImpl.execute()调用 executeInternal方法 异常内 如果出现指定code会刷新token后调用execute()
而execute()方法终止条件为 为方法变量 int retryTimes = 0; 那么会如果一直出现指定code会造成无限递归

模块版本情况

  • WxJava 模块名: weixin-java-mp
  • WxJava 版本号:3.7.0

详细描述

public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
    int retryTimes = 0;

    while(true) {
        try {
            return this.executeInternal(executor, uri, data);
        } catch (WxErrorException var10) {
            if (retryTimes + 1 > this.maxRetryTimes) {
                log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
                throw new RuntimeException("微信服务端异常,超出重试次数");
            }

            WxError error = var10.getError();
            if (error.getErrorCode() != -1) {
                throw var10;
            }

            int sleepMillis = this.retrySleepMillis * (1 << retryTimes);

            try {
                log.warn("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
                Thread.sleep((long)sleepMillis);
            } catch (InterruptedException var9) {
                throw new RuntimeException(var9);
            }

            if (retryTimes++ >= this.maxRetryTimes) {
                log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
                throw new RuntimeException("微信服务端异常,超出重试次数");
            }
        }
    }
}

protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
    E dataForLog = DataUtils.handleDataWithSecret(data);
    if (uri.contains("access_token=")) {
        throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
    } else {
        String accessToken = this.getAccessToken(false);
        String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken;

        try {
            T result = executor.execute(uriWithAccessToken, data, WxType.MP);
            log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", new Object[]{uriWithAccessToken, dataForLog, result});
            return result;
        } catch (WxErrorException var9) {
            WxError error = var9.getError();
            if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) {
                this.getWxMpConfigStorage().expireAccessToken();
                if (this.getWxMpConfigStorage().autoRefreshToken()) {
                    return this.execute(executor, uri, data);
                }
            }

            if (error.getErrorCode() != 0) {
                log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", new Object[]{uriWithAccessToken, dataForLog, error});
                throw new WxErrorException(error, var9);
            } else {
                return null;
            }
        } catch (IOException var10) {
            log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", new Object[]{uriWithAccessToken, dataForLog, var10.getMessage()});
            throw new WxErrorException(WxError.builder().errorMsg(var10.getMessage()).build(), var10);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions