0
0
Fork 0
mirror of https://git.verdigado.com/NB-Public/simple-wkd.git synced 2024-10-30 03:05:51 +01:00

Improve error handling

This commit is contained in:
Delta1925 2023-04-13 23:32:12 +02:00
parent 4b445a6a96
commit 98f0563952
No known key found for this signature in database
GPG key ID: 1C21ACE44193CB25
4 changed files with 17 additions and 19 deletions

View file

@ -15,18 +15,18 @@ pub fn confirm_action(token: &str) -> Result<(), Error> {
Err(_) => return Err(Error::Inaccessible), Err(_) => return Err(Error::Inaccessible),
} }
} else { } else {
return Err(Error::MissingPath); return Err(Error::MissingPending);
}; };
let key = match serde_json::from_str::<Pending>(&content) { let key = match serde_json::from_str::<Pending>(&content) {
Ok(key) => key, Ok(key) => key,
Err(_) => return Err(Error::ParseStored), Err(_) => return Err(Error::DeserializeData),
}; };
match key.action() { match key.action() {
Action::Add => { Action::Add => {
let cert = parse_pem(key.data())?; let cert = parse_pem(key.data())?;
let domain = match get_email_from_cert(&cert)?.split('@').last() { let domain = match get_email_from_cert(&cert)?.split('@').last() {
Some(domain) => domain.to_string(), Some(domain) => domain.to_string(),
None => return Err(Error::MalformedMail), None => return Err(Error::ParseEmail),
}; };
match sequoia_net::wkd::insert(PATH, domain, VARIANT, &cert) { match sequoia_net::wkd::insert(PATH, domain, VARIANT, &cert) {
Ok(_) => (), Ok(_) => (),

View file

@ -1,36 +1,34 @@
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use thiserror::Error; use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug, Clone, Copy)]
pub enum Error { pub enum Error {
#[error("Error while parsing cert")] #[error("Error while parsing cert")]
ParseCert, ParseCert,
#[error("Error while parsing E-Mail")] #[error("Error while parsing an E-Mail address")]
ParseMail, ParseEmail,
#[error("Error while parsing stored data")] #[error("There is no pending request associated to this token")]
ParseStored, MissingPending,
#[error("Requested key does not exist")]
MissingKey,
#[error("No E-Mail found in the certificate")] #[error("No E-Mail found in the certificate")]
MissingMail, MissingMail,
#[error("The E-Mail is malformed")]
MalformedMail,
#[error("Error while serializing data")] #[error("Error while serializing data")]
SerializeData, SerializeData,
#[error("Error while deserializing data")] #[error("Error while deserializing data")]
DeserializeData, DeserializeData,
#[error("File or directory does not exist")]
MissingPath,
#[error("Requested key does not exist")]
MissingKey,
#[error("The file is inaccessible")] #[error("The file is inaccessible")]
Inaccessible, Inaccessible,
#[error("Error while adding a key to the wkd")] #[error("Error while adding a key to the wkd")]
AddingKey, AddingKey,
#[error("Error while generating the wkd path")]
PathGeneration,
} }
impl actix_web::ResponseError for Error { impl actix_web::ResponseError for Error {
fn status_code(&self) -> actix_web::http::StatusCode { fn status_code(&self) -> actix_web::http::StatusCode {
match self { match self {
Self::MissingPath => StatusCode::from_u16(404).unwrap(), Self::MissingPending => StatusCode::from_u16(404).unwrap(),
Self::MissingKey => StatusCode::from_u16(404).unwrap(), Self::MissingKey => StatusCode::from_u16(404).unwrap(),
_ => StatusCode::from_u16(500).unwrap(), _ => StatusCode::from_u16(500).unwrap(),
} }

View file

@ -63,7 +63,7 @@ async fn submit(pem: web::Form<Pem>) -> Result<String> {
Ok(String::from("Key submitted successfully!")) Ok(String::from("Key submitted successfully!"))
} }
#[get("/api/confirm/{data}")] #[get("/api/confirm/{value}")]
async fn confirm(token: web::Path<Token>) -> Result<String> { async fn confirm(token: web::Path<Token>) -> Result<String> {
confirm_action(&token.value)?; confirm_action(&token.value)?;
Ok(String::from("Confirmation successfull!")) Ok(String::from("Confirmation successfull!"))

View file

@ -32,7 +32,7 @@ pub fn get_email_from_cert(cert: &Cert) -> Result<String, Error> {
}; };
let email_opt = match userid_opt.email() { let email_opt = match userid_opt.email() {
Ok(email_opt) => email_opt, Ok(email_opt) => email_opt,
Err(_) => return Err(Error::ParseMail), Err(_) => return Err(Error::ParseCert),
}; };
match email_opt { match email_opt {
Some(email) => Ok(email), Some(email) => Ok(email),
@ -43,11 +43,11 @@ pub fn get_email_from_cert(cert: &Cert) -> Result<String, Error> {
pub fn get_user_file_path(email: &str) -> Result<PathBuf, Error> { pub fn get_user_file_path(email: &str) -> Result<PathBuf, Error> {
let wkd_url = match Url::from(email) { let wkd_url = match Url::from(email) {
Ok(wkd_url) => wkd_url, Ok(wkd_url) => wkd_url,
Err(_) => return Err(Error::ParseMail), Err(_) => return Err(Error::PathGeneration),
}; };
match wkd_url.to_file_path(VARIANT) { match wkd_url.to_file_path(VARIANT) {
Ok(path) => Ok(path), Ok(path) => Ok(path),
Err(_) => Err(Error::ParseMail), Err(_) => Err(Error::PathGeneration),
} }
} }