summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2023-11-23 15:42:47 +0100
committerJustus Winter <justus@sequoia-pgp.org>2023-11-23 15:43:09 +0100
commit91fa1a720c4fb47af1e9f7d9b95533d969709ada (patch)
tree951ca47c5f4e74d9efb89c414e42cb9bf3ab8ed2
parentba2a1b2d034d131c6e3c7cda6a960d71d07ae7d4 (diff)
net: Improve the errors returned from wkd::get.
- Notably, check the http status code.
-rw-r--r--net/src/wkd.rs25
1 files changed, 19 insertions, 6 deletions
diff --git a/net/src/wkd.rs b/net/src/wkd.rs
index ab69139d..02160e1b 100644
--- a/net/src/wkd.rs
+++ b/net/src/wkd.rs
@@ -34,7 +34,7 @@ use sequoia_openpgp::{
};
use crate::email::EmailAddress;
-use crate::{Result, Error};
+use crate::Result;
/// WKD variants.
///
@@ -159,7 +159,7 @@ fn parse_body<S: AsRef<str>>(body: &[u8], email_address: S)
// Collect only the correct packets.
let certs: Vec<Result<Cert>> = packets.collect();
if certs.is_empty() {
- return Err(Error::NotFound.into());
+ return Err(crate::Error::NotFound.into());
}
// Collect only the Certs that contain the email in any of their userids
@@ -175,7 +175,7 @@ fn parse_body<S: AsRef<str>>(body: &[u8], email_address: S)
Err(_) => true,
}).collect();
if valid_certs.is_empty() {
- Err(Error::EmailNotInUserids(email_address.into()).into())
+ Err(crate::Error::EmailNotInUserids(email_address.into()).into())
} else {
Ok(valid_certs)
}
@@ -239,10 +239,14 @@ pub async fn get<S: AsRef<str>>(c: &reqwest::Client, email_address: S)
} else {
// Fall back to the Direct Method.
c.get(direct_uri).send().await
- }?;
- let body = res.bytes().await?;
+ }.map_err(Error::NotFound)?;
- parse_body(&body, &email)
+ if res.status() == reqwest::StatusCode::OK {
+ let body = res.bytes().await?;
+ parse_body(&body, &email)
+ } else {
+ Err(crate::Error::NotFound.into())
+ }
}
/// Returns all e-mail addresses from certificate's User IDs matching `domain`.
@@ -362,6 +366,15 @@ impl KeyRing {
}
}
+/// Errors for this module.
+#[derive(thiserror::Error, Debug)]
+#[non_exhaustive]
+pub enum Error {
+ /// A requested cert was not found.
+ #[error("Cert not found")]
+ NotFound(#[from] reqwest::Error),
+}
+
#[cfg(test)]
mod tests {
use super::*;