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,5 +1,5 @@
use anyhow::bail; use anyhow::bail;
use log::info; use log::{info, warn};
use reqwest::Client; use reqwest::Client;
use roxmltree::Document; use roxmltree::Document;
use urlencoding::encode; use urlencoding::encode;
@ -7,7 +7,7 @@ use urlencoding::encode;
use crate::{ use crate::{
credential::Credential, credential::Credential,
gp_params::GpParams, gp_params::GpParams,
utils::{normalize_server, remove_url_scheme}, utils::{normalize_server, parse_gp_error, remove_url_scheme},
}; };
pub async fn gateway_login(gateway: &str, cred: &Credential, gp_params: &GpParams) -> anyhow::Result<String> { pub async fn gateway_login(gateway: &str, cred: &Credential, gp_params: &GpParams) -> anyhow::Result<String> {
@ -32,7 +32,14 @@ pub async fn gateway_login(gateway: &str, cred: &Credential, gp_params: &GpParam
let status = res.status(); let status = res.status();
if status.is_client_error() || status.is_server_error() { if status.is_client_error() || status.is_server_error() {
bail!("Gateway login error: {}", status) let (reason, res) = parse_gp_error(res).await;
warn!(
"Gateway login error: reason={}, status={}, response={}",
reason, status, res
);
bail!("Gateway login error, reason: {}", reason);
} }
let res_xml = res.text().await?; let res_xml = res.text().await?;

View File

@ -1,5 +1,5 @@
use anyhow::bail; use anyhow::bail;
use log::info; use log::{info, warn};
use reqwest::{Client, StatusCode}; use reqwest::{Client, StatusCode};
use roxmltree::Document; use roxmltree::Document;
use serde::Serialize; use serde::Serialize;
@ -10,7 +10,7 @@ use crate::{
gateway::{parse_gateways, Gateway}, gateway::{parse_gateways, Gateway},
gp_params::GpParams, gp_params::GpParams,
portal::PortalError, portal::PortalError,
utils::{normalize_server, remove_url_scheme, xml}, utils::{normalize_server, parse_gp_error, remove_url_scheme, xml},
}; };
#[derive(Debug, Serialize, Type)] #[derive(Debug, Serialize, Type)]
@ -110,7 +110,14 @@ pub async fn retrieve_config(portal: &str, cred: &Credential, gp_params: &GpPara
} }
if status.is_client_error() || status.is_server_error() { if status.is_client_error() || status.is_server_error() {
bail!("Portal config error: {}", status) let (reason, res) = parse_gp_error(res).await;
warn!(
"Portal config error: reason={}, status={}, response={}",
reason, status, res
);
bail!("Portal config error, reason: {}", reason);
} }
let res_xml = res.text().await.map_err(|e| PortalError::ConfigError(e.to_string()))?; let res_xml = res.text().await.map_err(|e| PortalError::ConfigError(e.to_string()))?;

View File

@ -1,5 +1,5 @@
use anyhow::bail; use anyhow::bail;
use log::info; use log::{info, warn};
use reqwest::{Client, StatusCode}; use reqwest::{Client, StatusCode};
use roxmltree::Document; use roxmltree::Document;
use serde::Serialize; use serde::Serialize;
@ -8,7 +8,7 @@ use specta::Type;
use crate::{ use crate::{
gp_params::GpParams, gp_params::GpParams,
portal::PortalError, portal::PortalError,
utils::{base64, normalize_server, xml}, utils::{base64, normalize_server, parse_gp_error, xml},
}; };
const REQUIRED_PARAMS: [&str; 8] = [ const REQUIRED_PARAMS: [&str; 8] = [
@ -126,6 +126,10 @@ pub async fn prelogin(portal: &str, gp_params: &GpParams) -> anyhow::Result<Prel
} }
if status.is_client_error() || status.is_server_error() { if status.is_client_error() || status.is_server_error() {
let (reason, res) = parse_gp_error(res).await;
warn!("Prelogin error: reason={}, status={}, response={}", reason, status, res);
bail!("Prelogin error: {}", status) bail!("Prelogin error: {}", status)
} }

View File

@ -1,4 +1,4 @@
use reqwest::Url; use reqwest::{Response, Url};
pub(crate) mod xml; 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 { pub fn remove_url_scheme(s: &str) -> String {
s.replace("http://", "").replace("https://", "") 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)
}