Skip to content

Commit cd80fa2

Browse files
committed
Improve error messages
1 parent 2548ec5 commit cd80fa2

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

rewatch/src/build/packages.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,27 @@ fn flatten_dependencies(dependencies: Vec<Dependency>) -> Vec<Dependency> {
397397
}
398398

399399
pub fn read_package_name(package_dir: &Path) -> Result<String> {
400-
let package_json_path = package_dir.join("package.json");
400+
let mut file_name = "package.json";
401+
let package_json_path = package_dir.join(file_name);
401402

402403
let package_json_contents = if Path::exists(&package_json_path) {
403404
fs::read_to_string(&package_json_path).map_err(|e| anyhow!("Could not read package.json: {}", e))?
404405
} else {
405406
let rescript_json_path = package_dir.join("rescript.json");
406-
fs::read_to_string(&rescript_json_path).map_err(|e| anyhow!("Could not read rescript.json: {}", e))?
407+
if Path::exists(&rescript_json_path) {
408+
file_name = "rescript.json";
409+
fs::read_to_string(&rescript_json_path)
410+
.map_err(|e| anyhow!("Could not read rescript.json: {}", e))?
411+
} else {
412+
return Err(anyhow!(
413+
"There is no package.json or rescript.json file in {}",
414+
package_dir.to_string_lossy()
415+
));
416+
}
407417
};
408418

409419
let package_json: serde_json::Value = serde_json::from_str(&package_json_contents)
410-
.map_err(|e| anyhow!("Could not parse package.json: {}", e))?;
420+
.map_err(|e| anyhow!("Could not parse {}: {}", file_name, e))?;
411421

412422
package_json["name"]
413423
.as_str()

0 commit comments

Comments
 (0)