Skip to content

Commit

Permalink
Unified error and complete handling. Fixes #171
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Apr 7, 2012
1 parent 05d6e02 commit 74ca9a4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -156,7 +156,6 @@ The first argument can be either a url or an options object. The only required o
* `followRedirect` - follow HTTP 3xx responses as redirects. defaults to true.
* `followAllRedirects` - follow non-GET HTTP 3xx responses as redirects. defaults to false.
* `maxRedirects` - the maximum number of redirects to follow, defaults to 10.
* `onResponse` - If true the callback will be fired on the "response" event instead of "end". If a function it will be called on "response" and not effect the regular semantics of the main callback on "end".
* `encoding` - Encoding to be used on `setEncoding` of response data. If set to `null`, the body is returned as a Buffer.
* `pool` - A hash object containing the agents for these requests. If omitted this request will use the global pool which is set to node's default maxSockets.
* `pool.maxSockets` - Integer containing the maximum amount of sockets in the pool.
Expand Down
16 changes: 4 additions & 12 deletions main.js
Expand Up @@ -119,6 +119,8 @@ Request.prototype.init = function (options) {
self._callback.apply(self, arguments)
self._callbackCalled = true
}
self.on('error', self.callback.bind())
self.on('complete', self.callback.bind(self, null))
}

if (self.url) {
Expand Down Expand Up @@ -187,11 +189,6 @@ Request.prototype.init = function (options) {
self.host = self.uri.hostname
}

if (self.onResponse === true) {
self.onResponse = self.callback
delete self.callback
}

self.clientErrorHandler = function (error) {
if (self._aborted) return

Expand All @@ -209,8 +206,6 @@ Request.prototype.init = function (options) {
}
self.emit('error', error)
}
if (self.onResponse) self.on('error', function (e) {self.onResponse(e)})
if (self.callback) self.on('error', function (e) {self.callback(e)})

if (options.form) {
self.form(options.form)
Expand Down Expand Up @@ -490,9 +485,6 @@ Request.prototype.start = function () {

self.emit('response', response)

if (self.onResponse) {
self.onResponse(null, response)
}
if (self.callback) {
var buffer = []
var bodyLen = 0
Expand Down Expand Up @@ -524,8 +516,8 @@ Request.prototype.start = function () {
response.body = JSON.parse(response.body)
} catch (e) {}
}

self.callback(null, response, response.body)
self.emit('complete', response, response.body)
})
}
}
Expand Down

0 comments on commit 74ca9a4

Please sign in to comment.