diff --git a/src/app.rs b/src/app.rs index 3a1da3e..93b9da4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,7 @@ use std::fs; use std::path::PathBuf; -use crate::db::DB; +use crate::db::{self, DB}; use crate::paths::Paths; pub mod config; @@ -17,6 +17,18 @@ pub mod vfs; #[cfg(test)] pub mod test; +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error(transparent)] + Config(#[from] config::Error), + #[error(transparent)] + Database(#[from] db::Error), + #[error("Filesystem error for `{0}`: `{1}`")] + Io(PathBuf, std::io::Error), + #[error(transparent)] + Settings(#[from] settings::Error), +} + #[derive(Clone)] pub struct App { pub port: u16, @@ -36,12 +48,16 @@ pub struct App { } impl App { - pub fn new(port: u16, paths: Paths) -> anyhow::Result { + pub fn new(port: u16, paths: Paths) -> Result { let db = DB::new(&paths.db_file_path)?; - fs::create_dir_all(&paths.web_dir_path)?; - fs::create_dir_all(&paths.swagger_dir_path)?; + fs::create_dir_all(&paths.web_dir_path) + .map_err(|e| Error::Io(paths.web_dir_path.clone(), e))?; + fs::create_dir_all(&paths.swagger_dir_path) + .map_err(|e| Error::Io(paths.swagger_dir_path.clone(), e))?; let thumbnails_dir_path = paths.cache_dir_path.join("thumbnails"); + fs::create_dir_all(&thumbnails_dir_path) + .map_err(|e| Error::Io(thumbnails_dir_path.clone(), e))?; let vfs_manager = vfs::Manager::new(db.clone()); let settings_manager = settings::Manager::new(db.clone()); diff --git a/src/app/config.rs b/src/app/config.rs index f50a52d..c20c608 100644 --- a/src/app/config.rs +++ b/src/app/config.rs @@ -1,6 +1,6 @@ use serde::Deserialize; use std::io::Read; -use std::path; +use std::path::{Path, PathBuf}; use crate::app::{ddns, settings, user, vfs}; @@ -8,9 +8,13 @@ use crate::app::{ddns, settings, user, vfs}; pub enum Error { #[error(transparent)] Ddns(#[from] ddns::Error), + #[error("Filesystem error for `{0}`: `{1}`")] + Io(PathBuf, std::io::Error), #[error(transparent)] Settings(#[from] settings::Error), #[error(transparent)] + Toml(#[from] toml::de::Error), + #[error(transparent)] User(#[from] user::Error), #[error(transparent)] Vfs(#[from] vfs::Error), @@ -25,10 +29,13 @@ pub struct Config { } impl Config { - pub fn from_path(path: &path::Path) -> anyhow::Result { - let mut config_file = std::fs::File::open(path)?; + pub fn from_path(path: &Path) -> Result { + let mut config_file = + std::fs::File::open(path).map_err(|e| Error::Io(path.to_owned(), e))?; let mut config_file_content = String::new(); - config_file.read_to_string(&mut config_file_content)?; + config_file + .read_to_string(&mut config_file_content) + .map_err(|e| Error::Io(path.to_owned(), e))?; let config = toml::de::from_str::(&config_file_content)?; Ok(config) } diff --git a/src/service/error.rs b/src/service/error.rs index e5a62c5..ad4c406 100644 --- a/src/service/error.rs +++ b/src/service/error.rs @@ -48,7 +48,9 @@ impl From for APIError { fn from(error: config::Error) -> APIError { match error { config::Error::Ddns(e) => e.into(), + config::Error::Io(_, _) => APIError::Internal, config::Error::Settings(e) => e.into(), + config::Error::Toml(_) => APIError::Internal, config::Error::User(e) => e.into(), config::Error::Vfs(e) => e.into(), }