Permalink
Browse files

Unified error and complete handling. Fixes #171

  • Loading branch information...
1 parent 05d6e02 commit 74ca9a4852b666d30dd71421e8cc8b8a83177148 @mikeal mikeal committed Apr 7, 2012
Showing with 4 additions and 13 deletions.
  1. +0 −1 README.md
  2. +4 −12 main.js
View
1 README.md
@@ -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.
View
16 main.js
@@ -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) {
@@ -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
@@ -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)
@@ -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
@@ -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)
})
}
}

0 comments on commit 74ca9a4

Please sign in to comment.