vowsjs / vows

Asynchronous BDD & continuous testing for node.js
http://vowsjs.org
Apache License 2.0
1.56k stars 165 forks source link

When using http.request(), errors are not being caught, causing vows to crash #257

Closed lxe closed 6 years ago

lxe commented 11 years ago

Here's the code I'm running to test this:

'Request does something that throws an error' : {
  topic: function () { 
    var req = require('http').request({
      hostname : 'www.google.com',
      port     : 80,
      path     : '/',
      method   : 'GET'
    }, function(res) {
      throw new Error('error!') // Error should be caught
    })

    req.end()
  },

  'Must reach here': function () {
    assert.ok(true); // Reach here at the end of test
  },
},

I should see something like this:

Request does something that throws an error
    ✓ Must reach here

Instead this happens:

/Users/lxe/devel/vows_test/test_request:115
                throw new Error('error!') // Error should be caught
                      ^
Error: error!
    at ClientRequest.<anonymous> (/Users/lxe/devel/vows_test/test_request.js:115:23)
    at ClientRequest.g (events.js:192:14)
    at ClientRequest.EventEmitter.emit (events.js:96:17)
    at ClientRequest.vows.describe.options.Emitter.emit (/Users/lxe/devel/vows_test/node_modules/vows/lib/vows.js:237:24)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1462:7)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)
    at Socket.socketOnData [as ondata] (http.js:1367:20)
    at TCP.onread (net.js:404:27)

When I'm running a regular asynchronous function that throws an error, everything is OK:

'Regular async function does something that throws an error' : {
  topic: function () { 
    (function(callback) {
      callback()
    })(function() {
      throw new Error('error!') // Error should be caught
    })
  },

  'Must reach here': function () {
    assert.ok(true); // Reach here at the end of test
  },
},
  Regular async function does something that throws an error
    ✓ Must reach here

Originally I was using https://github.com/mikeal/request and discovered this, but I since narrowed it down to http.request() call.

I'm using vows 0.7.0.

lxe commented 6 years ago

Silly me 4 years ago. You can't catch an error in a deferred function from that context!

evanp commented 6 years ago

Ha! Glad we got that all worked out.