Skip to main content

gadk_openapi/
error.rs

1// Copyright 2026 Chris Olson
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
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//! The crate error ([`Error`]).
16
17/// An error loading an OpenAPI document, building the toolset, or sending a tool's request.
18///
19/// Loading and building return it directly. A tool call returns it as the source of
20/// [`ToolError::Execution`](gadk_agent::ToolError::Execution) (`Request`, `Timeout` and
21/// `Cancelled`).
22#[derive(Debug, thiserror::Error)]
23#[non_exhaustive]
24pub enum Error {
25    /// The text is neither a JSON nor a YAML object, or a file could not be read.
26    #[error("cannot parse the OpenAPI document: {message}")]
27    Parse {
28        /// What went wrong.
29        message: String,
30    },
31    /// The document's `openapi` field is missing or is not `3.0.x` or `3.1.x`.
32    #[error("unsupported OpenAPI version {found}: only 3.0.x and 3.1.x are supported")]
33    UnsupportedVersion {
34        /// The version found (`swagger 2.0` for a Swagger 2.0 document, `no openapi field` when
35        /// there is none).
36        found: String,
37    },
38    /// A `$ref` is not local (`#/…`) or points at nothing.
39    #[error("cannot resolve $ref {reference:?} in {at}")]
40    Ref {
41        /// The reference.
42        reference: String,
43        /// The operation it was found in, as `<METHOD> <path>`.
44        at: String,
45    },
46    /// Two operations have the same tool name, or two properties of one tool have the same name
47    /// (`<tool name>.<property>`).
48    #[error("duplicate name {name:?}")]
49    DuplicateName {
50        /// The duplicated name.
51        name: String,
52    },
53    /// A prefixed tool name is longer than 64 bytes.
54    #[error("tool name {name:?} is longer than 64 bytes")]
55    NameTooLong {
56        /// The prefixed name.
57        name: String,
58    },
59    /// There is no absolute base URL: the server URL is relative, the document was not fetched,
60    /// and `OpenApiConfig::base_url` is not set (or is not an absolute URL). A base URL with a
61    /// query or fragment is rejected the same way.
62    #[error(
63        "no absolute base URL: set OpenApiConfig::base_url or give the document an absolute servers[0].url"
64    )]
65    NoBaseUrl,
66    /// Downloading the document failed.
67    #[error("cannot fetch the OpenAPI document from {url}: {message}")]
68    Fetch {
69        /// The URL.
70        url: String,
71        /// What went wrong.
72        message: String,
73    },
74    /// The HTTP client for the tools' requests could not be built (for example, the TLS backend
75    /// failed to initialize).
76    #[error("cannot build the HTTP client: {source}")]
77    HttpClient {
78        /// The underlying error.
79        source: Box<dyn std::error::Error + Send + Sync>,
80    },
81    /// Sending a tool's request or reading its response failed.
82    #[error("tool {tool}: request failed: {source}")]
83    Request {
84        /// The exposed tool name.
85        tool: String,
86        /// The underlying error (connection, DNS, the credential provider, or the body read).
87        source: Box<dyn std::error::Error + Send + Sync>,
88    },
89    /// The tool's request did not finish within `OpenApiConfig::timeout`.
90    #[error("tool {tool}: request timed out")]
91    Timeout {
92        /// The exposed tool name.
93        tool: String,
94    },
95    /// The invocation was cancelled while the tool's request was in flight.
96    #[error("tool {tool}: request cancelled")]
97    Cancelled {
98        /// The exposed tool name.
99        tool: String,
100    },
101}