请求-如何全局捕获错误

时间:2019-07-29 17:20:33

标签: javascript node.js error-handling request

我正在使用Node.JS和请求承诺。 我必须通过代理发送请求,并在发生错误时更改代理(如果它将停止工作)。我不想每次都在每个请求中都遇到错误。我要在全球范围内购买。怎么做?

async function getTest(){
   let res = rp('htt://someurl..')
        .then((body) => {
        })
        .catch((error) => {
            if (error.error.code === 'ECONNRESET') {
                console.log('gettext proxy error');
                return 'proxyerror';
            }
        });
   return await res;
}

我可以以某种方式绑定.on事件,以等待全局来自请求承诺的错误吗?

我不要这样……

async function getTest(){
    let res = rp('htt://someurl..')
        .then((body) => {
            rp('htt://someurl2..')
                .then((body) => {
                    rp('htt://someurl3..')
                        .then((body) => {
                        })
                        .catch((error) => {
                            if (error.error.code === 'ECONNRESET') {
                                console.log('gettext proxy error');
                                return 'proxyerror';
                            }
                        });
                })
                .catch((error) => {
                    if (error.error.code === 'ECONNRESET') {
                        console.log('gettext proxy error');
                        return 'proxyerror';
                    }
                });
        })
        .catch((error) => {
            if (error.error.code === 'ECONNRESET') {
                console.log('gettext proxy error');
                return 'proxyerror';
            }
        });
    return await res;
}

0 个答案:

没有答案
相关问题