Skip to content

Commit

Permalink
Handle errors when extending response.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Oct 13, 2023
1 parent 334b190 commit 3a94329
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/grant/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ module.exports = function code(options, issue, extend) {
var params = { code: code };
if (txn.req && txn.req.state) { params.state = txn.req.state; }
extend(txn, function(err, exparams) {
if (err) { return next(err); }
if (exparams) { utils.merge(params, exparams); }
complete(function(err) {
if (err) { return next(err); }
Expand Down
37 changes: 35 additions & 2 deletions test/grant/code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,6 @@ describe('grant.code', function() {
}

function extend(txn, done) {
console.log('EXTEND!');
console.log(txn);
if (txn.client.id !== 'c123') { return done(new Error('incorrect txn argument')); }
return done(null, { session_state: 'c1a43afe' });
}
Expand Down Expand Up @@ -642,6 +640,41 @@ describe('grant.code', function() {
});
});

describe('encountering an error while extending response', function() {
var err;

before(function(done) {
function issue(client, redirectURI, user, done) {
return done(null, 'xyz');
}

function extend(txn, done) {
return done(new Error('something went wrong'));
}

chai.oauth2orize.grant(code(issue, extend))
.txn(function(txn) {
txn.client = { id: 'cERROR', name: 'Example' };
txn.redirectURI = 'http://www.example.com/auth/callback';
txn.req = {
redirectURI: 'http://example.com/auth/callback'
};
txn.user = { id: 'u123', name: 'Bob' };
txn.res = { allow: true };
})
.next(function(e) {
err = e;
done();
})
.decide();
});

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

describe('encountering an error while completing transaction', function() {
var err;

Expand Down

0 comments on commit 3a94329

Please sign in to comment.