1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Error types for the pure Rust Hyper API.
use hyperdb_api_core::client::ErrorKind;
use std::error::Error as StdError;
use thiserror::Error as ThisError;
/// The error type for Hyper API operations.
///
/// This enum is `#[non_exhaustive]`: new variants and new fields on existing
/// struct variants may be added in minor releases. Match arms must include a
/// wildcard `_ =>` pattern.
#[derive(Debug, ThisError)]
#[non_exhaustive]
pub enum Error {
/// Error from the underlying Hyper client.
#[error("{0}")]
Client(#[from] hyperdb_api_core::client::Error),
/// I/O error.
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
/// Invalid name error (empty or too long).
#[error("Invalid name: {0}")]
InvalidName(String),
/// Invalid table definition.
#[error("Invalid table definition: {0}")]
InvalidTableDefinition(String),
/// Database object not found (table, schema, etc.).
#[error("Not found: {0}")]
NotFound(String),
/// Database object already exists.
#[error("Already exists: {0}")]
AlreadyExists(String),
/// Generic error with a custom message.
#[error("{message}")]
#[non_exhaustive]
Other {
/// The error message.
message: String,
/// The underlying cause of the error, if any.
#[source]
source: Option<Box<dyn StdError + Send + Sync>>,
},
}
impl Error {
/// Creates a new error with the given message.
///
/// This is a convenience constructor for creating generic errors.
pub fn new(message: impl Into<String>) -> Self {
Error::Other {
message: message.into(),
source: None,
}
}
/// Creates a new error with a cause.
///
/// This is a convenience constructor for creating generic errors with a source.
pub fn with_cause<E>(message: impl Into<String>, cause: E) -> Self
where
E: Into<Box<dyn StdError + Send + Sync>>,
{
Error::Other {
message: message.into(),
source: Some(cause.into()),
}
}
/// Returns the error kind, if this is a client error.
///
/// This is available when the error originates from `hyperdb_api_core::client::Error`.
/// Use this for matching on error categories (e.g., `ErrorKind::Connection`).
#[must_use]
pub fn kind(&self) -> Option<ErrorKind> {
match self {
Error::Client(err) => Some(err.kind()),
_ => None,
}
}
/// Returns the error message.
#[must_use]
pub fn message(&self) -> String {
self.to_string()
}
/// Extracts the `PostgreSQL` SQLSTATE code from the error, if available.
///
/// This is only available for database query errors from the Hyper client.
///
/// # Example
///
/// ```
/// use hyperdb_api::Error;
///
/// // Assuming we have a client error with SQLSTATE
/// // let err: Error = ...;
/// // if let Some("42P04") = err.sqlstate() {
/// // println!("Database already exists");
/// // }
/// ```
#[must_use]
pub fn sqlstate(&self) -> Option<&str> {
match self {
Error::Client(err) => err.sqlstate(),
_ => None,
}
}
}
impl From<std::convert::Infallible> for Error {
fn from(_: std::convert::Infallible) -> Self {
unreachable!()
}
}
/// Result type for Hyper API operations.
pub type Result<T> = std::result::Result<T, Error>;