nautilus_network/http/error.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! HTTP client error types.
17
18use std::error::Error;
19
20/// Errors returned by the HTTP client.
21///
22/// Includes generic transport errors, timeouts, and proxy configuration errors.
23#[derive(thiserror::Error, Debug)]
24pub enum HttpClientError {
25 #[error("HTTP error occurred: {0}")]
26 Error(String),
27
28 #[error("HTTP request timed out: {0}")]
29 TimeoutError(String),
30
31 #[error("Invalid proxy URL: {0}")]
32 InvalidProxy(String),
33
34 #[error("Failed to build HTTP client: {0}")]
35 ClientBuildError(String),
36}
37
38impl From<reqwest::Error> for HttpClientError {
39 fn from(source: reqwest::Error) -> Self {
40 // reqwest's Display omits the actionable cause (DNS, refused, TLS),
41 // which lives in the source chain, so walk and append it.
42 let mut message = source.to_string();
43 let mut cause: Option<&(dyn std::error::Error + 'static)> = source.source();
44 while let Some(err) = cause {
45 message.push_str(": ");
46 message.push_str(&err.to_string());
47 cause = err.source();
48 }
49
50 if source.is_timeout() {
51 Self::TimeoutError(message)
52 } else {
53 Self::Error(message)
54 }
55 }
56}
57
58impl From<String> for HttpClientError {
59 fn from(value: String) -> Self {
60 Self::Error(value)
61 }
62}