2013-11-25 7 views
20

मैं NodeJS पर साधारण प्रॉक्सी लिखा था और यह लग रहा हैत्रुटि से निपटने

तरह
var request = require('request'); 
app.all('/proxy/*', function(req, res){ 
    req.pipe(request({ 
     url: config.backendUrl + req.params[0], 
     qs: req.query, 
     method: req.method 
    })).pipe(res); 
}); 

यह ठीक काम करता है, तो दूरस्थ होस्ट उपलब्ध है, लेकिन यदि दूरस्थ होस्ट बिना क्रिया का अपवाद के साथ उपलब्ध नहीं पूरे नोड सर्वर क्रैश हो जाता है

stream.js:94            
     throw er; // Unhandled stream error in pipe.   
      ^            
Error: connect ECONNREFUSED         
    at errnoException (net.js:901:11)      
    at Object.afterConnect [as oncomplete] (net.js:892:19) 

मैं ऐसी त्रुटियों को कैसे संभाल सकता हूं?

+0

[यहां बेहतर जवाब] (http://stackoverflow.com/questions/7222982/node-request-how-to-determine-if-an-error-occurred-during-the-request) – laggingreflex

उत्तर

28

डॉक्स (https://github.com/mikeal/request) आप निम्नलिखित लाइनों के साथ कुछ करने के लिए सक्षम होना चाहिए को देखते हुए:

आप अनुरोध पर वैकल्पिक कॉलबैक तर्क, उदाहरण के लिए उपयोग कर सकते हैं: वैकल्पिक रूप से

app.all('/proxy/*', function(req, res){ 
    req.pipe(request({ 
     url: config.backendUrl + req.params[0], 
     qs: req.query, 
     method: req.method 
    }, function(error, response, body){ 
    if (error.code === 'ECONNREFUSED'){ 
     console.error('Refused connection'); 
    } else { 
     throw error; 
    } 
    })).pipe(res); 
}); 

, आप की तरह कुछ के साथ, न आया हुआ अपवाद पकड़ कर सकते हैं निम्नलिखित:

process.on('uncaughtException', function(err){ 
    console.error('uncaughtException: ' + err.message); 
    console.error(err.stack); 
    process.exit(1);    // exit with error 
}); 
+0

धन्यवाद, यह मेरे लिये कार्य करता है। – ioncreature

2

आप ECONNREFUSED मेकअप के लिए न आया हुआ अपवाद को पकड़ने के हैं अपनी प्रक्रिया को पुनरारंभ करना सुनिश्चित करें। मैंने परीक्षण में देखा कि यदि आप अपवाद को अनदेखा करते हैं और फिर से कनेक्ट करने का प्रयास करते हैं तो सॉकेट अस्थिर हो जाती है।

process.on('uncaughtException', function(err){ 
//Is this our connection refused exception? 
    if(err.message.indexOf("ECONNREFUSED") > -1) 
    { 
    //Safer to shut down instead of ignoring 
    //See: http://shapeshed.com/uncaught-exceptions-in-node/ 
    console.error("Waiting for CLI connection to come up. Restarting in 2 second..."); 
    setTimeout(shutdownProcess, 2000); 
    } 
    else 
    { 
    //This is some other exception.. 
    console.error('uncaughtException: ' + err.message); 
    console.error(err.stack); 
    shutdownProcess(); 
    } 
}); 

//Desc: Restarts the process. Since forever is managing this process it's safe to shut down 
//  it will be restarted. If we ignore exceptions it could lead to unstable behavior. 
//  Exit and let the forever utility restart everything 
function shutdownProcess() 
{ 
    process.exit(1); //exit with error 
} 
2

आप वास्तव में ECONNREFUSED अपवाद को रोकने के लिए प्रयास करना चाहिए: http://shapeshed.com/uncaught-exceptions-in-node/

मैं "हमेशा के लिए" उपकरण का उपयोग कर मेरी नोड प्रक्रिया पुनः आरंभ करने के लिए, निम्न कोड के साथ समाप्त हो गया:

यहाँ एक महान अवलोकन दिया गया है बेकार होने से:

var request = require('request'); 
app.all('/proxy/*', function(req, res){ 
    req.pipe(request({ 
     url: config.backendUrl + req.params[0], 
     qs: req.query, 
     method: req.method 
    })) 
    .on('error', err => { 
     const msg = 'Error on connecting to the webservice.'; 
     console.error(msg, err); 
     res.status(500).send(msg); 
    }) 
    .pipe(res); 
}); 

यदि आपको वास्तविक अनिश्चित अपवाद मिलता है, तो आपको एप्लिकेशन को मरने देना चाहिए।

संबंधित मुद्दे