Skip to content

Commit 5c06274

Browse files
committed
[Aptos Node] Improve error messages for config file loading.
1 parent be33166 commit 5c06274

File tree

4 files changed

+88
-23
lines changed

4 files changed

+88
-23
lines changed

aptos-node/src/lib.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,26 @@ impl AptosNodeArgs {
133133
)
134134
.expect("Test mode should start correctly");
135135
} else {
136-
// Load the config file
136+
// Get the config file path
137137
let config_path = self.config.expect("Config is required to launch node");
138+
if !config_path.exists() {
139+
panic!(
140+
"The node config file could not be found! Ensure the given path is correct: {:?}",
141+
config_path.display()
142+
)
143+
}
144+
145+
// A config file exists, attempt to parse the config
138146
let config = NodeConfig::load(config_path.clone()).unwrap_or_else(|error| {
139147
panic!(
140-
"Failed to load node config file! Given file path: {:?}. Error: {:?}",
141-
config_path, error
148+
"Failed to parse node config file! Given file path: {:?}. Error: {:?}",
149+
config_path.display(),
150+
error
142151
)
143152
});
144-
println!("Using node config {:?}", &config);
145153

146154
// Start the node
155+
println!("Using node config {:?}", &config);
147156
start(config, None).expect("Node should start correctly");
148157
};
149158
}

config/src/config/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub enum Error {
1515
Yaml(String, #[source] serde_yaml::Error),
1616
#[error("Config is missing expected value: {0}")]
1717
Missing(&'static str),
18+
#[error("Unexpected error: {0}")]
19+
Unexpected(String),
1820
}
1921

2022
pub fn invariant(cond: bool, msg: String) -> Result<(), Error> {

config/src/config/execution_config.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,41 @@ impl Default for ExecutionConfig {
5656
impl ExecutionConfig {
5757
pub fn load(&mut self, root_dir: &RootPath) -> Result<(), Error> {
5858
if !self.genesis_file_location.as_os_str().is_empty() {
59-
let path = root_dir.full_path(&self.genesis_file_location);
60-
let mut file = File::open(&path).map_err(|e| Error::IO("genesis".into(), e))?;
59+
// Ensure the genesis file exists
60+
let genesis_path = root_dir.full_path(&self.genesis_file_location);
61+
if !genesis_path.exists() {
62+
return Err(Error::Unexpected(format!(
63+
"The genesis file could not be found! Ensure the given path is correct: {:?}",
64+
genesis_path.display()
65+
)));
66+
}
67+
68+
// Open the genesis file and read the bytes
69+
let mut file = File::open(&genesis_path).map_err(|error| {
70+
Error::Unexpected(format!(
71+
"Failed to open the genesis file: {:?}. Error: {:?}",
72+
genesis_path.display(),
73+
error
74+
))
75+
})?;
6176
let mut buffer = vec![];
62-
file.read_to_end(&mut buffer)
63-
.map_err(|e| Error::IO("genesis".into(), e))?;
64-
let data = bcs::from_bytes(&buffer).map_err(|e| Error::BCS("genesis", e))?;
65-
self.genesis = Some(data);
77+
file.read_to_end(&mut buffer).map_err(|error| {
78+
Error::Unexpected(format!(
79+
"Failed to read the genesis file into a buffer: {:?}. Error: {:?}",
80+
genesis_path.display(),
81+
error
82+
))
83+
})?;
84+
85+
// Deserialize the genesis file and store it
86+
let genesis = bcs::from_bytes(&buffer).map_err(|error| {
87+
Error::Unexpected(format!(
88+
"Failed to BCS deserialize the genesis file: {:?}. Error: {:?}",
89+
genesis_path.display(),
90+
error
91+
))
92+
})?;
93+
self.genesis = Some(genesis);
6694
}
6795

6896
Ok(())

config/src/config/mod.rs

+39-13
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,27 @@ impl WaypointConfig {
132132
pub fn waypoint(&self) -> Waypoint {
133133
let waypoint = match &self {
134134
WaypointConfig::FromConfig(waypoint) => Some(*waypoint),
135-
WaypointConfig::FromFile(path) => {
136-
let content = fs::read_to_string(path)
137-
.unwrap_or_else(|_| panic!("Failed to read waypoint file {}", path.display()));
138-
Some(
139-
Waypoint::from_str(content.trim())
140-
.unwrap_or_else(|_| panic!("Failed to parse waypoint: {}", content.trim())),
141-
)
135+
WaypointConfig::FromFile(waypoint_path) => {
136+
if !waypoint_path.exists() {
137+
panic!(
138+
"Waypoint file not found! Ensure the given path is correct: {:?}",
139+
waypoint_path.display()
140+
);
141+
}
142+
let content = fs::read_to_string(waypoint_path).unwrap_or_else(|error| {
143+
panic!(
144+
"Failed to read waypoint file {:?}. Error: {:?}",
145+
waypoint_path.display(),
146+
error
147+
)
148+
});
149+
Some(Waypoint::from_str(content.trim()).unwrap_or_else(|error| {
150+
panic!(
151+
"Failed to parse waypoint: {:?}. Error: {:?}",
152+
content.trim(),
153+
error
154+
)
155+
}))
142156
}
143157
WaypointConfig::FromStorage(backend) => {
144158
let storage: Storage = backend.into();
@@ -255,8 +269,8 @@ impl NodeConfig {
255269
}
256270

257271
/// Reads the config file and returns the configuration object in addition to doing some
258-
/// post-processing of the config
259-
/// Paths used in the config are either absolute or relative to the config location
272+
/// post-processing of the config.
273+
/// Paths used in the config are either absolute or relative to the config location.
260274
pub fn load<P: AsRef<Path>>(input_path: P) -> Result<Self, Error> {
261275
let mut config = Self::load_config(&input_path)?;
262276

@@ -420,11 +434,23 @@ impl NodeConfig {
420434

421435
pub trait PersistableConfig: Serialize + DeserializeOwned {
422436
fn load_config<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
423-
let mut file = File::open(&path)
424-
.map_err(|e| Error::IO(path.as_ref().to_str().unwrap().to_string(), e))?;
437+
// Open the file and read it into a string
438+
let config_path_string = path.as_ref().to_str().unwrap().to_string();
439+
let mut file = File::open(&path).map_err(|error| {
440+
Error::Unexpected(format!(
441+
"Failed to open config file: {:?}. Error: {:?}",
442+
config_path_string, error
443+
))
444+
})?;
425445
let mut contents = String::new();
426-
file.read_to_string(&mut contents)
427-
.map_err(|e| Error::IO(path.as_ref().to_str().unwrap().to_string(), e))?;
446+
file.read_to_string(&mut contents).map_err(|error| {
447+
Error::Unexpected(format!(
448+
"Failed to read the config file into a string: {:?}. Error: {:?}",
449+
config_path_string, error
450+
))
451+
})?;
452+
453+
// Parse the file string
428454
Self::parse(&contents)
429455
}
430456

0 commit comments

Comments
 (0)