Skip to content

Commit 4584a6c

Browse files
committed
Throw when unable to parse jsons
1 parent 30b8e1c commit 4584a6c

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

app/components/verification/ContractIdentifier.tsx

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,21 @@ export default function ContractIdentifier({
7575
const jsonContent = await jsonFile.text();
7676
const stdJson = JSON.parse(jsonContent);
7777

78-
if (stdJson.sources) {
79-
for (const [filePath, sourceInfo] of Object.entries(stdJson.sources)) {
80-
if (sourceInfo && typeof sourceInfo === "object" && "content" in sourceInfo) {
81-
const sourceContent = (sourceInfo as any).content;
82-
if (typeof sourceContent === "string") {
83-
if (selectedLanguage === "solidity") {
84-
const fileContracts = await parseFileContent(filePath, sourceContent);
85-
contracts.push(...fileContracts);
86-
} else if (selectedLanguage === "vyper" && filePath.endsWith(".vy")) {
87-
// For Vyper, generate contract identifier from file path
88-
const contractName = filePath.split("/").pop()?.replace(".vy", "") || "";
89-
if (contractName) {
90-
contracts.push({
91-
fileName: filePath,
92-
contractName,
93-
fullIdentifier: `${filePath}:${contractName}`,
94-
});
95-
}
96-
}
78+
for (const [filePath, sourceInfo] of Object.entries(stdJson.sources)) {
79+
if (sourceInfo && typeof sourceInfo === "object" && "content" in sourceInfo) {
80+
const sourceContent = (sourceInfo as any).content;
81+
if (selectedLanguage === "solidity") {
82+
const fileContracts = await parseFileContent(filePath, sourceContent);
83+
contracts.push(...fileContracts);
84+
} else if (selectedLanguage === "vyper" && filePath.endsWith(".vy")) {
85+
// For Vyper, generate contract identifier from file path
86+
const contractName = filePath.split("/").pop()?.replace(".vy", "") || "";
87+
if (contractName) {
88+
contracts.push({
89+
fileName: filePath,
90+
contractName,
91+
fullIdentifier: `${filePath}:${contractName}`,
92+
});
9793
}
9894
}
9995
}
@@ -167,7 +163,7 @@ export default function ContractIdentifier({
167163
}
168164
}, [isDropdownOpen]);
169165

170-
const parseFileContent = async (fileName: string, content: string): Promise<ParsedContract[]> => {
166+
const parseFileContent = async (fileName: string | null, content: string): Promise<ParsedContract[]> => {
171167
try {
172168
const ast = parse(content, {
173169
loc: false,
@@ -180,6 +176,11 @@ export default function ContractIdentifier({
180176
if (ast.children) {
181177
for (const child of ast.children) {
182178
if (child.type === "ContractDefinition" && child.name) {
179+
if (!fileName) {
180+
const extension = selectedLanguage === "solidity" ? ".sol" : ".vy";
181+
fileName = `${child.name}${extension}`;
182+
}
183+
183184
contracts.push({
184185
fileName,
185186
contractName: child.name,
@@ -191,8 +192,7 @@ export default function ContractIdentifier({
191192

192193
return contracts;
193194
} catch (error) {
194-
console.warn(`Failed to parse ${fileName}:`, error);
195-
return [];
195+
throw new Error(`Failed to parse ${fileName}: ${error}`);
196196
}
197197
};
198198

@@ -298,12 +298,9 @@ export default function ContractIdentifier({
298298
>
299299
<span>
300300
{contractIdentifier ? (
301-
<>
302-
{contractIdentifier.substring(0, contractIdentifier.lastIndexOf(":"))}:
303-
<span className="font-bold">
304-
{contractIdentifier.substring(contractIdentifier.lastIndexOf(":") + 1)}
305-
</span>
306-
</>
301+
<span className="font-bold">
302+
{contractIdentifier.substring(contractIdentifier.lastIndexOf(":") + 1)}
303+
</span>
307304
) : (
308305
<span className="text-gray-500">Select a contract...</span>
309306
)}
@@ -338,7 +335,7 @@ export default function ContractIdentifier({
338335
}}
339336
className="w-full px-3 py-2 text-left hover:bg-gray-50 focus:bg-gray-50 focus:outline-none font-mono text-sm"
340337
>
341-
{contract.fileName}:<span className="font-bold">{contract.contractName}</span>
338+
<span className="font-bold">{contract.contractName}</span>
342339
</button>
343340
))
344341
) : (

0 commit comments

Comments
 (0)