Skip to content

Commit 27051ad

Browse files
authored
chore(avm-transpiler): better error messages (#7217)
1 parent b4f50a5 commit 27051ad

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

avm-transpiler/src/main.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,36 @@ fn main() {
2020
let args: Vec<String> = env::args().collect();
2121
let in_contract_artifact_path = &args[1];
2222
let out_transpiled_artifact_path = &args[2];
23+
let json_parse_error = format!(
24+
"Unable to parse json for: {in_contract_artifact_path}
25+
This is probably a stale json file with a different wire format.
26+
You might need to recompile the contract or delete the json file"
27+
);
2328

2429
// Parse original (pre-transpile) contract.
25-
let contract_json =
26-
fs::read_to_string(Path::new(in_contract_artifact_path)).expect("Unable to read file");
30+
let contract_json = fs::read_to_string(Path::new(in_contract_artifact_path))
31+
.expect(&format!("Unable to read file: {in_contract_artifact_path}"));
2732
let raw_json_obj: serde_json::Value =
28-
serde_json::from_str(&contract_json).expect("Unable to parse json");
33+
serde_json::from_str(&contract_json).expect(&json_parse_error);
2934

3035
// Skip if contract has "transpiled: true" flag!
3136
if let Some(serde_json::Value::Bool(true)) = raw_json_obj.get("transpiled") {
3237
warn!("Contract already transpiled. Skipping.");
3338
return;
3439
}
3540

36-
// Backup the original file.
37-
std::fs::copy(
38-
Path::new(in_contract_artifact_path),
39-
Path::new(&(in_contract_artifact_path.clone() + ".bak")),
40-
)
41-
.expect("Unable to backup file");
41+
// Backup the output file if it already exists.
42+
if Path::new(out_transpiled_artifact_path).exists() {
43+
std::fs::copy(
44+
Path::new(out_transpiled_artifact_path),
45+
Path::new(&(out_transpiled_artifact_path.clone() + ".bak")),
46+
)
47+
.expect(&format!("Unable to backup file: {out_transpiled_artifact_path}"));
48+
}
4249

4350
// Parse json into contract object
4451
let contract: CompiledAcirContractArtifact =
45-
serde_json::from_str(&contract_json).expect("Unable to parse json");
52+
serde_json::from_str(&contract_json).expect(&json_parse_error);
4653

4754
// Transpile contract to AVM bytecode
4855
let transpiled_contract = TranspiledContractArtifact::from(contract);

0 commit comments

Comments
 (0)