Skip to content

Commit

Permalink
FABN-926: Improve messages for failed proposals
Browse files Browse the repository at this point in the history
- Make the thrown error message from fabric-network API for concise.
- Log full error received from fabric-client API.

Change-Id: I05c8454a8a2291580b61db068160abfb50824c59
Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
  • Loading branch information
bestbeforetoday committed Nov 20, 2018
1 parent bd906a8 commit 2068039
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
12 changes: 6 additions & 6 deletions fabric-network/lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ class Transaction {
// this is either an error from the sdk, peer response or chaincode response.
// we can distinguish between sdk vs peer/chaincode by the isProposalResponse flag in the future.
// TODO: would be handy to know which peer the response is from and include it here.
const warning = util.format('Response from attempted peer comms was an error: %j', responseContent);
logger.warn('_validatePeerResponses: ' + warning);
invalidResponseMsgs.push(warning);
const message = responseContent.message;
logger.warn('_validatePeerResponses: Received error response from peer:', responseContent);
invalidResponseMsgs.push(message);
invalidResponses.push(responseContent);
} else {
// anything else is a successful response ie status will be less the 400.
Expand All @@ -196,9 +196,9 @@ class Transaction {
});

if (validResponses.length === 0) {
const errorMessages = ['No valid responses from any peers.'];
invalidResponseMsgs.forEach(invalidResponse => errorMessages.push(invalidResponse));
const msg = errorMessages.join('\n');
const messages = Array.of(`No valid responses from any peers. ${invalidResponseMsgs.length} peer error responses:`,
...invalidResponseMsgs);
const msg = messages.join('\n ');
logger.error('_validatePeerResponses: ' + msg);
throw new Error(msg);
}
Expand Down
9 changes: 8 additions & 1 deletion fabric-network/test/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('Transaction', () => {
status: 200
}
};
const errorProposalResponse = Object.assign(new Error(), {response: {status: 500, payload: 'error'}});
const errorResponseMessage = 'I_AM_AN_ERROR_RESPONSE';
const errorProposalResponse = Object.assign(new Error(errorResponseMessage), {response: {status: 500, payload: 'error'}});
const emptyStringProposalResponse = {
response: {
status: 200,
Expand Down Expand Up @@ -169,6 +170,12 @@ describe('Transaction', () => {
return expect(promise).to.be.rejectedWith('No valid responses from any peers');
});

it('throws with message including underlying error message', () => {
channel.sendTransactionProposal.resolves(errorProposalResponses);
const promise = transaction.submit();
return expect(promise).to.be.rejectedWith(errorResponseMessage);
});

it('succeeds if some proposal responses are valid', () => {
channel.sendTransactionProposal.resolves(mixedProposalResponses);
const promise = transaction.submit();
Expand Down
1 change: 1 addition & 0 deletions test/typescript/integration/network-e2e/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ test('\n\n***** Network End-to-end flow: handle transaction error *****\n\n', as
const response = await contract.submitTransaction('throwError', 'a', 'b', '100');
t.fail('Transaction "throwError" should have thrown an error. Got response: ' + response.toString());
} catch (expectedErr) {
t.comment(expectedErr.message);
if (expectedErr.message.includes('throwError: an error occurred')) {
t.pass('Successfully handled invocation errors');
} else {
Expand Down

0 comments on commit 2068039

Please sign in to comment.