Skip to content

Commit c0053b3

Browse files
imlucaskangas
authored andcommitted
fix(client): Better error handling for tokens
1 parent 35a3cfc commit c0053b3

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

scout-client/lib/token.js

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,11 @@ function Token(config) {
1111
this.config = config;
1212
this.expirationRedLine = 15 * 1000;
1313
this.session = {};
14-
this.readable = false;
1514

1615
process.nextTick(function() {
17-
this.bake(function(err, res) {
16+
this.bake(function(err) {
1817
if (err) return this.emit('error', err);
19-
20-
this.session = res;
2118
this.schedule();
22-
23-
this.readable = true;
24-
debug('emit readable!');
25-
this.emit('readable');
2619
}.bind(this));
2720
}.bind(this));
2821
}
@@ -47,21 +40,22 @@ Token.prototype.close = function(fn) {
4740
clearTimeout(this.refreshTimeout);
4841
debug('closing token');
4942
request.del(this.config.scout + '/api/v1/token')
50-
.set('Accept', 'application/json')
51-
.set('Authorization', 'Bearer ' + this.session.token)
52-
.end(function(err, res) {
53-
debug('response from token close');
54-
fn(err, res);
55-
});
43+
.set('Accept', 'application/json')
44+
.set('Authorization', 'Bearer ' + this.session.token)
45+
.end(function(err, res) {
46+
debug('response from token close');
47+
fn(err, res);
48+
});
5649
};
5750

5851
Token.prototype.bake = function(done) {
5952
var payload = {
6053
seed: this.config.seed
6154
};
6255

63-
if (this.config.timeout)
56+
if (this.config.timeout) {
6457
payload.timeout = this.config.timeout;
58+
}
6559

6660
if (this.config.auth) {
6761
Object.keys(this.config.auth).map(function(name) {
@@ -73,36 +67,43 @@ Token.prototype.bake = function(done) {
7367
}
7468
debug('getting token for', this.config.seed, payload);
7569
request.post(this.config.scout + '/api/v1/token')
76-
.send(payload)
77-
.set('Accept', 'application/json')
78-
.end(function(err, res) {
79-
if (err) return done(err);
80-
81-
if (!err && res.status >= 400) {
82-
err = new Error(res.body ? res.body.message : res.text);
83-
err.code = res.status;
84-
Error.captureStackTrace(err, Token.prototype.bake);
85-
return done(err);
86-
}
87-
88-
if (!res.body.expires_at || !res.body.created_at) {
89-
return done(new Error('Malformed response. Missing expires_at or created_at'));
90-
}
91-
92-
if (new Date(res.body.expires_at) - Date.now() < (1 * 60 * 1000)) {
93-
return done(new Error('Got an expires that is less than a minute from now.'));
94-
}
95-
96-
done(null, res.body);
97-
}.bind(this));
70+
.send(payload)
71+
.set('Accept', 'application/json')
72+
.end(function(err, res) {
73+
if (err) {
74+
if (res && res.body) {
75+
err.message += ': ' + res.body.message;
76+
}
77+
console.error('Error getting token:', err);
78+
return done(err);
79+
}
80+
81+
if (!err && res.status >= 400) {
82+
err = new Error(res.body ? res.body.message : res.text);
83+
err.code = res.status;
84+
Error.captureStackTrace(err, Token.prototype.bake);
85+
return done(err);
86+
}
87+
88+
if (!res.body.expires_at || !res.body.created_at) {
89+
return done(new Error('Malformed response. Missing expires_at or created_at'));
90+
}
91+
92+
if (new Date(res.body.expires_at) - Date.now() < (1 * 60 * 1000)) {
93+
return done(new Error('Got an expires that is less than a minute from now.'));
94+
}
95+
96+
this.session = res.body;
97+
this.emit('data', this.session);
98+
99+
done(null, res.body);
100+
}.bind(this));
98101
};
99102

100103
Token.prototype.refresh = function() {
101-
this.bake(function(err, res) {
104+
this.bake(function(err) {
102105
if (err) return this.emit('error', err);
103-
if (!res) return this.emit('error', new Error('Empty response with no error'));
104106

105-
this.session = res;
106107
debug('token refreshed successfully');
107108
return this.schedule();
108109
}.bind(this));
@@ -114,5 +115,6 @@ Token.prototype.schedule = function() {
114115
return this;
115116
}
116117
var ms = (new Date(this.session.expires_at) - Date.now()) - this.expirationRedLine;
118+
debug('scheduling token refresh %dms from now', ms);
117119
this.refreshTimeout = setTimeout(this.refresh.bind(this), ms);
118120
};

0 commit comments

Comments
 (0)