search

Home  >  Q&A  >  body text

Enhance error message output in Node.js to close the listener

I have the following code that handles http requests.

request.connection.addListener('close', function(had_error) {
   console.log(had_error);
   ...

I want to print more error information, but it only prints "true".

P粉517475670P粉517475670224 days ago442

reply all(1)I'll reply

  • P粉662614213

    P粉6626142132024-04-04 08:08:06

    Use Error events to track errors. It returns an Error object containing complete details.

    request.connection.addListener('error', function(error) {
        console.log(error.reason|| error.message);
    });

    Since the connection has been deprecated since v15, you can also use it if you are using the latest version

    request.on('error',function(error){
        console.log(error.reason|| error.message);
    });

    reply
    0
  • Cancelreply