mirror of
https://github.com/agersant/polaris
synced 2024-11-10 02:04:13 +00:00
Error cleanup
This commit is contained in:
parent
4c5a6bc2d6
commit
fee2f17fb1
3 changed files with 33 additions and 8 deletions
24
src/app.rs
24
src/app.rs
|
@ -1,7 +1,7 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::db::DB;
|
use crate::db::{self, DB};
|
||||||
use crate::paths::Paths;
|
use crate::paths::Paths;
|
||||||
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
@ -17,6 +17,18 @@ pub mod vfs;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod 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)]
|
#[derive(Clone)]
|
||||||
pub struct App {
|
pub struct App {
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
|
@ -36,12 +48,16 @@ pub struct App {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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)?;
|
let db = DB::new(&paths.db_file_path)?;
|
||||||
fs::create_dir_all(&paths.web_dir_path)?;
|
fs::create_dir_all(&paths.web_dir_path)
|
||||||
fs::create_dir_all(&paths.swagger_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");
|
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 vfs_manager = vfs::Manager::new(db.clone());
|
||||||
let settings_manager = settings::Manager::new(db.clone());
|
let settings_manager = settings::Manager::new(db.clone());
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path;
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use crate::app::{ddns, settings, user, vfs};
|
use crate::app::{ddns, settings, user, vfs};
|
||||||
|
|
||||||
|
@ -8,9 +8,13 @@ use crate::app::{ddns, settings, user, vfs};
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Ddns(#[from] ddns::Error),
|
Ddns(#[from] ddns::Error),
|
||||||
|
#[error("Filesystem error for `{0}`: `{1}`")]
|
||||||
|
Io(PathBuf, std::io::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Settings(#[from] settings::Error),
|
Settings(#[from] settings::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
|
Toml(#[from] toml::de::Error),
|
||||||
|
#[error(transparent)]
|
||||||
User(#[from] user::Error),
|
User(#[from] user::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Vfs(#[from] vfs::Error),
|
Vfs(#[from] vfs::Error),
|
||||||
|
@ -25,10 +29,13 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn from_path(path: &path::Path) -> anyhow::Result<Config> {
|
pub fn from_path(path: &Path) -> Result<Config, Error> {
|
||||||
let mut config_file = std::fs::File::open(path)?;
|
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();
|
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)?;
|
let config = toml::de::from_str::<Self>(&config_file_content)?;
|
||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,9 @@ impl From<config::Error> for APIError {
|
||||||
fn from(error: config::Error) -> APIError {
|
fn from(error: config::Error) -> APIError {
|
||||||
match error {
|
match error {
|
||||||
config::Error::Ddns(e) => e.into(),
|
config::Error::Ddns(e) => e.into(),
|
||||||
|
config::Error::Io(_, _) => APIError::Internal,
|
||||||
config::Error::Settings(e) => e.into(),
|
config::Error::Settings(e) => e.into(),
|
||||||
|
config::Error::Toml(_) => APIError::Internal,
|
||||||
config::Error::User(e) => e.into(),
|
config::Error::User(e) => e.into(),
|
||||||
config::Error::Vfs(e) => e.into(),
|
config::Error::Vfs(e) => e.into(),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue