Browse Source

pass errors in stream mode

Closes #97
http2
Vsevolod Strukchinsky 10 years ago
parent
commit
e0863dcce9
  1. 8
      index.js
  2. 21
      test/test-stream.js

8
index.js

@ -161,11 +161,19 @@ function asStream(opts) {
ee.on('response', function (res) {
proxy.setReadable(res);
var statusCode = res.statusCode;
if (statusCode < 200 || statusCode > 299) {
proxy.emit('error', new got.HTTPError(statusCode, opts), null, res);
}
proxy.emit('response', res);
});
ee.on('redirect', proxy.emit.bind(proxy, 'redirect'));
ee.on('error', proxy.emit.bind(proxy, 'error'));
return proxy;
}

21
test/test-stream.js

@ -19,6 +19,11 @@ s.on('/redirect', function (req, res) {
res.end();
});
s.on('/error', function (req, res) {
res.statusCode = 404;
res.end();
});
test('setup', function (t) {
s.listen(s.port, function () {
t.end();
@ -82,6 +87,22 @@ test('response event', function (t) {
});
});
test('error event', function (t) {
t.plan(4);
got.stream(s.url + '/error')
.on('error', function (err, data, res) {
t.equal(err.message, 'Response code 404 (Not Found)');
t.equal(null, data);
t.ok(res);
});
got.stream('.com')
.on('error', function (err) {
t.ok(/getaddrinfo ENOTFOUND/.test(err.message));
});
});
test('cleanup', function (t) {
s.close();
t.end();

Loading…
Cancel
Save