JavaScript - 检测类型 net::ERR_INCOMPLETE_CHUNKED_ENCODING

问题描述

我正在使用简单的 XHR 调用服务器来下载文件。 下面的代码一个进度,以及针对不稳定连接情况的“保护”机制。 此保护将检测任何 XHR 错误并根据错误决定是否重试查询或放弃弹出友好错误消息:

function Messaging(p){
    var o=p.progressBar,h=new XMLHttpRequest(),protectionTimer;
    h.open(m,p.path); h.responseType='arraybuffer';
    if(p.hdr) for(i in p.hdr) h.setRequestHeader(i,p.hdr[i]);
    h.onload=function() {
        if(protectionTimer) clearTimeout(protectionTimer);
        if(h.status<200||h.status>399) return errorHandler();
        /* Handle the data here - everything is fine */
    };
    h.upload.onprogress=h.onprogress=function(e) {
        if(protectionTimer) clearTimeout(protectionTimer);
        protectionTimer=setTimeout(retry,5000);
        o.max=e.total; o.val=e.loaded;
    };
    h.onerror=errorHandler; try { h.send(p.data); } catch(e) { errorHandler(e); }
    
    function errorHandler(e) {
        var x=h.response,a;

        // The below is for debugging
        if(x) {
            try { x=new TextDecoder().decode(x); }
            catch(z) { x="&lt;ERROR&gt;"; }
        }
        else x=p.path+" => [empty]";

        /*
            Here is the error detection and the issue:
            The condition say that errors ranging 100 to 199 are not fatal,so we can retry.
            However,thinking of h.status of 0 is a bit problematic:
                For CONNECTION_RESET error that happened to me when the connection was unstable,I want restart.
                But in my backend CGI I had an error which caused return here different error which I really don't care about "why" - I just care to STOP here as is fatal (and h.status in both cases is 0).
        */
        if((h.status>=100&&h.status<200)||!h.status) {
            console.log(h.status+" >> "+x);

            // Retrial
            h.open(m,p.p);
            if(protectionTimer) clearTimeout(protectionTimer);
            protectionTimer=setTimeout(retry,5000);
            return h.send(v);
        }
        o.xX(); if(p.err) p.err(h,x); else EF.alert({tit:h.status,txt:x,err:1}); 
    }
    function retry() { h.abort(); errorHandler(); } // Not sure if abort will call onerror event... 
};

根据我在代码中的评论,我不在乎为什么我会收到只能出现在 FF 或 Chrome 中的错误,我什至无法在任何地方找到错误名称“net::ERR_INCOMPLETE_CHUNKED_ENCODING” XHR 也不在错误事件中 - h.status 也响应 0(显然)。

如果 h.status 返回 0,我如何检测 CONNECTION_RESET 错误或任何其他错误

编辑: 我试过 console.log(h.getAllResponseHeaders()); -> 结果是[空]

非常感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)