Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
address node-sdk returning connect error (#3639)
Browse files Browse the repository at this point in the history
* address node-sdk returning connect error

Signed-off-by: Dave Kelsey <[email protected]>

* change to use code in Error object

Signed-off-by: Dave Kelsey <[email protected]>
  • Loading branch information
Dave Kelsey authored and nklincoln committed Mar 20, 2018
1 parent 81475b3 commit f1b7a1f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
8 changes: 7 additions & 1 deletion packages/composer-connector-hlfv1/lib/hlfqueryhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class HLFQueryHandler {
// peer to query.
let failedPeer = this.queryPeerIndex; // could be -1 if first attempt
this.queryPeerIndex = -1;
//for (let i in this.allQueryPeers) { // i is a string
for (let i = 0; i < this.allQueryPeers.length && !success; i++) {
if (i === failedPeer) {
continue;
Expand Down Expand Up @@ -147,6 +146,13 @@ class HLFQueryHandler {
throw new Error('No payloads were returned from the query request:' + functionName);
}
const payload = payloads[0];

// if it has a code value is 14, means unavailable, so throw that error
// code 2 looks like it is a chaincode response that was an error.
if (payload instanceof Error && payload.code && payload.code === 14) {
throw payload;
}

LOG.exit(method, payload);
return payload;

Expand Down
22 changes: 16 additions & 6 deletions packages/composer-connector-hlfv1/test/hlfqueryhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ describe('HLFQueryHandler', () => {
.should.be.rejectedWith(/No payloads were returned from the query request/);
});

it('should return any responses that are errors', async () => {
const response = [ new Error('such error') ];
it('should return any responses that are errors and not UNAVAILABLE', async () => {
const response = new Error('such error');
mockChannel.queryByChaincode.resolves([response]);
mockConnection.businessNetworkIdentifier = 'org-acme-biznet';
let result = await queryHandler.querySinglePeer(mockPeer2, mockTransactionID, 'myfunc', ['arg1', 'arg2']);
Expand All @@ -208,14 +208,24 @@ describe('HLFQueryHandler', () => {
args: ['arg1', 'arg2'],
targets: [mockPeer2]
});
result[0].should.be.instanceOf(Error);
result[0].message.should.equal('such error');
result.should.be.instanceOf(Error);
result.message.should.equal('such error');
});

it('should throw if query request fails', () => {
mockChannel.queryByChaincode.rejects(new Error('Connect Failed'));
it('should throw any responses that are errors and code 14 being unavailable.', () => {
const response = new Error('14 UNAVAILABLE: Connect Failed');
response.code = 14;
mockChannel.queryByChaincode.resolves([response]);
mockConnection.businessNetworkIdentifier = 'org-acme-biznet';
return queryHandler.querySinglePeer(mockPeer2, 'txid', 'myfunc', ['arg1', 'arg2'])
.should.be.rejectedWith(/Connect Failed/);
});


it('should throw if query request fails', () => {
mockChannel.queryByChaincode.rejects(new Error('Query Failed'));
return queryHandler.querySinglePeer(mockPeer2, 'txid', 'myfunc', ['arg1', 'arg2'])
.should.be.rejectedWith(/Query Failed/);

});

Expand Down

0 comments on commit f1b7a1f

Please sign in to comment.