Skip to content

Commit

Permalink
Adjust error wording when loading missing files
Browse files Browse the repository at this point in the history
```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.
  • Loading branch information
ctz committed Aug 28, 2024
1 parent 98612b4 commit 9832a72
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ 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(),
};

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))?,
);
}

Expand All @@ -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()),
)
}
}
Expand Down

0 comments on commit 9832a72

Please sign in to comment.