From d9fe8b8160f30af13a8731876057484ce289c171 Mon Sep 17 00:00:00 2001 From: Dietrich Date: Sat, 31 Oct 2020 11:15:29 +0100 Subject: [PATCH] improve error handling --- src/userlib/files.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/userlib/files.rs b/src/userlib/files.rs index e6d77d8..83bca9c 100644 --- a/src/userlib/files.rs +++ b/src/userlib/files.rs @@ -81,10 +81,14 @@ impl LockedFileGuard { } pub fn replace_contents(&mut self, new_content: String) -> Result<(), crate::UserLibError> { - self.file = File::create(&self.path).expect("Failed to truncate file."); - self.file - .write_all(&new_content.into_bytes()) - .expect("Failed to write all users."); + self.file = match File::create(&self.path) { + Ok(file) => file, + Err(e) => return Err(("Failed to truncate file.".to_owned(), e).into()), + }; + match self.file.write_all(&new_content.into_bytes()) { + Ok(_) => (), + Err(e) => return Err(("Could not write (all) users. ".to_owned(), e).into()), + }; let _ = self.file.write("\n".as_bytes()); Ok(()) }