[Pkg-javascript-commits] [node-stream-http] 30/208: Error handling improvements
Bastien Roucariès
rouca at moszumanska.debian.org
Sun Aug 13 13:39:25 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-stream-http.
commit 89c8220dac3fe385d47fd97bef073ffac74410d8
Author: John Hiesey <john at hiesey.com>
Date: Tue Jul 7 18:49:17 2015 -0700
Error handling improvements
---
lib/request.js | 48 ++++++++++++++++++++++++++++++++++++++----------
lib/response.js | 2 +-
2 files changed, 39 insertions(+), 11 deletions(-)
diff --git a/lib/request.js b/lib/request.js
index ef60060..ae790cb 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -117,10 +117,19 @@ ClientRequest.prototype._onFinish = function () {
}).then(function (response) {
self._fetchResponse = response
self._connect()
+ }).then(undefined, function (reason) {
+ self.emit('error', reason)
})
} else {
var xhr = self._xhr = new window.XMLHttpRequest()
- xhr.open(self._opts.method, self._url, true)
+ try {
+ xhr.open(self._opts.method, self._url, true)
+ } catch (err) {
+ process.nextTick(function () {
+ self.emit('error', err)
+ })
+ return
+ }
// Can't set responseType on really old browsers
if ('responseType' in xhr)
@@ -141,10 +150,7 @@ ClientRequest.prototype._onFinish = function () {
switch (xhr.readyState) {
case rStates.LOADING:
case rStates.DONE:
- if (!self._response && statusValid(xhr))
- self._connect()
- if (self._response)
- self._response._onXHRReadyStateChange()
+ self._onXHRProgress()
break
}
}
@@ -152,15 +158,25 @@ ClientRequest.prototype._onFinish = function () {
// in onprogress, not in onreadystatechange with xhr.readyState = 3
if (self._mode === 'moz-chunked-arraybuffer') {
xhr.onprogress = function () {
- self._response._onXHRReadyStateChange()
+ self._onXHRProgress()
}
}
- xhr.send(body)
+ xhr.onerror = function () {
+ self.emit('error', new Error('XHR error'))
+ }
+
+ try {
+ xhr.send(body)
+ } catch (err) {
+ process.nextTick(function () {
+ self.emit('error', err)
+ })
+ return
+ }
+
// This is the best approximation to where 'socket' should be fired
- process.nextTick(function () {
- self.emit('socket')
- })
+ self.emit('socket')
}
}
@@ -176,6 +192,18 @@ function statusValid (xhr) {
}
}
+ClientRequest.prototype._onXHRProgress = function () {
+ var self = this
+
+ if (!statusValid(self._xhr) || self._failed)
+ return
+
+ if (!self._response)
+ self._connect()
+
+ self._response._onXHRProgress()
+}
+
ClientRequest.prototype._connect = function () {
var self = this
diff --git a/lib/response.js b/lib/response.js
index 4d65016..d95b835 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -86,7 +86,7 @@ util.inherits(IncomingMessage, stream.Readable)
IncomingMessage.prototype._read = function () {}
-IncomingMessage.prototype._onXHRReadyStateChange = function () {
+IncomingMessage.prototype._onXHRProgress = function () {
var self = this
var xhr = self._xhr
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stream-http.git
More information about the Pkg-javascript-commits
mailing list