Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-cli-validation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@satoshai/abi-cli": patch
---

Add CLI validation for --output/--stdout with multiple contracts and improve fetch error messages
12 changes: 12 additions & 0 deletions src/commands/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ export const fetchCommand = defineCommand({

const contractIds = args.contract.split(',').map((s) => s.trim());

if (args.output && contractIds.length > 1) {
throw new Error(
'--output cannot be used with multiple contracts. Omit --output to write separate files, or use --stdout.',
);
}

if (args.stdout && contractIds.length > 1) {
throw new Error(
'--stdout cannot be used with multiple contracts. Fetch one contract at a time with --stdout.',
);
}

for (const contractId of contractIds) {
const { address, name } = parseContractId(contractId);

Expand Down
19 changes: 17 additions & 2 deletions src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ export async function fetchContractAbi(
const baseUrl = resolveNetwork(network);
const url = `${baseUrl}/v2/contracts/interface/${address}/${name}`;

const response = await fetch(url);
let response: Response;
try {
response = await fetch(url);
} catch (cause) {
throw new Error(
`Network error fetching ABI for ${address}.${name} on ${network}`,
{ cause },
);
}

if (!response.ok) {
if (response.status === 404) {
Expand All @@ -44,5 +52,12 @@ export async function fetchContractAbi(
);
}

return (await response.json()) as ClarityAbi;
try {
return (await response.json()) as ClarityAbi;
} catch (cause) {
throw new Error(
`Invalid JSON response for ${address}.${name} on ${network}`,
{ cause },
);
}
}