]> git.ardis.lu - solc-metadata/commitdiff
Add error handling and descriptive logs
authorArdis Lu <ardisglu@gmail.com>
Sun, 7 May 2023 02:50:28 +0000 (19:50 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sun, 7 May 2023 02:50:28 +0000 (19:50 -0700)
index.html

index 6bd9126e4b3f574c3aa30d335a19dcfde45eb4bb..b920de39d12109f0121f2c84d07e4715167bc00a 100644 (file)
       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');
     })
   </script>
 </body>
This page took 0.045531 seconds and 4 git commands to generate.