错误:套接字在使用请求承诺的Node JS中发出的Http请求中挂起,导致循环重新启动

时间:2019-02-04 03:07:12

标签: node.js async-await request-promise

我正在尝试使用for循环内的request-promise发出Http Request。但是,如果Http请求花费的时间很长,则请求承诺会关闭连接。

这种行为是可以的,但我无法理解的是在打印错误后,for循环再次从0开始。

下面是代码

const rp = require('request-promise');
async function stepIterator(processingSteps, documentId) {
    var finalResult = null;
    for (var step = 0, len = processingSteps.length; step < len; step++) {
        if (step === 0 || step === 1 || step == 2 || step == 3) {
            try {
                console.log('Calling step ', step);
                let url = 'http://internal-server:8080/process';
                let collection = getCollection(documentId);
                let splitText = getSPlit(documentId);
                let outputFormat = 'xmi';
                let documentObject = await callServer(url, collection, splitText, outputFormat);
                finalResult = documentObject;
            } catch (error) {
                console.log("Error");
            }
        }
    }
    return finalResult;
}

async function callServer(url, collection, splitText, outputFormat) {
    var options = {
        method: 'POST',
        uri: url,
        headers: {
            'Content-Type': 'multipart/form-data',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive '
        },
        formData: {
            collection: collection,
            text: splitText,
            output: outputFormat
        }
    };
    return rp(options)
} 

完整的错误跟踪如下

  

{ RequestError: Error: socket hang up at new RequestError (D:\New_Projects\new-data-access-layer\node_modules\request-promise-core\lib\errors.js:14:15) at Request.plumbing.callback (D:\New_Projects\new-data-access-layer\node_modules\request-promise-core\lib\plumbing.js:87:29) at Request.RP$callback [as _callback] (D:\New_Projects\new-data-access-layer\node_modules\request-promise-core\lib\plumbing.js:46:31) at self.callback (D:\New_Projects\new-data-access-layer\node_modules\request\request.js:185:22) at Request.emit (events.js:182:13) at Request.onRequestError (D:\New_Projects\new-data-access-layer\node_modules\request\request.js:881:8) at ClientRequest.emit (events.js:182:13) at Socket.socketOnEnd (_http_client.js:425:9) at Socket.emit (events.js:187:15) at endReadableNT (_stream_readable.js:1094:12) at process._tickCallback (internal/process/next_tick.js:63:19) name: 'RequestError', message: 'Error: socket hang up', cause:
{ Error: socket hang up at createHangUpError (_http_client.js:322:15) at Socket.socketOnEnd (_http_client.js:425:23) at Socket.emit (events.js:187:15) at endReadableNT (_stream_readable.js:1094:12) at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }, error: { Error: socket hang up at createHangUpError (_http_client.js:322:15) at Socket.socketOnEnd (_http_client.js:425:23) at Socket.emit (events.js:187:15) at endReadableNT (_stream_readable.js:1094:12) at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }, options: { method: 'POST', uri: 'http://internal-server:8080/process', json: true, headers: { Connection: 'keep-alive ' }, body: { docSplitId: [Array], _id: 5c579d84812acb17ec74ac39, contentType: 'application/pdf', location: 'C:\\Users\\newuser\\AppData\\Local\\Temp\\2\\report.pdf', docModelVersion: '1', visualMetaDataId: null, categoryId: '5c52a72f6df294140c0535bc', deductedInfo: null, status: 'New', isDeleted: false, metadata: [Object], detailedStatus: [Array] }, callback: [Function: RP$callback], transform: undefined, simple: true, resolveWithFullResponse: false, transform2xxOnly: false }, response: undefined }

0 个答案:

没有答案