From 98f0563952e558e41aba396bd01f3b8db9d1aad7 Mon Sep 17 00:00:00 2001 From: Delta1925 Date: Thu, 13 Apr 2023 23:32:12 +0200 Subject: [PATCH] Improve error handling --- src/confirmation.rs | 6 +++--- src/errors.rs | 22 ++++++++++------------ src/main.rs | 2 +- src/utils.rs | 6 +++--- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/confirmation.rs b/src/confirmation.rs index 620693a..2563cd0 100644 --- a/src/confirmation.rs +++ b/src/confirmation.rs @@ -15,18 +15,18 @@ pub fn confirm_action(token: &str) -> Result<(), Error> { Err(_) => return Err(Error::Inaccessible), } } else { - return Err(Error::MissingPath); + return Err(Error::MissingPending); }; let key = match serde_json::from_str::(&content) { Ok(key) => key, - Err(_) => return Err(Error::ParseStored), + Err(_) => return Err(Error::DeserializeData), }; match key.action() { Action::Add => { let cert = parse_pem(key.data())?; let domain = match get_email_from_cert(&cert)?.split('@').last() { Some(domain) => domain.to_string(), - None => return Err(Error::MalformedMail), + None => return Err(Error::ParseEmail), }; match sequoia_net::wkd::insert(PATH, domain, VARIANT, &cert) { Ok(_) => (), diff --git a/src/errors.rs b/src/errors.rs index 6cc1dcc..70c33ca 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,36 +1,34 @@ use actix_web::http::StatusCode; use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Error, Debug, Clone, Copy)] pub enum Error { #[error("Error while parsing cert")] ParseCert, - #[error("Error while parsing E-Mail")] - ParseMail, - #[error("Error while parsing stored data")] - ParseStored, + #[error("Error while parsing an E-Mail address")] + ParseEmail, + #[error("There is no pending request associated to this token")] + MissingPending, + #[error("Requested key does not exist")] + MissingKey, #[error("No E-Mail found in the certificate")] MissingMail, - #[error("The E-Mail is malformed")] - MalformedMail, #[error("Error while serializing data")] SerializeData, #[error("Error while deserializing data")] DeserializeData, - #[error("File or directory does not exist")] - MissingPath, - #[error("Requested key does not exist")] - MissingKey, #[error("The file is inaccessible")] Inaccessible, #[error("Error while adding a key to the wkd")] AddingKey, + #[error("Error while generating the wkd path")] + PathGeneration, } impl actix_web::ResponseError for Error { fn status_code(&self) -> actix_web::http::StatusCode { match self { - Self::MissingPath => StatusCode::from_u16(404).unwrap(), + Self::MissingPending => StatusCode::from_u16(404).unwrap(), Self::MissingKey => StatusCode::from_u16(404).unwrap(), _ => StatusCode::from_u16(500).unwrap(), } diff --git a/src/main.rs b/src/main.rs index ab701c2..8d22a5d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,7 +63,7 @@ async fn submit(pem: web::Form) -> Result { Ok(String::from("Key submitted successfully!")) } -#[get("/api/confirm/{data}")] +#[get("/api/confirm/{value}")] async fn confirm(token: web::Path) -> Result { confirm_action(&token.value)?; Ok(String::from("Confirmation successfull!")) diff --git a/src/utils.rs b/src/utils.rs index 22c53b5..7059d0f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -32,7 +32,7 @@ pub fn get_email_from_cert(cert: &Cert) -> Result { }; let email_opt = match userid_opt.email() { Ok(email_opt) => email_opt, - Err(_) => return Err(Error::ParseMail), + Err(_) => return Err(Error::ParseCert), }; match email_opt { Some(email) => Ok(email), @@ -43,11 +43,11 @@ pub fn get_email_from_cert(cert: &Cert) -> Result { pub fn get_user_file_path(email: &str) -> Result { let wkd_url = match Url::from(email) { Ok(wkd_url) => wkd_url, - Err(_) => return Err(Error::ParseMail), + Err(_) => return Err(Error::PathGeneration), }; match wkd_url.to_file_path(VARIANT) { Ok(path) => Ok(path), - Err(_) => Err(Error::ParseMail), + Err(_) => Err(Error::PathGeneration), } }