Skip to content
This repository has been archived by the owner on Nov 23, 2019. It is now read-only.

Emit errors when the request fails #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }));
Expand Down