headwaters_client/error.rs
1//! Error type for the client.
2
3/// An error from constructing or calling the Headwaters read API.
4#[derive(Debug, thiserror::Error)]
5#[non_exhaustive]
6pub enum Error {
7 /// The configured base URL could not be parsed.
8 #[error("invalid base URL: {0}")]
9 InvalidBaseUrl(String),
10
11 /// A required environment variable was not set (see [`from_env`]).
12 ///
13 /// [`from_env`]: crate::HeadwatersClient::from_env
14 #[error("missing environment variable: {0}")]
15 MissingEnvVar(&'static str),
16
17 /// The RPC failed — a transport error, or a non-OK status from the server.
18 #[error("read API request failed: {0}")]
19 Rpc(#[from] connectrpc::ConnectError),
20}
21
22impl Error {
23 /// The Connect status code, when this is an [`Error::Rpc`] carrying one.
24 ///
25 /// Lets callers branch on the failure kind — e.g. distinguish a missing
26 /// entity ([`is_not_found`](Self::is_not_found)) from a transport failure.
27 pub fn code(&self) -> Option<connectrpc::ErrorCode> {
28 match self {
29 Error::Rpc(e) => Some(e.code),
30 _ => None,
31 }
32 }
33
34 /// True when the failure is a `not_found` status — the requested job,
35 /// dataset, or run does not exist.
36 pub fn is_not_found(&self) -> bool {
37 self.code() == Some(connectrpc::ErrorCode::NotFound)
38 }
39}