0

An error is being passed by the callback in my request function. I am trying to determine under what conditions an error is passed to the callback.

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if(error){
   //why or when would an error be created?
  }
  else if (response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage. 
  }
  else{
   // when would this happen ?
   }
})

the documentation doesn't seem to cover what conditions will cause an error object to be created and passed. Right now I just assume anything but a 200 or 300 will cause an error to be created, but I am just guessing.

1 Answers1

0

request library uses the node.js http module internally for making GET request. From it's doc:

If any error is encountered during the request (be that with DNS resolution, TCP level errors, or actual HTTP parse errors) an 'error' event is emitted on the returned request object.

I guess you have to go though the http module source to exactly find out what are the errors.

hassansin
  • 16,918
  • 3
  • 43
  • 49