首页 > 解决方案 > 获取失败如何调查?

问题描述

我正在调查一个未能获取错误的问题。只有少数用户似乎得到了这一点,而且并非一直如此,但大多数用户来自同一家公司。

代码中有一个 4 次重试循环,但它只是失败了 4 次。

建议用户单击 CTRL+F5 并且它似乎将其整理出来,至少在一段时间内。

我们有一个星期没有任何问题,然后我们更新了系统,问题又开始了。

谷歌充满了无法获取的问题,但很难找到相关的问题,尤其是因为我们无法重现该问题。

我们在 Sentry 中收到错误报告,我们可以看到类似的事件

Fetch Save [200]  time: 14.41.03
ui.click
ui.click
Navigation from: url to url     time 14.41.34
exception: type-error: failed to fetch    time 14.41.40

大多数用户都在使用 chrome 的更新版本,但我们已经尝试过其他浏览器。

任何帮助,将不胜感激。我有源代码,可以进行更改。

谢谢

async function doFetch(input, init) {
    //console.log('doFetch', input);
    const response = await fetch(input, init);

    if (!response.ok && (response.status != 304)) {
        throw new Error(response.status+' - '+response.statusText);
    }

    const responseForError = response.clone();
    try {
        return await response.json();
    } catch (e) {
        e.extraInfo = {
            responseHead: String(await responseForError.text()).substr(0, 200),
        };

        throw e;
    }
}

export async function fetchJSON(input, init = {}) {
    const _init = {
        ...init,
        headers: {
            ...init.headers,
            Accept: 'application/json',
            //'Cache-Control': 'no-store',
        },
        method: init.method || 'GET',
        credentials: 'same-origin',
    };

    const response = await retryFetchLoop(input, _init);
    checkResponseForError(response);

    return response;
}

async function retryFetchLoop(URL, init) {
    let retryCounter = (((init.method == 'GET') || init.canRetry) ? 4 : 1);
    let retryDelay = 0;

    while (retryCounter > 0) {
        try {

            return await doFetch(URL, init);

        } catch (error) {
            if ((error.message == 'Failed to fetch')
                || String(error.message).match(/NetworkError when attempting to fetch resource/)
                || String(error.message).match(/Network request failed/)
                || String(error.message).match(/Network Error/)
                || ((error.response || {}).status == 502)
                || ((error.response || {}).status == 504)
            ) {
                if (retryCounter > 1) {
                    retryCounter--;
                    retryDelay += 500;

                    await new Promise(resolve => setTimeout(resolve, retryDelay));

                    continue;
                }

                const logErr = new ConnectionError('Failed to fetch');
                logErr.extraInfo = {
                    URL,
                    OriginalError: error.message,
                };
                errorLogger(logErr);

                throw error;
            }

            error.extraInfo = {
                ...(error.extraInfo || {}),
                URL,
            };
            errorLogger(error);

            throw error;
        }

    }
}

标签: node.jsreactjsamazon-web-services

解决方案


错误是由于 CORS。某些东西将 _sm_byp 添加到查询参数并导致重定向。该重定向触发 Access-Control-Allow-Origin 失败。

app.use(cors({
    origin: (ctx) => {
        return '*';
    },
     ...
}));

推荐阅读