Author: Manos Pitsidianakis [manos@pitsidianak.is]
Hash: e7ca77aa8a1c03beb08a8956130927a69d4fdf6a
Timestamp: Thu, 02 Nov 2023 12:51:55 +0000 (10 months ago)

+43 -19 +/-3 browse
core/config.rs: add context for I/O errors
core/config.rs: add context for I/O errors

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
1diff --git a/cli/tests/basic_interfaces.rs b/cli/tests/basic_interfaces.rs
2index 8fcdcdf..8e8a438 100644
3--- a/cli/tests/basic_interfaces.rs
4+++ b/cli/tests/basic_interfaces.rs
5 @@ -116,9 +116,10 @@ For more information, try '--help'."#,
6 output.code(255).stderr(predicates::str::is_empty()).stdout(
7 predicate::eq(
8 format!(
9- "[1] Could not read configuration file from path: {} Caused by:\n[2] Error \
10- returned from internal I/O operation: No such file or directory (os error 2)",
11- conf.display()
12+ "[1] Could not read configuration file from path: {path} Caused by:\n[2] \
13+ Configuration file {path} not found. Caused by:\n[3] Error returned from \
14+ internal I/O operation: No such file or directory (os error 2)",
15+ path = conf.display()
16 )
17 .as_str(),
18 )
19 diff --git a/core/src/config.rs b/core/src/config.rs
20index d99248f..ef2ab16 100644
21--- a/core/src/config.rs
22+++ b/core/src/config.rs
23 @@ -76,14 +76,18 @@ impl Configuration {
24 pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
25 let path = path.as_ref();
26 let mut s = String::new();
27- let mut file = std::fs::File::open(path)?;
28- file.read_to_string(&mut s)?;
29+ let mut file = std::fs::File::open(path)
30+ .with_context(|| format!("Configuration file {} not found.", path.display()))?;
31+ file.read_to_string(&mut s)
32+ .with_context(|| format!("Could not read from file {}.", path.display()))?;
33 let config: Self = toml::from_str(&s)
34 .map_err(anyhow::Error::from)
35- .context(format!(
36- "Could not parse configuration file `{}` successfully: ",
37- path.display()
38- ))?;
39+ .with_context(|| {
40+ format!(
41+ "Could not parse configuration file `{}` successfully: ",
42+ path.display()
43+ )
44+ })?;
45
46 Ok(config)
47 }
48 @@ -106,14 +110,20 @@ impl Configuration {
49 }
50
51 debug_assert!(path != self.db_path());
52- let mut file = std::fs::File::create(&path)?;
53- let metadata = file.metadata()?;
54+ let mut file = std::fs::File::create(&path)
55+ .with_context(|| format!("Could not create file {}.", path.display()))?;
56+ let metadata = file
57+ .metadata()
58+ .with_context(|| format!("Could not fstat file {}.", path.display()))?;
59 let mut permissions = metadata.permissions();
60
61 permissions.set_mode(0o600); // Read/write for owner only.
62- file.set_permissions(permissions)?;
63- file.write_all(msg.as_bytes())?;
64- file.flush()?;
65+ file.set_permissions(permissions)
66+ .with_context(|| format!("Could not chmod 600 file {}.", path.display()))?;
67+ file.write_all(msg.as_bytes())
68+ .with_context(|| format!("Could not write message to file {}.", path.display()))?;
69+ file.flush()
70+ .with_context(|| format!("Could not flush message I/O to file {}.", path.display()))?;
71 Ok(path)
72 }
73
74 diff --git a/core/src/connection.rs b/core/src/connection.rs
75index df1b7d8..769a814 100644
76--- a/core/src/connection.rs
77+++ b/core/src/connection.rs
78 @@ -187,7 +187,9 @@ impl Connection {
79 INIT_SQLITE_LOGGING.call_once(|| {
80 _ = unsafe { rusqlite::trace::config_log(Some(log_callback)) };
81 });
82- let conn = DbConnection::open(conf.db_path.to_str().unwrap())?;
83+ let conn = DbConnection::open(conf.db_path.to_str().unwrap()).with_context(|| {
84+ format!("sqlite3 library could not open {}.", conf.db_path.display())
85+ })?;
86 rusqlite::vtab::array::load_module(&conn)?;
87 conn.pragma_update(None, "journal_mode", "WAL")?;
88 conn.pragma_update(None, "foreign_keys", "on")?;
89 @@ -346,7 +348,14 @@ impl Connection {
90 .stdin(Stdio::piped())
91 .stdout(Stdio::piped())
92 .stderr(Stdio::piped())
93- .spawn()?;
94+ .spawn()
95+ .with_context(|| {
96+ format!(
97+ "Could not launch {} {}.",
98+ std::env::var("SQLITE_BIN").unwrap_or_else(|_| "sqlite3".into()),
99+ db_path.display()
100+ )
101+ })?;
102 let mut stdin = child.stdin.take().unwrap();
103 std::thread::spawn(move || {
104 stdin
105 @@ -381,12 +390,16 @@ impl Connection {
106 .into());
107 }
108
109- let file = std::fs::File::open(db_path)?;
110- let metadata = file.metadata()?;
111+ let file = std::fs::File::open(db_path)
112+ .with_context(|| format!("Could not open database {}.", db_path.display()))?;
113+ let metadata = file
114+ .metadata()
115+ .with_context(|| format!("Could not fstat database {}.", db_path.display()))?;
116 let mut permissions = metadata.permissions();
117
118 permissions.set_mode(0o600); // Read/write for owner only.
119- file.set_permissions(permissions)?;
120+ file.set_permissions(permissions)
121+ .with_context(|| format!("Could not chmod 600 database {}.", db_path.display()))?;
122 }
123 Self::open_db(conf)
124 }