Skip to content

Commit b91ad63

Browse files
add back error handling.
1 parent 8200345 commit b91ad63

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/common/connection.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,12 @@ Connection.prototype.connect = function() {
129129
this.isConnecting = true;
130130
// retrieves an access token
131131
this.fetchToken(function(err, token) {
132-
if (!err) {
133-
that.token = token;
134-
}
135132
that.isConnecting = false;
133+
if (err) {
134+
that.emit('connected', err);
135+
return;
136+
}
137+
that.token = token;
136138
that.emit('connected');
137139
});
138140
};
@@ -249,7 +251,11 @@ Connection.prototype.createAuthorizedReq = function(reqOpts, callback) {
249251
reqOpts.headers['User-Agent'] = USER_AGENT;
250252
}
251253

252-
function onConnected() {
254+
function onConnected(err) {
255+
if (err) {
256+
callback(err);
257+
return;
258+
}
253259
callback(null, that.authorizeReq(reqOpts));
254260
}
255261

@@ -258,7 +264,7 @@ Connection.prototype.createAuthorizedReq = function(reqOpts, callback) {
258264
return;
259265
}
260266

261-
this.on('connected', onConnected);
267+
this.once('connected', onConnected);
262268

263269
if (!this.isConnecting) {
264270
this.connect();

test/common/connection.js

+11
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ describe('Connection', function() {
7777
conn.req({ uri: 'https://someuri' }, function() {});
7878
});
7979

80+
it('should pass error to callback', function(done) {
81+
var error = new Error('Something terrible happened.');
82+
conn.fetchToken = function(cb) {
83+
cb(error);
84+
};
85+
conn.req({}, function(err) {
86+
assert.equal(error, err);
87+
done();
88+
});
89+
});
90+
8091
it('should make other requests wait while connecting', function(done) {
8192
var numTokenFetches = 0;
8293
var requestedUris = [];

0 commit comments

Comments
 (0)