Error cleanup

This commit is contained in:
Antoine Gersant 2022-11-21 16:45:18 -08:00
parent 4c5a6bc2d6
commit fee2f17fb1
3 changed files with 33 additions and 8 deletions

View file

@ -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<Self> {
pub fn new(port: u16, paths: Paths) -> Result<Self, Error> {
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());

View file

@ -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<Config> {
let mut config_file = std::fs::File::open(path)?;
pub fn from_path(path: &Path) -> Result<Config, Error> {
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::<Self>(&config_file_content)?;
Ok(config)
}

View file

@ -48,7 +48,9 @@ impl From<config::Error> 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(),
}