From 9832a721f8bc6984afd0d0692f976035ea7d97ed Mon Sep 17 00:00:00 2001 From: Joe Birr-Pixton Date: Tue, 20 Aug 2024 15:59:34 +0100 Subject: [PATCH] Adjust error wording when loading missing files ```shell $ SSL_CERT_FILE="notexist" target/debug/examples/google thread 'main' panicked at examples/google.rs:7:58: could not load platform certs: Custom { kind: NotFound, error: "could not load certs from dir notexist: No such file or directory (os error 2)" } ``` Note "from dir notexist". In this case `path.is_file()` is false (because it doesn't exist) but that doesn't imply it was being read as a directory. --- src/lib.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c310ee9..c9d55d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -159,7 +159,7 @@ impl CertPaths { let mut certs = match &self.file { Some(cert_file) => { - load_pem_certs(cert_file).map_err(|err| Self::load_err(cert_file, err))? + load_pem_certs(cert_file).map_err(|err| Self::load_err(cert_file, "file", err))? } None => Vec::new(), }; @@ -167,7 +167,7 @@ impl CertPaths { if let Some(cert_dir) = &self.dir { certs.append( &mut load_pem_certs_from_dir(cert_dir) - .map_err(|err| Self::load_err(cert_dir, err))?, + .map_err(|err| Self::load_err(cert_dir, "dir", err))?, ); } @@ -177,14 +177,10 @@ impl CertPaths { Ok(Some(certs)) } - fn load_err(path: &Path, err: Error) -> Error { + fn load_err(path: &Path, typ: &str, err: Error) -> Error { Error::new( err.kind(), - format!( - "could not load certs from {} {}: {err}", - if path.is_file() { "file" } else { "dir" }, - path.display() - ), + format!("could not load certs from {typ} {}: {err}", path.display()), ) } }