Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0082018

Browse files
committedFeb 7, 2025
refactor: allow errors to propagate out of init_client
1 parent 0f58cf5 commit 0082018

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed
 

‎compiler-cli/src/http.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ impl gleam_core::io::HttpClient for HttpClient {
2727
let request = request
2828
.try_into()
2929
.expect("Unable to convert HTTP request for use by reqwest library");
30-
let mut response = REQWEST_CLIENT
31-
.get_or_init(|| init_client().expect("Unable to create reqwest client"))
32-
.execute(request)
33-
.await
34-
.map_err(Error::http)?;
30+
let client = init_client().map_err(Error::http)?;
31+
let mut response = client.execute(request).await.map_err(Error::http)?;
3532
let mut builder = Response::builder()
3633
.status(response.status())
3734
.version(response.version());
@@ -44,21 +41,28 @@ impl gleam_core::io::HttpClient for HttpClient {
4441
}
4542
}
4643

47-
fn init_client() -> Result<Client, Error> {
44+
fn init_client() -> Result<&'static Client, Error> {
45+
if let Some(client) = REQWEST_CLIENT.get() {
46+
return Ok(client);
47+
}
48+
4849
let certificate_path = std::env::var("GLEAM_CACERTS_PATH")
4950
.map_err(|_| Error::CannotReadCertificate { path: "".into() })?;
51+
5052
let certificate_bytes =
5153
std::fs::read(&certificate_path).map_err(|_| Error::CannotReadCertificate {
5254
path: certificate_path.clone(),
5355
})?;
56+
5457
let certificate =
5558
Certificate::from_pem(&certificate_bytes).map_err(|_| Error::CannotReadCertificate {
5659
path: certificate_path.clone(),
5760
})?;
58-
Client::builder()
59-
.add_root_certificate(certificate)
60-
.build()
61-
.map_err(|_| Error::CannotReadCertificate {
62-
path: certificate_path,
63-
})
61+
62+
match Client::builder().add_root_certificate(certificate).build() {
63+
Ok(client) => Ok(REQWEST_CLIENT.get_or_init(|| client)),
64+
Err(_) => Err(Error::CannotReadCertificate {
65+
path: certificate_path.clone(),
66+
}),
67+
}
6468
}

0 commit comments

Comments
 (0)
Please sign in to comment.