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),
}
} else {
return Err(Error::MissingPath);
return Err(Error::MissingPending);
};
let key = match serde_json::from_str::<Pending>(&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(_) => (),

View file

@ -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(),
}

View file

@ -63,7 +63,7 @@ async fn submit(pem: web::Form<Pem>) -> Result<String> {
Ok(String::from("Key submitted successfully!"))
}
#[get("/api/confirm/{data}")]
#[get("/api/confirm/{value}")]
async fn confirm(token: web::Path<Token>) -> Result<String> {
confirm_action(&token.value)?;
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() {
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<String, Error> {
pub fn get_user_file_path(email: &str) -> Result<PathBuf, Error> {
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),
}
}