Propogate invalid url errors to web frontend

This commit is contained in:
Edward Shen 2022-01-11 23:48:53 -08:00
parent 83779545b9
commit 1fca1c51e4
Signed by: edward
GPG key ID: 19182661E818369F
2 changed files with 22 additions and 5 deletions

View file

@ -142,6 +142,8 @@ pub enum ParseUrlError {
BadUrl, BadUrl,
#[error("Missing decryption key")] #[error("Missing decryption key")]
NeedKey, NeedKey,
#[error(transparent)]
InvalidKey(#[from] PartialParsedUrlParseError),
} }
impl FromStr for ParsedUrl { impl FromStr for ParsedUrl {
@ -157,7 +159,7 @@ impl FromStr for ParsedUrl {
let PartialParsedUrl { let PartialParsedUrl {
mut decryption_key, mut decryption_key,
needs_password, needs_password,
} = PartialParsedUrl::try_from(fragment).unwrap_or_default(); } = PartialParsedUrl::try_from(fragment)?;
url.set_fragment(None); url.set_fragment(None);

View file

@ -89,10 +89,24 @@ fn main() {
}; };
let (key, needs_pw) = { let (key, needs_pw) = {
let partial_parsed_url = url let fragment = match url.split_once('#').map(|(_, fragment)| fragment) {
.split_once('#') Some(fragment) => fragment,
.and_then(|(_, fragment)| PartialParsedUrl::try_from(fragment).ok()) None => {
.unwrap_or_default(); error!("Key is missing in url; bailing.");
render_message("Invalid paste link: Missing decryption key.".into());
return;
}
};
let partial_parsed_url = match PartialParsedUrl::try_from(fragment) {
Ok(partial_parsed_url) => partial_parsed_url,
Err(e) => {
error!("Failed to parse text fragment; bailing.");
render_message(format!("Invalid paste link: {}", e.to_string()).into());
return;
}
};
let key = if let Some(key) = partial_parsed_url.decryption_key { let key = if let Some(key) = partial_parsed_url.decryption_key {
key key
} else { } else {
@ -100,6 +114,7 @@ fn main() {
render_message("Invalid paste link: Missing decryption key.".into()); render_message("Invalid paste link: Missing decryption key.".into());
return; return;
}; };
(key, partial_parsed_url.needs_password) (key, partial_parsed_url.needs_password)
}; };