Closed annaloro closed 6 months ago
An addition to this issue: I think it would be useful if Manta would have a pre-check phase at startup time where all the required files and components are checked. This could be useful to print better error messages when the certificates or the CA are not present, or if the socks proxy is unreachable.
It'd be nice if it would also provide some defaults to be used as examples, printing the actual location of the files on the different platforms used (Linux or Mac, so far).
Hi @annaloro ,
the configuration file could be found here https://github.com/eth-cscs/manta?tab=readme-ov-file#configuration
thanks
this is a typical example of Result/Error management (https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html)
The error is here https://github.com/eth-cscs/manta/blob/main/src/common/config_ops.rs#L31
The build
function (https://docs.rs/config/latest/config/builder/struct.ConfigBuilder.html#method.build) is returning a Result<Config, ConfigError>
.
The problem is, if manta can't find the config file, then, the build
function will return a Result
containing a ConfigError
, therefore unwrap
will fail because unwrap
assumes build
was successful and expects a Config
therefore the code panics.
The best way to solve this is by using pattern matching to evaluate what Result
contains, if it is a successful value, then process as usual, otherwise (eg config file is missing) exit the program with status code 1
and show an error message. An example can be done as follows:
let config_rslt = ::config::Config::builder()
.add_source(::config::File::from(config_path))
.add_source(
::config::Environment::with_prefix("MANTA")
.try_parsing(true)
.prefix_separator("_"),
)
.build();
match config_rslt {
Ok(config) => config,
Err(error) => {
eprintln!("Error processing config.toml file. Reason:\n{}", error);
std::process::exit(1);
}
}
In this example, we save the Result
from build
function in a variable called config_rslt
and inspect its content using pattern matching, if the Result is successful (Ok) then we just return the value, if it is an error, then, the error is sent to stderr
and exit code 1
Here is the error just after finishing the installation :
(base) $USER@$HOSTNAME:~/GIT$ manta --version thread 'main' panicked at src/common/config_ops.rs:31:10: called
Result::unwrap()
on anErr
value: configuration file "/home/$USER.config/manta/config.toml" not found note: run withRUST_BACKTRACE=1
environment variable to display a backtrace (base) $USER@$HOSTNAME:~/GIT$Could you provide a default config.toml file ?
Thanks, Kind Regards, Vincenzo