[英]Error can't be catched?: NodeJS/Hyperledger
What is the best way to hide an error?隐藏错误的最佳方法是什么?
I want to get rid of this error (to hide it, or even better solve it if someone knows where is the problem):我想摆脱这个错误(隐藏它,或者如果有人知道问题出在哪里更好地解决它):
2019-12-12T17:43:54.626Z - error: [Orderer.js]: sendBroadcast - on error: "Error: 2 UNKNOWN: Stream removed\n
at Object.exports.createStatusError (/home/hl-root/fabric-samples/fullgear-4node-setup/app/node_modules/grpc/src/common.js:91:15)\n
at ClientDuplexStream._emitStatusIfDone (/home/hl-root/fabric-samples/fullgear-4node-setup/app/node_modules/grpc/src/client.js:233:26)\n
at ClientDuplexStream._receiveStatus (/home/hl-root/fabric-samples/fullgear-4node-setup/app/node_modules/grpc/src/client.js:211:8)\n
at Object.onReceiveStatus (/home/hl-root/fabric-samples/fullgear-4node-setup/app/node_modules/grpc/src/client_interceptors.js:1306:15)\n
at InterceptingListener._callNext (/home/hl-root/fabric-samples/fullgear-4node-setup/app/node_modules/grpc/src/client_interceptors.js:568:42)\n
at InterceptingListener.onReceiveStatus (/home/hl-root/fabric-samples/fullgear-4node-setup/app/node_modules/grpc/src/client_interceptors.js:618:8)\n
at /home/hl-root/fabric-samples/fullgear-4node-setup/app/node_modules/grpc/src/client_interceptors.js:1123:18"
Transaction submitted
My code looks like:我的代码看起来像:
while {
try {
const gateway = new Gateway();
await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });
const network = await gateway.getNetwork('mychannel');
const contract = network.getContract('chaincode');
await contract.submitTransaction('createTransaction', 'Transaction3', '1');
console.log('Transaction submitted');
}
catch (error) {
console.error(`Failed to submit transaction: ${error}`);
process.exit(1);
}
}
I get the error but the instruction is made and i get the date (so catch error never work?)我得到了错误,但指令已经发出,我得到了日期(所以捕获错误永远不起作用?)
Your issue is that network.getContract returns a Promise, so what is happening here is that your code is moving passed that line before the Promise resolves, so your try/catch completes before the error is thrown, hence the exception.你的问题是 network.getContract 返回一个 Promise,所以这里发生的事情是你的代码在 Promise 解决之前移动通过那一行,所以你的 try/catch 在抛出错误之前完成,因此异常。 Try this:
尝试这个:
while {
try {
const gateway = new Gateway();
await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });
const network = await gateway.getNetwork('mychannel');
const contract = await network.getContract('chaincode');
await contract.submitTransaction('createTransaction', 'Transaction3', '1');
console.log('Transaction submitted');
}
catch (error) {
console.error(`Failed to submit transaction: ${error}`);
process.exit(1);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.