Error typing for rustical_store as well as some refactoring

This commit is contained in:
Lennart
2024-06-01 13:00:36 +02:00
parent 7fcd9a17f5
commit 1d763b5c8f
20 changed files with 135 additions and 79 deletions

View File

@@ -5,7 +5,7 @@ use actix_web::{
HttpResponse,
};
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
use rustical_store::store::CalendarStore;
use rustical_store::CalendarStore;
pub async fn route_delete_calendar<A: CheckAuthentication, C: CalendarStore + ?Sized>(
context: Data<CalDavContext<C>>,

View File

@@ -5,7 +5,7 @@ use actix_web::HttpResponse;
use anyhow::Result;
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
use rustical_store::calendar::Calendar;
use rustical_store::store::CalendarStore;
use rustical_store::CalendarStore;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Clone, Debug)]
@@ -69,10 +69,7 @@ pub async fn route_mkcol_calendar<A: CheckAuthentication, C: CalendarStore + ?Si
return Err(Error::Unauthorized);
}
let request: MkcalendarRequest = quick_xml::de::from_str(&body).map_err(|e| {
dbg!(e.to_string());
Error::BadRequest
})?;
let request: MkcalendarRequest = quick_xml::de::from_str(&body)?;
let request = request.set.prop;
let calendar = Calendar {

View File

@@ -10,7 +10,7 @@ use rustical_dav::{
resource::HandlePropfind,
};
use rustical_store::event::Event;
use rustical_store::store::CalendarStore;
use rustical_store::CalendarStore;
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
@@ -154,10 +154,7 @@ pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore + ?S
return Err(Error::Unauthorized);
}
let request: ReportRequest = quick_xml::de::from_str(&body).map_err(|err| {
dbg!(err.to_string());
Error::InternalError
})?;
let request: ReportRequest = quick_xml::de::from_str(&body)?;
let events = match request.clone() {
ReportRequest::CalendarQuery(cal_query) => {
get_events_calendar_query(cal_query, &cid, &cal_store).await?
@@ -178,7 +175,7 @@ pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore + ?S
}
PropfindType::Propname => {
// TODO: Implement
return Err(Error::InternalError);
return Err(Error::NotImplemented);
}
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags.into(),
};

View File

@@ -6,7 +6,7 @@ use rustical_dav::error::Error;
use rustical_dav::resource::{Resource, ResourceService};
use rustical_dav::xml_snippets::{HrefElement, TextNode};
use rustical_store::calendar::Calendar;
use rustical_store::store::CalendarStore;
use rustical_store::CalendarStore;
use serde::Serialize;
use std::sync::Arc;
use strum::{EnumString, IntoStaticStr, VariantNames};

View File

@@ -0,0 +1,44 @@
use actix_web::{http::StatusCode, HttpResponse};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Unauthorized")]
Unauthorized,
#[error("Not implemented")]
NotImplemented,
#[error(transparent)]
StoreError(#[from] rustical_store::Error),
#[error(transparent)]
DavError(#[from] rustical_dav::Error),
#[error(transparent)]
XmlDecodeError(#[from] quick_xml::DeError),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl actix_web::ResponseError for Error {
fn status_code(&self) -> actix_web::http::StatusCode {
match self {
Error::StoreError(err) => match err {
rustical_store::Error::NotFound => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
},
Error::DavError(err) => err.status_code(),
Error::Unauthorized => StatusCode::UNAUTHORIZED,
Error::XmlDecodeError(_) => StatusCode::BAD_REQUEST,
Error::NotImplemented => StatusCode::INTERNAL_SERVER_ERROR,
Error::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
match self {
Error::DavError(err) => err.error_response(),
_ => HttpResponse::build(self.status_code()).body(self.to_string()),
}
}
}

View File

@@ -1,27 +1,9 @@
use crate::CalDavContext;
use actix_web::http::StatusCode;
use crate::Error;
use actix_web::web::{Data, Path};
use actix_web::{HttpResponse, ResponseError};
use actix_web::HttpResponse;
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
use rustical_store::store::CalendarStore;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl ResponseError for Error {
fn status_code(&self) -> actix_web::http::StatusCode {
match self {
Self::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> {
HttpResponse::build(self.status_code()).body(self.to_string())
}
}
use rustical_store::CalendarStore;
pub async fn delete_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
context: Data<CalDavContext<C>>,

View File

@@ -6,7 +6,7 @@ use rustical_dav::error::Error;
use rustical_dav::resource::{Resource, ResourceService};
use rustical_dav::xml_snippets::TextNode;
use rustical_store::event::Event;
use rustical_store::store::CalendarStore;
use rustical_store::CalendarStore;
use serde::Serialize;
use std::sync::Arc;
use strum::{EnumString, IntoStaticStr, VariantNames};

View File

@@ -6,18 +6,20 @@ use event::resource::EventResource;
use principal::PrincipalResource;
use root::RootResource;
use rustical_auth::CheckAuthentication;
use rustical_dav::error::Error;
use rustical_dav::propfind::{route_propfind, ServicePrefix};
use rustical_store::store::CalendarStore;
use rustical_store::CalendarStore;
use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::RwLock;
pub mod calendar;
pub mod error;
pub mod event;
pub mod principal;
pub mod root;
pub use error::Error;
pub struct CalDavContext<C: CalendarStore + ?Sized> {
pub store: Arc<RwLock<C>>,
}

View File

@@ -7,7 +7,7 @@ use async_trait::async_trait;
use rustical_auth::AuthInfo;
use rustical_dav::resource::{Resource, ResourceService};
use rustical_dav::xml_snippets::HrefElement;
use rustical_store::store::CalendarStore;
use rustical_store::CalendarStore;
use serde::Serialize;
use strum::{EnumString, IntoStaticStr, VariantNames};
use tokio::sync::RwLock;