Skip to main content

gadk_model/rest/
error.rs

1// Copyright 2026 Chris Olson
2// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! The transport's provider-neutral error.
17
18use crate::error::ModelError;
19
20/// Why [`RestTransport::send`](super::RestTransport::send) failed, or (boxed in
21/// [`ModelError::Other`]) why a streaming body under a deadline ended.
22///
23/// `Display` carries no provider prefix: the transport's own texts
24/// (`net/http: timeout awaiting response headers`, `context deadline exceeded`), else the
25/// cause's `Display` (a [`ModelError`]'s, e.g. `transport error: …`). Each provider maps the
26/// variants to its own error type and text instead of using this `Display`.
27#[derive(Debug, thiserror::Error)]
28pub enum RestError {
29    /// The last attempt received no response: [`HttpClient::send`](crate::HttpClient::send)
30    /// failed (connection error, invalid request, …). The cause is kept as it is so the
31    /// provider can classify it and strip URLs.
32    #[error("{0}")]
33    Transport(ModelError),
34    /// The last attempt's [`RequestHeaders::headers`](super::RequestHeaders::headers) failed
35    /// (for example a credential fetch); the provider's own error, boxed.
36    #[error("{0}")]
37    Headers(Box<dyn std::error::Error + Send + Sync>),
38    /// The last attempt's response headers did not arrive within the transport's
39    /// `response_header_timeout`.
40    #[error("net/http: timeout awaiting response headers")]
41    ResponseHeaderTimeout,
42    /// The call's deadline passed: the last attempt's
43    /// [`CallTimeout::PerAttempt`](super::CallTimeout::PerAttempt) deadline, or the
44    /// [`CallTimeout::Call`](super::CallTimeout::Call) deadline at any point of the call.
45    #[error("context deadline exceeded")]
46    DeadlineExceeded,
47    /// The final response's body failed while being read (the body of every status ≥ 400,
48    /// and of a non-streaming 2xx); `parts` is that response's head, `source` the body error.
49    #[error("{source}")]
50    DroppedBody {
51        /// The response head (status and headers), boxed to keep the error small.
52        parts: Box<http::response::Parts>,
53        /// The body stream's error.
54        source: ModelError,
55    },
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn display_is_the_cause_or_gos_runtime_text() {
64        let io = std::io::Error::other("connection refused");
65        assert_eq!(
66            RestError::Transport(ModelError::Transport(Box::new(io))).to_string(),
67            "transport error: connection refused"
68        );
69        assert_eq!(RestError::Headers("boom".into()).to_string(), "boom");
70        assert_eq!(
71            RestError::ResponseHeaderTimeout.to_string(),
72            "net/http: timeout awaiting response headers"
73        );
74        assert_eq!(
75            RestError::DeadlineExceeded.to_string(),
76            "context deadline exceeded"
77        );
78        let (parts, ()) = http::Response::new(()).into_parts();
79        let io = std::io::Error::other("unexpected EOF");
80        assert_eq!(
81            RestError::DroppedBody {
82                parts: Box::new(parts),
83                source: ModelError::Transport(Box::new(io)),
84            }
85            .to_string(),
86            "transport error: unexpected EOF"
87        );
88    }
89
90    #[test]
91    fn rest_error_is_send_sync_static() {
92        fn assert_bounds<T: std::error::Error + Send + Sync + 'static>() {}
93        assert_bounds::<RestError>();
94    }
95}