title: Preventing Node TypeError crashing app
body: <p>Using the simple <a href="https://github.com/mikeal/request">request.js http client</a> I've noticed that sometimes a simple <code>TypeError</code> can crash the whole node app. Taking one of the examples:</p>
  
  <pre><code>request('http://www.google.com', function (error, response, body) {
      if (!error &amp;&amp; response.statusCode == 200) {
         console.log(body) // Print the google web page.
      }
  })
  </code></pre>
  
  <p>Now, take the hypothetical (in Google's case!) situation where Google fails to respond and the connection just hangs then times out. This code simply crashes the node app as <code>response</code> is undefined so <code>response.statusCode</code> cannot be read. This bubbles up to the event loop and triggers the crash with error: </p>
  
  <pre><code>TypeError: Cannot read property 'statusCode' of undefined
  </code></pre>
  
  <p>What's the simplest way I can prevent this from happening? I could add a check for the value of <code>error</code> before checking for the <code>statusCode</code> for example:</p>
  
  <pre><code>request('http://www.google.com', function (error, response, body) {
      if (!error) {
          if (response.statusCode == 200) {
            // do stuff
          }
      }
  })
  </code></pre>
  
  <p>But I'd rather not add unnecessary lines to the app if possible. I may be missing something obvious here! Any pointers greatly appreciated. Thanks.</p>