Improve the error message

Related #327
This commit is contained in:
Kevin Yue
2024-03-23 20:05:54 +08:00
parent 558485f5a9
commit 08bd4efefa
4 changed files with 42 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
use reqwest::Url;
use reqwest::{Response, Url};
pub(crate) mod xml;
@@ -41,3 +41,18 @@ pub fn normalize_server(server: &str) -> anyhow::Result<String> {
pub fn remove_url_scheme(s: &str) -> String {
s.replace("http://", "").replace("https://", "")
}
pub(crate) async fn parse_gp_error(res: Response) -> (String, String) {
let reason = res
.headers()
.get("x-private-pan-globalprotect")
.map_or_else(|| "<none>", |v| v.to_str().unwrap_or("<invalid header>"))
.to_string();
let res = res.text().await.map_or_else(
|_| "<failed to read response>".to_string(),
|v| if v.is_empty() { "<empty>".to_string() } else { v },
);
(reason, res)
}