From 882ab4001d7134894088b34e25642a94e234040f Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sun, 19 May 2024 22:30:40 +0800 Subject: [PATCH] chore: improve error message --- crates/gpapi/src/utils/request.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/crates/gpapi/src/utils/request.rs b/crates/gpapi/src/utils/request.rs index 2a3dceb..f164c2d 100644 --- a/crates/gpapi/src/utils/request.rs +++ b/crates/gpapi/src/utils/request.rs @@ -17,16 +17,15 @@ pub enum RequestIdentityError { } /// Create an identity object from a certificate and key -pub(crate) fn create_identity_from_pem( - cert: &str, - key: Option<&str>, - passphrase: Option<&str>, -) -> anyhow::Result { - let cert_pem = fs::read(cert)?; +pub fn create_identity_from_pem(cert: &str, key: Option<&str>, passphrase: Option<&str>) -> anyhow::Result { + let cert_pem = fs::read(cert).map_err(|err| anyhow::anyhow!("Failed to read certificate file: {}", err))?; // Get the private key pem let key_pem = match key { - Some(key) => pem::parse(fs::read(key)?)?, + Some(key) => { + let pem_file = fs::read(key).map_err(|err| anyhow::anyhow!("Failed to read key file: {}", err))?; + pem::parse(pem_file)? + } None => { // If key is not provided, find the private key in the cert pem parse_many(&cert_pem)? @@ -57,7 +56,7 @@ pub(crate) fn create_identity_from_pem( Ok(identity) } -pub(crate) fn create_identity_from_pkcs12(pkcs12: &str, passphrase: Option<&str>) -> anyhow::Result { +pub fn create_identity_from_pkcs12(pkcs12: &str, passphrase: Option<&str>) -> anyhow::Result { let pkcs12 = fs::read(pkcs12)?; let Some(passphrase) = passphrase else {