From 6942320408b1e7882d9132edd45d781d49a6b570 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Wed, 17 Sep 2025 22:10:58 +0800 Subject: client(httpconnector): better error handling --- src/client/connector/httpconnector.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/client/connector/httpconnector.rs b/src/client/connector/httpconnector.rs index 9addf66..c636ed9 100644 --- a/src/client/connector/httpconnector.rs +++ b/src/client/connector/httpconnector.rs @@ -6,6 +6,24 @@ use reqwest::{ }; use std::{net::IpAddr, thread::sleep, time::Duration}; +#[derive(Debug)] +pub enum HTTPConnectorError { + Url(url::ParseError), + Reqwest(reqwest::Error), +} + +impl From for HTTPConnectorError { + fn from(e: url::ParseError) -> Self { + HTTPConnectorError::Url(e) + } +} + +impl From for HTTPConnectorError { + fn from(e: reqwest::Error) -> Self { + HTTPConnectorError::Reqwest(e) + } +} + pub struct HTTPConnector { network_enforcer: Executor<'static, 'static>, pull_url: Url, @@ -16,7 +34,7 @@ pub struct HTTPConnector { } impl HTTPConnector { - pub fn new(pull_url: &str, ntfy_url: &str, ip_getter_url: &str) -> std::result::Result { + pub fn new(pull_url: &str, ntfy_url: &str, ip_getter_url: &str) -> std::result::Result { Ok(Self { network_enforcer: Executor::new(&NETWORK_T, ["on"]).unwrap(), // .unwrap() should never panic prev: String::new(), @@ -26,7 +44,13 @@ impl HTTPConnector { Ok(url) => url, Err(_) => Url::parse((pull_url.to_string() + ip_getter_url).as_str())?, }, - client: Client::new(), + client: match Client::builder().build() { + Ok(c) => c, + Err(_) => match Client::builder().danger_accept_invalid_certs(true).build() { + Ok(c) => c, + Err(_) => Client::builder().danger_accept_invalid_certs(true).danger_accept_invalid_hostnames(true).build()?, + }, + }, }) } -- cgit v1.2.3