From 461d3a51db03d02365e1dcd8abf0aca2594a0749 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sat, 6 May 2023 19:50:28 -0700 Subject: [PATCH] Add error handling and descriptive logs --- index.html | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index 6bd9126..b920de3 100644 --- a/index.html +++ b/index.html @@ -128,27 +128,50 @@ const ipfs = new URL(data.get('ipfs')); const result = e.target.elements['result']; - const bytecode = await fetchBytecode(address, rpc); + // Reset the output, if there were previous runs + result.textContent = ''; + result.style.setProperty('visibility', 'visible'); + + // Retrieve smart contract runtime bytecode + let bytecode; + try { + bytecode = await fetchBytecode(address, rpc); + if (bytecode === undefined) { // RPC call succeeded, but address has no bytecode + result.textContent += `❌ Failed to fetch runtime bytecode for: ${address}`; + return; + } + } + catch { // RPC call failed + result.textContent += `❌ Failed to connect to: ${rpc}`; + return; + } + result.textContent += '✅ Successfully fetched runtime bytecode.\n'; + + // Parse runtime bytecode const cbor = extractCBOR(bytecode); const metadata = decodeCBOR(cbor); - let output; + if (metadata.bzzr0 !== undefined) { const hex = [...metadata.bzzr0].map(b => b.toString(16).padStart(2, '0')).join(''); - output = 'Unable to fetch metadata because the smart contract uses a Swarm hash:\n'; - output += `bzzr://${hex}`; + result.textContent += 'Unable to fetch JSON metadata because the smart contract uses a Swarm hash:\n'; + result.textContent += `bzzr://${hex}`; } else if (metadata.bzzr1 !== undefined) { const hex = [...metadata.bzzr1].map(b => b.toString(16).padStart(2, '0')).join(''); - output = 'Unable to fetch metadata because the smart contract uses a Swarm hash:\n'; - output += `bzz-raw://${hex}`; + result.textContent += 'Unable to fetch JSON metadata because the smart contract uses a Swarm hash:\n'; + result.textContent += `bzz-raw://${hex}`; } else { const { cidV0, cidV1 } = calculateCID(metadata); - output = JSON.stringify(await fetchJSONMetadata(cidV1, ipfs), null, 2); + try { + const json = await fetchJSONMetadata(cidV1, ipfs); + result.textContent += '✅ Successfully fetched JSON metadata.\n\n'; + result.textContent += JSON.stringify(json, null, 2); + } + catch { + result.textContent += `❌ Failed to fetch JSON metadata from IPFS CIDv1: ${cidV1}`; + } } - - result.textContent = output; - result.style.setProperty('visibility', 'visible'); }) -- 2.47.3