From 91fa1a720c4fb47af1e9f7d9b95533d969709ada Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 23 Nov 2023 15:42:47 +0100 Subject: net: Improve the errors returned from wkd::get. - Notably, check the http status code. --- net/src/wkd.rs | 25 +++++++++++++++++++------ 1 file 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>(body: &[u8], email_address: S) // Collect only the correct packets. let certs: Vec> = 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>(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>(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::*; -- cgit v1.2.3