Skip to content

Commit d2fda94

Browse files
authored
Improve ABI file path validation (#1766)
- Error message now shows within CLI interactive session instead of error crash - Passing `--abi` will correctly pre-fill the interactive form - Catch JSON parsing exception for `EthereumABI`. Closes: #1765
1 parent 1d4217a commit d2fda94

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

.changeset/soft-scissors-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
Improve ABI file path validation

packages/cli/src/commands/init.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -623,47 +623,34 @@ async function processInitForm(
623623
type: 'input',
624624
name: 'abi',
625625
message: 'ABI file (path)',
626-
initial: initAbi,
626+
initial: initAbiPath,
627627
skip: () =>
628628
!protocolInstance.hasABIs() ||
629629
initFromExample !== undefined ||
630630
abiFromEtherscan !== undefined ||
631-
isSubstreams ||
632-
!!initAbiPath,
631+
isSubstreams,
633632
validate: async (value: string) => {
634633
if (initFromExample || abiFromEtherscan || !protocolInstance.hasABIs()) {
635634
return true;
636635
}
637636

638637
const ABI = protocolInstance.getABI();
639-
if (initAbiPath) {
640-
try {
641-
loadAbiFromFile(ABI, initAbiPath);
642-
return true;
643-
} catch (e) {
644-
this.error(e.message);
645-
}
646-
}
638+
if (initAbiPath) value = initAbiPath;
647639

648640
try {
649641
loadAbiFromFile(ABI, value);
650642
return true;
651643
} catch (e) {
652-
this.error(e.message);
644+
return e.message;
653645
}
654646
},
655647
result: async (value: string) => {
656648
if (initFromExample || abiFromEtherscan || !protocolInstance.hasABIs()) {
657649
return null;
658650
}
651+
659652
const ABI = protocolInstance.getABI();
660-
if (initAbiPath) {
661-
try {
662-
return loadAbiFromFile(ABI, initAbiPath);
663-
} catch (e) {
664-
return e.message;
665-
}
666-
}
653+
if (initAbiPath) value = initAbiPath;
667654

668655
try {
669656
return loadAbiFromFile(ABI, value);

packages/cli/src/protocols/ethereum/abi.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ export default class ABI {
141141
}
142142

143143
static load(name: string, file: string) {
144-
const data = JSON.parse(fs.readFileSync(file).toString());
144+
let data;
145+
try {
146+
data = JSON.parse(fs.readFileSync(file).toString());
147+
} catch (e) {
148+
throw Error(`Could not parse ABI: ${e}`);
149+
}
150+
145151
const abi = ABI.normalized(data);
146152

147153
if (abi === null || abi === undefined) {

0 commit comments

Comments
 (0)