aboutsummaryrefslogtreecommitdiff
path: root/src/guilds.rs
diff options
context:
space:
mode:
authorDaniel <kingdread@gmx.de>2021-11-12 16:02:32 +0100
committerDaniel <kingdread@gmx.de>2021-11-12 16:02:32 +0100
commit57240aa00d7a8f7cd611654c44bd04cec9192133 (patch)
tree0e748f1f6b64f3787047dffc69c0f4d6a7d83aff /src/guilds.rs
parent2e2bdac00092f8fcb96283da697a7a820a8c8978 (diff)
downloadraidgrep-57240aa00d7a8f7cd611654c44bd04cec9192133.tar.gz
raidgrep-57240aa00d7a8f7cd611654c44bd04cec9192133.tar.bz2
raidgrep-57240aa00d7a8f7cd611654c44bd04cec9192133.zip
Better error handling, less .unwraps()
Some of these unwraps are fine to stay, mostly those that deal with locks - in this case, crashing the program if something goes wrong is probably fine. However, we also had a lot of other places where we panic'd on errors, even though we really shouldn't have. For example, an invalid zip file would bring down the whole scanner. In this case, we now use proper Result<>s and we log the error. Some places stay with unwrap() for now, mainly the code that is rare and obvious when it goes wrong - such as an overflow in input values. It could be made nicer, but it is not a priority for now. Some unwraps() have been changed to expect() to signal why they shouldn't fail.
Diffstat (limited to 'src/guilds.rs')
-rw-r--r--src/guilds.rs32
1 files changed, 23 insertions, 9 deletions
diff --git a/src/guilds.rs b/src/guilds.rs
index e847841..115de42 100644
--- a/src/guilds.rs
+++ b/src/guilds.rs
@@ -5,6 +5,8 @@ use std::collections::HashMap;
use std::fs::File;
use std::sync::RwLock;
+use anyhow::Result;
+use log::{error, warn};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
@@ -51,19 +53,31 @@ pub fn lookup(api_id: &str) -> Option<Guild> {
/// Loads the cache from the file system.
pub fn prepare_cache() {
let path = paths::cache_path();
- if !path.is_file() {
- return;
- }
+ if let Some(path) = path {
+ if !path.is_file() {
+ return;
+ }
- let file = File::open(path).expect("Unable to read cache file");
- let cache = serde_json::from_reader(file).expect("Cache file has invalid format");
- let mut target = CACHE.write().unwrap();
- *target = cache;
+ let file = File::open(path).expect("Unable to read cache file");
+ let cache = serde_json::from_reader(file).expect("Cache file has invalid format");
+ let mut target = CACHE.write().unwrap();
+ *target = cache;
+ } else {
+ warn!("Could not determine the cache path, the persistent guild cache is disabled");
+ }
}
/// Saves the cache to the file system
pub fn save_cache() {
let path = paths::cache_path();
- let file = File::create(path).expect("Cannot open cache for writing");
- serde_json::to_writer(file, &*CACHE.read().unwrap()).unwrap();
+ // We already warned about that earlier
+ if let Some(path) = path {
+ let result: Result<()> = try {
+ let file = File::create(path)?;
+ serde_json::to_writer(file, &*CACHE.read().unwrap())?;
+ };
+ if let Err(e) = result {
+ error!("Error saving the cache: {}", e);
+ }
+ }
}