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,
#[error("Missing decryption key")]
NeedKey,
#[error(transparent)]
InvalidKey(#[from] PartialParsedUrlParseError),
}
impl FromStr for ParsedUrl {
@ -157,7 +159,7 @@ impl FromStr for ParsedUrl {
let PartialParsedUrl {
mut decryption_key,
needs_password,
} = PartialParsedUrl::try_from(fragment).unwrap_or_default();
} = PartialParsedUrl::try_from(fragment)?;
url.set_fragment(None);

View file

@ -89,10 +89,24 @@ fn main() {
};
let (key, needs_pw) = {
let partial_parsed_url = url
.split_once('#')
.and_then(|(_, fragment)| PartialParsedUrl::try_from(fragment).ok())
.unwrap_or_default();
let fragment = match url.split_once('#').map(|(_, fragment)| fragment) {
Some(fragment) => fragment,
None => {
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 {
key
} else {
@ -100,6 +114,7 @@ fn main() {
render_message("Invalid paste link: Missing decryption key.".into());
return;
};
(key, partial_parsed_url.needs_password)
};