From e1f19a1832120a8f65d4deb74b122e295fa47f37 Mon Sep 17 00:00:00 2001 From: bumi Date: Sun, 15 Apr 2018 16:45:09 +0200 Subject: [PATCH 1/2] Fail smarter with better error handling This should give more insights in case of an error during loading data from Ethereum and IPFS. --- app/lib/kredits/kredits.js | 45 +++++++++++++++++++++++++++-------- app/lib/kredits/utils/ipfs.js | 1 - app/routes/application.js | 2 ++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/app/lib/kredits/kredits.js b/app/lib/kredits/kredits.js index b087371..7001a9a 100644 --- a/app/lib/kredits/kredits.js +++ b/app/lib/kredits/kredits.js @@ -27,22 +27,44 @@ export default class Kredits { this.ipfsConfig = ipfsConfig; this.ipfs = new IPFS(ipfsConfig); - let registryContract = this.initRegistryContract(provider); + return this.ipfs._ipfsAPI.id().catch((error) => { + throw new Error(`IPFS node not available; config: ${JSON.stringify(ipfsConfig)} - ${error.message}`); + }).then(() => { - let addresses = Object.keys(contracts).reduce((mem, name) => { - let contractName = capitalize(name); - mem[contractName] = registryContract.functions.getProxyFor(contractName); - return mem; - }, {}); + let registryContract = this.initRegistryContract(provider); - return RSVP.hash(addresses) - .then((addresses) => { - return new Kredits(provider, signer, addresses); - }); + let addresses = Object.keys(contracts).reduce((mem, name) => { + let contractName = capitalize(name); + mem[contractName] = registryContract.functions.getProxyFor(contractName).catch((error) => { + throw new Error(`Failed to get address for ${contractName} from registry at ${registryContract.address} + - correct registry? does it have version entry? - ${error.message}` + ); + }); + return mem; + }, {}); + + return RSVP.hash(addresses) + .then((addresses) => { + return new Kredits(provider, signer, addresses); + }); + }); } static initRegistryContract(provider) { let address = addresses['Registry'][provider.chainId]; + if (!address) { + throw new Error(`Registry address not found; invalid network? + requested network: ${provider.chainId} + supported networks: ${Object.keys(addresses['Registry'])} + `); + } + provider.getCode(address).then((code) => { + // not sure if we always get the same return value of the code is not available + // that's why checking if it is < 5 long + if (code === '0x00' || code.length < 5) { + throw new Error(`Registry not found at ${address} on network ${provider.chainId}`); + } + }); let abi = abis['Registry']; console.log('Initialize registry contract:', address, abi, provider); return new ethers.Contract(address, abi, provider); @@ -69,6 +91,9 @@ export default class Kredits { let contractName = capitalize(name); let address = this.addresses[contractName]; + if (!address || !abis[contractName]) { + throw new Error('Address or ABI not found for ' + contractName); + } let contract = new ethers.Contract(address, abis[contractName], this.signer); this.contracts[name] = new contracts[contractName](contract); diff --git a/app/lib/kredits/utils/ipfs.js b/app/lib/kredits/utils/ipfs.js index 5a2a7e4..bc6ccda 100644 --- a/app/lib/kredits/utils/ipfs.js +++ b/app/lib/kredits/utils/ipfs.js @@ -51,5 +51,4 @@ export default class IPFS { return multihashes.encode(digest, hashData.hashFunction, hashData.hashSize); } - } diff --git a/app/routes/application.js b/app/routes/application.js index 9371c64..602cdbd 100644 --- a/app/routes/application.js +++ b/app/routes/application.js @@ -13,6 +13,8 @@ export default Route.extend({ transition.retry(); } } + }).catch((error) => { + console.log('Error initializing Kredits', error); }); }, }); From 7e6c6a037d3e0ce73034cc56ee1c8e5d735ee6f5 Mon Sep 17 00:00:00 2001 From: bumi Date: Sun, 15 Apr 2018 18:02:52 +0200 Subject: [PATCH 2/2] code style. use interpolation syntax --- app/lib/kredits/kredits.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/kredits/kredits.js b/app/lib/kredits/kredits.js index 7001a9a..46a893f 100644 --- a/app/lib/kredits/kredits.js +++ b/app/lib/kredits/kredits.js @@ -92,7 +92,7 @@ export default class Kredits { let contractName = capitalize(name); let address = this.addresses[contractName]; if (!address || !abis[contractName]) { - throw new Error('Address or ABI not found for ' + contractName); + throw new Error(`Address or ABI not found for ${contractName}`); } let contract = new ethers.Contract(address, abis[contractName], this.signer); this.contracts[name] = new contracts[contractName](contract);