Merge pull request #47 from 67P/features/initialize-error-handling

Fail smarter with better error handling
This commit was merged in pull request #47.
This commit is contained in:
2018-04-15 18:14:12 +00:00
committed by GitHub
3 changed files with 37 additions and 11 deletions
+26 -1
View File
@@ -27,11 +27,19 @@ export default class Kredits {
this.ipfsConfig = ipfsConfig; this.ipfsConfig = ipfsConfig;
this.ipfs = new IPFS(ipfsConfig); this.ipfs = new IPFS(ipfsConfig);
return this.ipfs._ipfsAPI.id().catch((error) => {
throw new Error(`IPFS node not available; config: ${JSON.stringify(ipfsConfig)} - ${error.message}`);
}).then(() => {
let registryContract = this.initRegistryContract(provider); let registryContract = this.initRegistryContract(provider);
let addresses = Object.keys(contracts).reduce((mem, name) => { let addresses = Object.keys(contracts).reduce((mem, name) => {
let contractName = capitalize(name); let contractName = capitalize(name);
mem[contractName] = registryContract.functions.getProxyFor(contractName); 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 mem;
}, {}); }, {});
@@ -39,10 +47,24 @@ export default class Kredits {
.then((addresses) => { .then((addresses) => {
return new Kredits(provider, signer, addresses); return new Kredits(provider, signer, addresses);
}); });
});
} }
static initRegistryContract(provider) { static initRegistryContract(provider) {
let address = addresses['Registry'][provider.chainId]; 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']; let abi = abis['Registry'];
console.log('Initialize registry contract:', address, abi, provider); console.log('Initialize registry contract:', address, abi, provider);
return new ethers.Contract(address, abi, provider); return new ethers.Contract(address, abi, provider);
@@ -69,6 +91,9 @@ export default class Kredits {
let contractName = capitalize(name); let contractName = capitalize(name);
let address = this.addresses[contractName]; 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); let contract = new ethers.Contract(address, abis[contractName], this.signer);
this.contracts[name] = new contracts[contractName](contract); this.contracts[name] = new contracts[contractName](contract);
-1
View File
@@ -51,5 +51,4 @@ export default class IPFS {
return multihashes.encode(digest, hashData.hashFunction, hashData.hashSize); return multihashes.encode(digest, hashData.hashFunction, hashData.hashSize);
} }
} }
+2
View File
@@ -13,6 +13,8 @@ export default Route.extend({
transition.retry(); transition.retry();
} }
} }
}).catch((error) => {
console.log('Error initializing Kredits', error);
}); });
}, },
}); });