diff --git a/lib/exchange/clientCredentials.js b/lib/exchange/clientCredentials.js index 4963dbc4..142d33da 100644 --- a/lib/exchange/clientCredentials.js +++ b/lib/exchange/clientCredentials.js @@ -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; @@ -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); } } } diff --git a/test/exchange/authorizationCode.test.js b/test/exchange/authorizationCode.test.js index 83b5ee10..a3ed3406 100644 --- a/test/exchange/authorizationCode.test.js +++ b/test/exchange/authorizationCode.test.js @@ -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) { diff --git a/test/exchange/clientCredentials.test.js b/test/exchange/clientCredentials.test.js index f3e16f5c..d4a2849a 100644 --- a/test/exchange/clientCredentials.test.js +++ b/test/exchange/clientCredentials.test.js @@ -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')); } @@ -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;