Skip to content

Commit

Permalink
Catch exceptions thrown from issue callback in client credentials exc…
Browse files Browse the repository at this point in the history
…hange.
  • Loading branch information
jaredhanson committed Sep 18, 2013
1 parent 4261fc6 commit c23db9d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
16 changes: 10 additions & 6 deletions lib/exchange/clientCredentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var utils = require('../utils')
* @return {Function}
* @api public
*/
module.exports = function clientCredentials(options, issue) {
module.exports = function(options, issue) {
if (typeof options == 'function') {
issue = options;
options = undefined;
Expand Down Expand Up @@ -112,11 +112,15 @@ module.exports = function clientCredentials(options, issue) {
res.end(json);
}

var arity = issue.length;
if (arity == 3) {
issue(client, scope, issued);
} else { // arity == 2
issue(client, issued);
try {
var arity = issue.length;
if (arity == 3) {
issue(client, scope, issued);
} else { // arity == 2
issue(client, issued);
}
} catch (ex) {
return next(ex);
}
}
}
2 changes: 1 addition & 1 deletion test/exchange/authorizationCode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe('exchange.authorizationCode', function() {
});
});

describe('encountering an error while issuing an access token', function() {
describe('encountering an exception while issuing an access token', function() {
var response, err;

before(function(done) {
Expand Down
24 changes: 24 additions & 0 deletions test/exchange/clientCredentials.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe('exchange.clientCredentials', function() {
return done(null, 's3cr1t', 'blahblag', { 'token_type': 'foo', 'expires_in': 3600 })
} else if (client.id == 'cUN') {
return done(null, false)
} else if (client.id == 'cTHROW') {
throw new Error('something was thrown')
}
return done(new Error('something is wrong'));
}
Expand Down Expand Up @@ -252,6 +254,28 @@ describe('exchange.clientCredentials', function() {
});
});

describe('encountering an exception while issuing an access token', function() {
var response, err;

before(function(done) {
chai.connect(clientCredentials(issue))
.req(function(req) {
req.user = { id: 'cTHROW', name: 'Example' };
req.body = {};
})
.next(function(e) {
err = e;
done();
})
.dispatch();
});

it('should error', function() {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('something was thrown');
});
});

describe('handling a request without a body', function() {
var response, err;

Expand Down

0 comments on commit c23db9d

Please sign in to comment.