mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 07:02:24 +00:00
work on errors
This commit is contained in:
42
crates/dav/src/error.rs
Normal file
42
crates/dav/src/error.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use actix_web::{http::StatusCode, HttpResponse};
|
||||
use thiserror::Error;
|
||||
|
||||
// use crate::routes::propfind;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
#[error("Not found")]
|
||||
NotFound,
|
||||
#[error("Bad request")]
|
||||
BadRequest,
|
||||
#[error("Unauthorized")]
|
||||
Unauthorized,
|
||||
#[error("Internal server error :(")]
|
||||
InternalError,
|
||||
// #[error(transparent)]
|
||||
// PropfindError(#[from] propfind::Error),
|
||||
#[error("Internal server error")]
|
||||
Other(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
impl actix_web::error::ResponseError for Error {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match self {
|
||||
Self::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Self::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Self::NotFound => StatusCode::NOT_FOUND,
|
||||
Self::BadRequest => StatusCode::BAD_REQUEST,
|
||||
Self::Unauthorized => StatusCode::UNAUTHORIZED,
|
||||
// Self::PropfindError(e) => e.status_code(),
|
||||
}
|
||||
}
|
||||
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
match self {
|
||||
Error::Unauthorized => HttpResponse::build(self.status_code())
|
||||
.append_header(("WWW-Authenticate", "Basic"))
|
||||
.body(self.to_string()),
|
||||
_ => HttpResponse::build(self.status_code()).body(self.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod depth_extractor;
|
||||
pub mod error;
|
||||
pub mod namespace;
|
||||
pub mod resource;
|
||||
pub mod xml_snippets;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::{error::Error, xml_snippets::TagList};
|
||||
use actix_web::{http::StatusCode, HttpRequest};
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
@@ -7,8 +8,6 @@ use serde::Serialize;
|
||||
use std::str::FromStr;
|
||||
use strum::{EnumProperty, VariantNames};
|
||||
|
||||
use crate::xml_snippets::TagList;
|
||||
|
||||
// A resource is identified by a URI and has properties
|
||||
// A resource can also be a collection
|
||||
// A resource cannot be none, only Methods like PROPFIND, GET, REPORT, etc. can be exposed
|
||||
@@ -25,7 +24,7 @@ pub trait Resource: Sized {
|
||||
auth_info: AuthInfo,
|
||||
uri_components: Self::UriComponents,
|
||||
prefix: String,
|
||||
) -> Result<Self>;
|
||||
) -> Result<Self, Error>;
|
||||
|
||||
fn get_path(&self) -> &str;
|
||||
async fn get_members(&self) -> Result<Vec<Self::MemberType>>;
|
||||
|
||||
Reference in New Issue
Block a user