Skip to content

Commit e9b9477

Browse files
harrisobdenyeart
authored andcommitted
FAB-13372 Fabric-Samples return error msg
Update the Balance Transfer to return the fabric error. The sample application was updated to both return the error in the REST response and to post the error to the log. The sample was updated with the 'channel.close()' so that users will note how to shutdown a channel and not hold connections open. Change-Id: I49f20a50340adff52cf57db00a121ffd75eb1827 Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
1 parent e6ce28c commit e9b9477

8 files changed

+184
-139
lines changed

balance-transfer/app/create-channel.js

+23-10
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,32 @@ var createChannel = async function(channelName, channelConfigPath, username, org
4545
};
4646

4747
// send to orderer
48-
var response = await client.createChannel(request)
49-
logger.debug(' response ::%j', response);
50-
if (response && response.status === 'SUCCESS') {
51-
logger.debug('Successfully created the channel.');
52-
let response = {
53-
success: true,
54-
message: 'Channel \'' + channelName + '\' created Successfully'
55-
};
56-
return response;
48+
const result = await client.createChannel(request)
49+
logger.debug(' result ::%j', result);
50+
if (result) {
51+
if (result.status === 'SUCCESS') {
52+
logger.debug('Successfully created the channel.');
53+
const response = {
54+
success: true,
55+
message: 'Channel \'' + channelName + '\' created Successfully'
56+
};
57+
return response;
58+
} else {
59+
logger.error('Failed to create the channel. status:' + result.status + ' reason:' + result.info);
60+
const response = {
61+
success: false,
62+
message: 'Channel \'' + channelName + '\' failed to create status:' + result.status + ' reason:' + result.info
63+
};
64+
return response;
65+
}
5766
} else {
5867
logger.error('\n!!!!!!!!! Failed to create the channel \'' + channelName +
5968
'\' !!!!!!!!!\n\n');
60-
throw new Error('Failed to create the channel \'' + channelName + '\'');
69+
const response = {
70+
success: false,
71+
message: 'Failed to create the channel \'' + channelName + '\'',
72+
};
73+
return response;
6174
}
6275
} catch (err) {
6376
logger.error('Failed to initialize the channel: ' + err.stack ? err.stack : err);

balance-transfer/app/install-chaincode.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,17 @@ var installChaincode = async function(peers, chaincodeName, chaincodePath,
4747
// lets have a look at the responses to see if they are
4848
// all good, if good they will also include signatures
4949
// required to be committed
50-
var all_good = true;
51-
for (var i in proposalResponses) {
52-
let one_good = false;
53-
if (proposalResponses && proposalResponses[i].response &&
54-
proposalResponses[i].response.status === 200) {
55-
one_good = true;
50+
for (const i in proposalResponses) {
51+
if (proposalResponses[i] instanceof Error) {
52+
error_message = util.format('install proposal resulted in an error :: %s', proposalResponses[i].toString());
53+
logger.error(error_message);
54+
} else if (proposalResponses[i].response && proposalResponses[i].response.status === 200) {
5655
logger.info('install proposal was good');
5756
} else {
58-
logger.error('install proposal was bad %j',proposalResponses.toJSON());
57+
all_good = false;
58+
error_message = util.format('install proposal was bad for an unknown reason %j', proposalResponses[i]);
59+
logger.error(error_message);
5960
}
60-
all_good = all_good & one_good;
61-
}
62-
if (all_good) {
63-
logger.info('Successfully sent install Proposal and received ProposalResponse');
64-
} else {
65-
error_message = 'Failed to send install Proposal or receive valid response. Response null or status is not 200'
66-
logger.error(error_message);
6761
}
6862
} catch(error) {
6963
logger.error('Failed to install due to error: ' + error.stack ? error.stack : error);
@@ -74,15 +68,19 @@ var installChaincode = async function(peers, chaincodeName, chaincodePath,
7468
let message = util.format('Successfully installed chaincode');
7569
logger.info(message);
7670
// build a response to send back to the REST caller
77-
let response = {
71+
const response = {
7872
success: true,
7973
message: message
8074
};
8175
return response;
8276
} else {
8377
let message = util.format('Failed to install due to:%s',error_message);
8478
logger.error(message);
85-
throw new Error(message);
79+
const response = {
80+
success: false,
81+
message: message
82+
};
83+
return response;
8684
}
8785
};
8886
exports.installChaincode = installChaincode;

balance-transfer/app/instantiate-chaincode.js

+57-54
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,35 @@
1414
* limitations under the License.
1515
*/
1616
'use strict';
17-
var util = require('util');
18-
var helper = require('./helper.js');
19-
var logger = helper.getLogger('instantiate-chaincode');
17+
const util = require('util');
18+
const helper = require('./helper.js');
19+
const logger = helper.getLogger('instantiate-chaincode');
2020

21-
var instantiateChaincode = async function(peers, channelName, chaincodeName, chaincodeVersion, functionName, chaincodeType, args, username, org_name) {
21+
const instantiateChaincode = async function(peers, channelName, chaincodeName, chaincodeVersion, functionName, chaincodeType, args, username, org_name) {
2222
logger.debug('\n\n============ Instantiate chaincode on channel ' + channelName +
2323
' ============\n');
24-
var error_message = null;
25-
24+
let error_message = null;
25+
let client = null;
26+
let channel = null;
2627
try {
2728
// first setup the client for this org
28-
var client = await helper.getClientForOrg(org_name, username);
29+
client = await helper.getClientForOrg(org_name, username);
2930
logger.debug('Successfully got the fabric client for the organization "%s"', org_name);
30-
var channel = client.getChannel(channelName);
31+
channel = client.getChannel(channelName);
3132
if(!channel) {
3233
let message = util.format('Channel %s was not defined in the connection profile', channelName);
3334
logger.error(message);
3435
throw new Error(message);
3536
}
36-
var tx_id = client.newTransactionID(true); // Get an admin based transactionID
37+
const tx_id = client.newTransactionID(true); // Get an admin based transactionID
3738
// An admin based transactionID will
3839
// indicate that admin identity should
3940
// be used to sign the proposal request.
4041
// will need the transaction ID string for the event registration later
41-
var deployId = tx_id.getTransactionID();
42+
const deployId = tx_id.getTransactionID();
4243

4344
// send proposal to endorser
44-
var request = {
45+
const request = {
4546
targets : peers,
4647
chaincodeId: chaincodeName,
4748
chaincodeType: chaincodeType,
@@ -70,23 +71,24 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
7071
// the returned object has both the endorsement results
7172
// and the actual proposal, the proposal will be needed
7273
// later when we send a transaction to the orderer
73-
var proposalResponses = results[0];
74-
var proposal = results[1];
74+
const proposalResponses = results[0];
75+
const proposal = results[1];
7576

76-
// lets have a look at the responses to see if they are
77-
// all good, if good they will also include signatures
78-
// required to be committed
79-
var all_good = true;
80-
for (var i in proposalResponses) {
81-
let one_good = false;
82-
if (proposalResponses && proposalResponses[i].response &&
83-
proposalResponses[i].response.status === 200) {
84-
one_good = true;
77+
// look at the responses to see if they are all are good
78+
// response will also include signatures required to be committed
79+
let all_good = true;
80+
for (const i in proposalResponses) {
81+
if (proposalResponses[i] instanceof Error) {
82+
all_good = false;
83+
error_message = util.format('instantiate proposal resulted in an error :: %s', proposalResponses[i].toString());
84+
logger.error(error_message);
85+
} else if (proposalResponses[i].response && proposalResponses[i].response.status === 200) {
8586
logger.info('instantiate proposal was good');
8687
} else {
87-
logger.error('instantiate proposal was bad');
88+
all_good = false;
89+
error_message = util.format('instantiate proposal was bad for an unknown reason %j', proposalResponses[i]);
90+
logger.error(error_message);
8891
}
89-
all_good = all_good & one_good;
9092
}
9193

9294
if (all_good) {
@@ -97,8 +99,8 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
9799

98100
// wait for the channel-based event hub to tell us that the
99101
// instantiate transaction was committed on the peer
100-
var promises = [];
101-
let event_hubs = channel.getChannelEventHubsForOrg();
102+
const promises = [];
103+
const event_hubs = channel.getChannelEventHubsForOrg();
102104
logger.debug('found %s eventhubs for this organization %s',event_hubs.length, org_name);
103105
event_hubs.forEach((eh) => {
104106
let instantiateEventPromise = new Promise((resolve, reject) => {
@@ -138,22 +140,22 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
138140
promises.push(instantiateEventPromise);
139141
});
140142

141-
var orderer_request = {
143+
const orderer_request = {
142144
txId: tx_id, // must include the transaction id so that the outbound
143-
// transaction to the orderer will be signed by the admin
144-
// id as was the proposal above, notice that transactionID
145-
// generated above was based on the admin id not the current
146-
// user assigned to the 'client' instance.
145+
// transaction to the orderer will be signed by the admin id
146+
// the same as the proposal above, notice that transactionID
147+
// generated above was based on the admin id not the current
148+
// user assigned to the 'client' instance.
147149
proposalResponses: proposalResponses,
148150
proposal: proposal
149151
};
150-
var sendPromise = channel.sendTransaction(orderer_request);
152+
const sendPromise = channel.sendTransaction(orderer_request);
151153
// put the send to the orderer last so that the events get registered and
152154
// are ready for the orderering and committing
153155
promises.push(sendPromise);
154-
let results = await Promise.all(promises);
156+
const results = await Promise.all(promises);
155157
logger.debug(util.format('------->>> R E S P O N S E : %j', results));
156-
let response = results.pop(); // orderer results are last in the results
158+
const response = results.pop(); // orderer results are last in the results
157159
if (response.status === 'SUCCESS') {
158160
logger.info('Successfully sent transaction to the orderer.');
159161
} else {
@@ -162,9 +164,9 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
162164
}
163165

164166
// now see what each of the event hubs reported
165-
for(let i in results) {
166-
let event_hub_result = results[i];
167-
let event_hub = event_hubs[i];
167+
for(const i in results) {
168+
const event_hub_result = results[i];
169+
const event_hub = event_hubs[i];
168170
logger.debug('Event results for event hub :%s',event_hub.getPeerAddr());
169171
if(typeof event_hub_result === 'string') {
170172
logger.debug(event_hub_result);
@@ -173,30 +175,31 @@ var instantiateChaincode = async function(peers, channelName, chaincodeName, cha
173175
logger.debug(event_hub_result.toString());
174176
}
175177
}
176-
} else {
177-
error_message = util.format('Failed to send Proposal and receive all good ProposalResponse');
178-
logger.debug(error_message);
179178
}
180179
} catch (error) {
181180
logger.error('Failed to send instantiate due to error: ' + error.stack ? error.stack : error);
182181
error_message = error.toString();
182+
} finally {
183+
if (channel) {
184+
channel.close();
185+
}
183186
}
184187

185-
if (!error_message) {
186-
let message = util.format(
187-
'Successfully instantiate chaincode in organization %s to the channel \'%s\'',
188-
org_name, channelName);
189-
logger.info(message);
190-
// build a response to send back to the REST caller
191-
let response = {
192-
success: true,
193-
message: message
194-
};
195-
return response;
196-
} else {
197-
let message = util.format('Failed to instantiate. cause:%s',error_message);
188+
let success = true;
189+
let message = util.format('Successfully instantiate chaincode in organization %s to the channel \'%s\'', org_name, channelName);
190+
if (error_message) {
191+
message = util.format('Failed to instantiate the chaincode. cause:%s',error_message);
192+
success = false;
198193
logger.error(message);
199-
throw new Error(message);
194+
} else {
195+
logger.info(message);
200196
}
197+
198+
// build a response to send back to the REST caller
199+
const response = {
200+
success: success,
201+
message: message
202+
};
203+
return response;
201204
};
202205
exports.instantiateChaincode = instantiateChaincode;

0 commit comments

Comments
 (0)