From 1acd414946d1242ca9c425210195feb33bff0872 Mon Sep 17 00:00:00 2001 From: Gabriel Andretta Date: Tue, 1 Sep 2015 15:46:40 -0300 Subject: [PATCH] Emit errors when the request fails It order for this to actually work, yields/send-json needs to be updated. A pull request has been sent https://github.com/yields/send-json/pull/5. --- lib/index.js | 6 +++++- test/index.test.js | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index be26e35..da3cf7c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -195,7 +195,11 @@ Segment.prototype.send = function(path, msg, fn) { // send send(url, msg, headers, function(err, res) { self.debug('sent %O, received %O', msg, arguments); - if (err) return fn(err); + if (err) { + self.analytics.emit('error', err); + return fn(err); + } + res.url = url; fn(null, res); }); diff --git a/test/index.test.js b/test/index.test.js index da34851..199328d 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -405,6 +405,18 @@ describe('Segment.io', function() { }); }); + it('should emit an error when the request doesn\'t succeed', function(done) { + var emittedErr; + analytics.on('error', function(err) { emittedErr = err; }); + + protocol('http:'); + segment.send('/u', { userId: 'id' }, function(err) { + assert(err); + assert.strictEqual(emittedErr, err); + done(); + }); + }); + describe('/g', ensure('/g', { groupId: 'gid', userId: 'uid' })); describe('/p', ensure('/p', { userId: 'id', name: 'page', properties: {} })); describe('/a', ensure('/a', { userId: 'id', from: 'b', to: 'a' }));