Skip to content

Commit

Permalink
fix(v1): queryChainCode Error Handling changed
Browse files Browse the repository at this point in the history
NodeSDK has changed the way errors are done on queryChainCode

Signed-off-by: Dave Kelsey <[email protected]>
  • Loading branch information
Dave Kelsey committed Mar 16, 2017
1 parent d6178db commit 44753c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 7 additions & 2 deletions packages/composer-connector-hlfv1/lib/hlfconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@ class HLFConnection extends Connection {
}

proposalResponses.forEach((proposalResponse) => {
if (proposalResponse.error) {
throw proposalResponse.error;
if (proposalResponse instanceof Error) {
console.log('throwing proposal response');
throw proposalResponse;
} else if (proposalResponse.response.status === 200) {
return true;
}
Expand Down Expand Up @@ -550,6 +551,10 @@ class HLFConnection extends Connection {
throw new Error('No payloads were returned from the query request:' + functionName);
}
const payload = payloads[0];
if (payload instanceof Error) {
// will be handled by the catch block
throw payload;
}
LOG.exit(payload);
return payload;
})
Expand Down
20 changes: 18 additions & 2 deletions packages/composer-connector-hlfv1/test/hlfconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ describe('HLFConnection', () => {
// This is the generated nonce.
sandbox.stub(utils, 'getNonce').returns('11111111-1111-1111-1111-111111111111');
// This is the deployment proposal and response (from the peers).
const proposalResponses = [ {'error': new Error('such error')} ];
const proposalResponses = [ new Error('such error') ];
const proposal = { proposal: 'i do' };
const header = { header: 'gooooal' };
// This is the generated transaction
Expand Down Expand Up @@ -725,6 +725,22 @@ describe('HLFConnection', () => {
.should.be.rejectedWith(/No payloads were returned from the query request/);
});

it('should throw any responses that are errors', () => {
// This is the generated nonce.
sandbox.stub(utils, 'getNonce').returns('11111111-1111-1111-1111-111111111111');
// This is the generated transaction
mockChain.buildTransactionID.returns('00000000-0000-0000-0000-000000000000');
// mock out getUserContext version in case we need to return to using this one
mockChain.buildTransactionID_getUserContext.resolves('00000000-0000-0000-0000-000000000000');
// This is the transaction proposal and response (from the peers).
const response = [ new Error('such error') ];
// This is the response from the chaincode.
mockChain.queryByChaincode.resolves(response);
return connection.queryChainCode(mockSecurityContext, 'myfunc', ['arg1', 'arg2'])
.should.be.rejectedWith(/such error/);

});

});

describe('#invokeChainCode', () => {
Expand Down Expand Up @@ -817,7 +833,7 @@ describe('HLFConnection', () => {
// mock out getUserContext version in case we need to return to using this one
mockChain.buildTransactionID_getUserContext.resolves('00000000-0000-0000-0000-000000000000');
// This is the transaction proposal and response (from the peers).
const proposalResponses = [ {'error': new Error('such error')} ];
const proposalResponses = [ new Error('such error') ];
const proposal = { proposal: 'i do' };
const header = { header: 'gooooal' };
mockChain.sendTransactionProposal.resolves([ proposalResponses, proposal, header ]);
Expand Down

0 comments on commit 44753c5

Please sign in to comment.