fabric-samples/balance-transfer/app/create-channel.js
Bret Harrison e9b9477a31 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>
2019-01-04 14:23:38 +00:00

81 lines
3.1 KiB
JavaScript

/**
* Copyright 2017 IBM All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var fs = require('fs');
var path = require('path');
var helper = require('./helper.js');
var logger = helper.getLogger('Create-Channel');
//Attempt to send a request to the orderer with the sendTransaction method
var createChannel = async function(channelName, channelConfigPath, username, orgName) {
logger.debug('\n====== Creating Channel \'' + channelName + '\' ======\n');
try {
// first setup the client for this org
var client = await helper.getClientForOrg(orgName);
logger.debug('Successfully got the fabric client for the organization "%s"', orgName);
// read in the envelope for the channel config raw bytes
var envelope = fs.readFileSync(path.join(__dirname, channelConfigPath));
// extract the channel config bytes from the envelope to be signed
var channelConfig = client.extractChannelConfig(envelope);
//Acting as a client in the given organization provided with "orgName" param
// sign the channel config bytes as "endorsement", this is required by
// the orderer's channel creation policy
// this will use the admin identity assigned to the client when the connection profile was loaded
let signature = client.signChannelConfig(channelConfig);
let request = {
config: channelConfig,
signatures: [signature],
name: channelName,
txId: client.newTransactionID(true) // get an admin based transactionID
};
// send to orderer
const result = await client.createChannel(request)
logger.debug(' result ::%j', result);
if (result) {
if (result.status === 'SUCCESS') {
logger.debug('Successfully created the channel.');
const response = {
success: true,
message: 'Channel \'' + channelName + '\' created Successfully'
};
return response;
} else {
logger.error('Failed to create the channel. status:' + result.status + ' reason:' + result.info);
const response = {
success: false,
message: 'Channel \'' + channelName + '\' failed to create status:' + result.status + ' reason:' + result.info
};
return response;
}
} else {
logger.error('\n!!!!!!!!! Failed to create the channel \'' + channelName +
'\' !!!!!!!!!\n\n');
const response = {
success: false,
message: 'Failed to create the channel \'' + channelName + '\'',
};
return response;
}
} catch (err) {
logger.error('Failed to initialize the channel: ' + err.stack ? err.stack : err);
throw new Error('Failed to initialize the channel: ' + err.toString());
}
};
exports.createChannel = createChannel;