hyperdb_api/error.rs
1// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! Error types for the pure Rust Hyper API.
5//!
6//! Callers match directly on [`Error`] variants. There is no `kind()`
7//! indirection, no `Other` catch-all, and no `Box<dyn StdError>`
8//! cause channel — see the [Microsoft Pragmatic Rust Guidelines][1]
9//! M-ERRORS-CANONICAL-STRUCTS and M-ERRORS-AVOID-WRAPPING-AND-AS-DYN.
10//!
11//! Internal errors from [`hyperdb_api_core::client::Error`] are mapped
12//! into this flat enum at the crate boundary via the `From` impl below.
13//!
14//! [1]: https://microsoft.github.io/rust-guidelines/
15
16use thiserror::Error as ThisError;
17
18/// The error type for Hyper API operations.
19///
20/// This enum is `#[non_exhaustive]`: new variants may be added in minor
21/// releases, so match arms must include a wildcard `_ =>` pattern.
22///
23/// Struct variants (`Connection`, `Server`, `Column`,
24/// `ColumnIndexOutOfBounds`, `Internal`) cannot use Rust's
25/// `#[non_exhaustive]` (E0639), so forward-compatibility for new fields
26/// relies on construction via the provided constructors:
27///
28/// - [`Self::internal`] for [`Self::Internal`]
29/// - [`Self::connection`] / [`Self::connection_with_io`] for [`Self::Connection`]
30/// - [`Self::server`] for [`Self::Server`]
31/// - [`Self::column`] for [`Self::Column`]
32/// - [`Self::column_index_out_of_bounds`] for [`Self::ColumnIndexOutOfBounds`]
33///
34/// Downstream code that uses struct-expression syntax for these
35/// variants will fail to compile if a new field is added in a minor
36/// release; using the constructors keeps callers source-compatible.
37#[derive(Debug, ThisError)]
38#[non_exhaustive]
39pub enum Error {
40 // ---- Connection / transport ----------------------------------------
41 /// Connection-level failure (network, handshake, lifecycle, socket
42 /// I/O). Carries the underlying [`std::io::Error`] when one is
43 /// available; the type is erased at the wire-protocol boundary in
44 /// `hyperdb-api-core`, so `source` is `None` for errors that
45 /// originated there. `sqlstate` is set when the server provided a
46 /// connection-class SQLSTATE (e.g. `08001`, `08006`, `57P03`).
47 ///
48 /// Construct via [`Self::connection`], [`Self::connection_with_io`],
49 /// or [`Self::connection_with_sqlstate`].
50 #[error(
51 "connection error{}: {message}",
52 sqlstate.as_ref().map(|s| format!(" ({s})")).unwrap_or_default(),
53 )]
54 Connection {
55 /// Human-readable description.
56 message: String,
57 /// Underlying I/O error, if available.
58 #[source]
59 source: Option<std::io::Error>,
60 /// `PostgreSQL` SQLSTATE code, if the server provided one
61 /// (typically `08*` connection-class codes).
62 sqlstate: Option<String>,
63 },
64
65 /// Authentication failed.
66 #[error("authentication failed: {0}")]
67 Authentication(String),
68
69 /// TLS handshake or configuration failure.
70 #[error("TLS error: {0}")]
71 Tls(String),
72
73 // ---- Server-side ---------------------------------------------------
74 /// Server-side error (a SQL query or DDL command failed at the
75 /// server). `sqlstate` is the 5-character `PostgreSQL` SQLSTATE
76 /// code when the server reported one. `detail` and `hint` mirror
77 /// the structured fields the server may include in its error
78 /// response and are appended to the `Display` output when present.
79 #[error(
80 "server error{}: {message}{}{}",
81 sqlstate.as_ref().map(|s| format!(" ({s})")).unwrap_or_default(),
82 detail.as_ref().map(|d| format!("\nDETAIL: {d}")).unwrap_or_default(),
83 hint.as_ref().map(|h| format!("\nHINT: {h}")).unwrap_or_default(),
84 )]
85 Server {
86 /// The 5-character `PostgreSQL` SQLSTATE code, if reported.
87 sqlstate: Option<String>,
88 /// The primary error message from the server.
89 message: String,
90 /// Additional detail line from the server's error response.
91 detail: Option<String>,
92 /// Resolution hint from the server's error response.
93 hint: Option<String>,
94 },
95
96 /// Wire-protocol or framing error.
97 #[error("protocol error: {0}")]
98 Protocol(String),
99
100 // ---- I/O -----------------------------------------------------------
101 /// Direct I/O error (file system, non-network sockets) at the SDK
102 /// boundary. Network I/O during connection lifecycle is reported as
103 /// [`Self::Connection`] instead.
104 #[error("I/O error: {0}")]
105 Io(#[from] std::io::Error),
106
107 // ---- Lifecycle -----------------------------------------------------
108 /// Operation attempted on a closed connection. `sqlstate` is set
109 /// when the server provided one (typically `57P01` admin shutdown
110 /// or `57P02` crash shutdown). Construct via [`Self::closed`] or
111 /// [`Self::closed_with_sqlstate`].
112 #[error(
113 "connection closed{}: {message}",
114 sqlstate.as_ref().map(|s| format!(" ({s})")).unwrap_or_default(),
115 )]
116 Closed {
117 /// Human-readable description.
118 message: String,
119 /// `PostgreSQL` SQLSTATE code, if the server provided one.
120 sqlstate: Option<String>,
121 },
122
123 /// Operation timed out.
124 #[error("operation timed out: {0}")]
125 Timeout(String),
126
127 /// Operation was cancelled. `sqlstate` is set when the server
128 /// provided one (typically `57014` `query_canceled`). Construct via
129 /// [`Self::cancelled`] or [`Self::cancelled_with_sqlstate`].
130 #[error(
131 "operation cancelled{}: {message}",
132 sqlstate.as_ref().map(|s| format!(" ({s})")).unwrap_or_default(),
133 )]
134 Cancelled {
135 /// Human-readable description.
136 message: String,
137 /// `PostgreSQL` SQLSTATE code, if the server provided one.
138 sqlstate: Option<String>,
139 },
140
141 // ---- Type / value --------------------------------------------------
142 /// Type or value conversion failed (out-of-range numeric, malformed
143 /// binary value, scalar query returned no rows, etc.). For
144 /// column-specific decoding errors, prefer [`Self::Column`].
145 #[error("conversion error: {0}")]
146 Conversion(String),
147
148 /// Serialization or deserialization of a value failed (e.g. a
149 /// `get_as`/`set_as` JSON conversion). Distinct from
150 /// [`Self::Conversion`], which covers SQL type/binary decoding.
151 #[error("serialization error: {0}")]
152 Serialization(String),
153
154 /// Configuration error (invalid endpoint, missing env var, bad
155 /// option combination).
156 #[error("configuration error: {0}")]
157 Config(String),
158
159 /// Feature is not supported on this connection or transport.
160 #[error("feature not supported: {0}")]
161 FeatureNotSupported(String),
162
163 // ---- Catalog / validation ------------------------------------------
164 /// Database identifier is invalid (empty, exceeds the `PostgreSQL`
165 /// 63-byte limit, or violates other naming rules).
166 #[error("invalid name: {0}")]
167 InvalidName(String),
168
169 /// Table definition is invalid (zero columns, conflicting
170 /// attributes).
171 #[error("invalid table definition: {0}")]
172 InvalidTableDefinition(String),
173
174 /// Database object (schema, table, etc.) was not found.
175 #[error("not found: {0}")]
176 NotFound(String),
177
178 /// Database object already exists.
179 #[error("already exists: {0}")]
180 AlreadyExists(String),
181
182 /// Caller-API misuse: a method was called in an invalid sequence
183 /// or combination (e.g. mixing two mutually exclusive insertion
184 /// modes on a single inserter, calling a method after the resource
185 /// has been finalized). Distinct from [`Self::Internal`], which is
186 /// reserved for true library invariant violations the caller could
187 /// not have triggered. Construct via [`Self::invalid_operation`].
188 #[error("invalid operation: {0}")]
189 InvalidOperation(String),
190
191 // ---- Column / row mapping ------------------------------------------
192 /// Structured error for named-column access in row decoding. Used
193 /// by `FromRow` impls and `Row::try_get` / `Row::get_by_name` to
194 /// signal which column failed and why.
195 #[error("column {name}: {kind}")]
196 Column {
197 /// The column name.
198 name: String,
199 /// The structured cause of the column-access failure.
200 #[source]
201 kind: ColumnErrorKind,
202 },
203
204 /// Column index was out of bounds for the row. Used for positional
205 /// access; named access uses [`Self::Column`] with
206 /// [`ColumnErrorKind::Missing`].
207 #[error("column index {idx} out of bounds (row has {column_count} columns)")]
208 ColumnIndexOutOfBounds {
209 /// The requested 0-based column index.
210 idx: usize,
211 /// The actual column count of the row.
212 column_count: usize,
213 },
214
215 // ---- Internal ------------------------------------------------------
216 /// Internal invariant violation — a state the library believes
217 /// should be unreachable. Callers cannot trigger this from the
218 /// public API in well-formed code; reaching it indicates a bug
219 /// inside `hyperdb-api`. Recovery is generally impossible beyond
220 /// logging and bailing.
221 ///
222 /// For caller-API misuse (e.g. mixing two mutually exclusive
223 /// methods, using a finalized resource), prefer
224 /// [`Self::InvalidOperation`].
225 ///
226 /// Construct via [`Self::internal`].
227 #[error("internal error: {message}")]
228 Internal {
229 /// Human-readable description of what invariant was violated.
230 message: String,
231 },
232}
233
234/// The structured cause of an [`Error::Column`].
235#[derive(Debug, ThisError)]
236#[non_exhaustive]
237pub enum ColumnErrorKind {
238 /// Column name was not found in the result schema.
239 #[error("column not found")]
240 Missing,
241
242 /// Column was SQL `NULL` but the target type was not `Option<T>`.
243 #[error("unexpected NULL")]
244 Null,
245
246 /// Column value could not be decoded as the target type.
247 #[error("type mismatch: expected {expected}, got {actual}")]
248 TypeMismatch {
249 /// Rust type name the caller asked for.
250 expected: String,
251 /// Hyper SQL type name (or descriptive label) of the column.
252 actual: String,
253 },
254}
255
256impl Error {
257 /// Constructs an [`Self::Internal`] error. Prefer this over
258 /// struct-expression syntax to remain source-compatible if new
259 /// fields are added in a minor release.
260 pub fn internal(message: impl Into<String>) -> Self {
261 Error::Internal {
262 message: message.into(),
263 }
264 }
265
266 /// Constructs an [`Self::Connection`] error with no underlying I/O
267 /// source and no SQLSTATE. Prefer this over struct-expression
268 /// syntax to remain source-compatible if new fields are added in a
269 /// minor release.
270 pub fn connection(message: impl Into<String>) -> Self {
271 Error::Connection {
272 message: message.into(),
273 source: None,
274 sqlstate: None,
275 }
276 }
277
278 /// Constructs an [`Self::Connection`] error wrapping an underlying
279 /// [`std::io::Error`]. Prefer this over struct-expression syntax
280 /// to remain source-compatible if new fields are added in a minor
281 /// release.
282 pub fn connection_with_io(message: impl Into<String>, source: std::io::Error) -> Self {
283 Error::Connection {
284 message: message.into(),
285 source: Some(source),
286 sqlstate: None,
287 }
288 }
289
290 /// Constructs an [`Self::Connection`] error carrying a SQLSTATE
291 /// code (typically `08*` connection-class) and no I/O source.
292 pub fn connection_with_sqlstate(
293 message: impl Into<String>,
294 sqlstate: impl Into<String>,
295 ) -> Self {
296 Error::Connection {
297 message: message.into(),
298 source: None,
299 sqlstate: Some(sqlstate.into()),
300 }
301 }
302
303 /// Constructs an [`Self::Server`] error. Prefer this over
304 /// struct-expression syntax to remain source-compatible if new
305 /// fields are added in a minor release.
306 pub fn server(
307 sqlstate: Option<String>,
308 message: impl Into<String>,
309 detail: Option<String>,
310 hint: Option<String>,
311 ) -> Self {
312 Error::Server {
313 sqlstate,
314 message: message.into(),
315 detail,
316 hint,
317 }
318 }
319
320 /// Constructs an [`Self::Column`] error. Prefer this over
321 /// struct-expression syntax to remain source-compatible if new
322 /// fields are added in a minor release.
323 pub fn column(name: impl Into<String>, kind: ColumnErrorKind) -> Self {
324 Error::Column {
325 name: name.into(),
326 kind,
327 }
328 }
329
330 /// Constructs an [`Self::ColumnIndexOutOfBounds`] error. Prefer
331 /// this over struct-expression syntax to remain source-compatible
332 /// if new fields are added in a minor release.
333 pub fn column_index_out_of_bounds(idx: usize, column_count: usize) -> Self {
334 Error::ColumnIndexOutOfBounds { idx, column_count }
335 }
336
337 // ---- Tuple-variant constructors ------------------------------------
338 //
339 // These accept `impl Into<String>` so callers can pass either `&str`,
340 // `String`, or `format!(...)` without the `.to_string()` / `.into()`
341 // ceremony every direct construction would otherwise require.
342
343 /// Constructs an [`Self::Authentication`] error.
344 pub fn authentication(message: impl Into<String>) -> Self {
345 Error::Authentication(message.into())
346 }
347
348 /// Constructs an [`Self::Tls`] error.
349 pub fn tls(message: impl Into<String>) -> Self {
350 Error::Tls(message.into())
351 }
352
353 /// Constructs an [`Self::Protocol`] error.
354 pub fn protocol(message: impl Into<String>) -> Self {
355 Error::Protocol(message.into())
356 }
357
358 /// Constructs an [`Self::Closed`] error with no SQLSTATE.
359 pub fn closed(message: impl Into<String>) -> Self {
360 Error::Closed {
361 message: message.into(),
362 sqlstate: None,
363 }
364 }
365
366 /// Constructs an [`Self::Closed`] error carrying a SQLSTATE code
367 /// (typically `57P01` admin shutdown or `57P02` crash shutdown).
368 pub fn closed_with_sqlstate(message: impl Into<String>, sqlstate: impl Into<String>) -> Self {
369 Error::Closed {
370 message: message.into(),
371 sqlstate: Some(sqlstate.into()),
372 }
373 }
374
375 /// Constructs an [`Self::Timeout`] error.
376 pub fn timeout(message: impl Into<String>) -> Self {
377 Error::Timeout(message.into())
378 }
379
380 /// Constructs an [`Self::Cancelled`] error with no SQLSTATE.
381 pub fn cancelled(message: impl Into<String>) -> Self {
382 Error::Cancelled {
383 message: message.into(),
384 sqlstate: None,
385 }
386 }
387
388 /// Constructs an [`Self::Cancelled`] error carrying a SQLSTATE
389 /// code (typically `57014` `query_canceled`).
390 pub fn cancelled_with_sqlstate(
391 message: impl Into<String>,
392 sqlstate: impl Into<String>,
393 ) -> Self {
394 Error::Cancelled {
395 message: message.into(),
396 sqlstate: Some(sqlstate.into()),
397 }
398 }
399
400 /// Constructs an [`Self::Conversion`] error.
401 pub fn conversion(message: impl Into<String>) -> Self {
402 Error::Conversion(message.into())
403 }
404
405 /// Constructs an [`Self::Serialization`] error.
406 pub fn serialization(message: impl Into<String>) -> Self {
407 Error::Serialization(message.into())
408 }
409
410 /// Constructs an [`Self::Config`] error.
411 pub fn config(message: impl Into<String>) -> Self {
412 Error::Config(message.into())
413 }
414
415 /// Constructs an [`Self::FeatureNotSupported`] error.
416 pub fn feature_not_supported(message: impl Into<String>) -> Self {
417 Error::FeatureNotSupported(message.into())
418 }
419
420 /// Constructs an [`Self::InvalidName`] error.
421 pub fn invalid_name(message: impl Into<String>) -> Self {
422 Error::InvalidName(message.into())
423 }
424
425 /// Constructs an [`Self::InvalidTableDefinition`] error.
426 pub fn invalid_table_definition(message: impl Into<String>) -> Self {
427 Error::InvalidTableDefinition(message.into())
428 }
429
430 /// Constructs an [`Self::NotFound`] error.
431 pub fn not_found(message: impl Into<String>) -> Self {
432 Error::NotFound(message.into())
433 }
434
435 /// Constructs an [`Self::AlreadyExists`] error.
436 pub fn already_exists(message: impl Into<String>) -> Self {
437 Error::AlreadyExists(message.into())
438 }
439
440 /// Constructs an [`Self::InvalidOperation`] error.
441 pub fn invalid_operation(message: impl Into<String>) -> Self {
442 Error::InvalidOperation(message.into())
443 }
444
445 /// Returns the error message in human-readable form. Equivalent to
446 /// `self.to_string()`.
447 #[must_use]
448 pub fn message(&self) -> String {
449 self.to_string()
450 }
451
452 /// Returns the `PostgreSQL` SQLSTATE code if this error carries
453 /// one, otherwise `None`.
454 ///
455 /// Returns `Some(...)` for [`Self::Server`] (Query-class codes),
456 /// [`Self::Connection`] (typically `08*`), [`Self::Closed`]
457 /// (typically `57P0*` shutdown codes), and [`Self::Cancelled`]
458 /// (typically `57014` `query_canceled`) when the underlying server
459 /// provided a code.
460 ///
461 /// SQLSTATE codes are 5-character strings — see the [`PostgreSQL`
462 /// errcodes appendix][1].
463 ///
464 /// [1]: https://www.postgresql.org/docs/current/errcodes-appendix.html
465 #[must_use]
466 pub fn sqlstate(&self) -> Option<&str> {
467 match self {
468 Error::Server { sqlstate, .. }
469 | Error::Connection { sqlstate, .. }
470 | Error::Closed { sqlstate, .. }
471 | Error::Cancelled { sqlstate, .. } => sqlstate.as_deref(),
472 _ => None,
473 }
474 }
475}
476
477// Internal mapping: `client::Error` → public `Error`. The mapping is
478// exhaustive over `client::ErrorKind` (verified to NOT be
479// `#[non_exhaustive]`); adding a kind in `hyperdb-api-core` will break
480// this build until the mapping is updated, which is intended.
481//
482// `chain = err.to_string()` walks the inner error's full Display chain
483// (message + cause + detail). We use it for tuple variants whose
484// `Display` is just `"<prefix>: {0}"`, where embedding the chain into
485// the single string field gives the caller the full picture.
486//
487// For the `Server` variant we use the *un-chained* `message` and pass
488// `detail`/`hint` separately; the `Server` `Display` impl re-appends
489// "DETAIL: ..." and "HINT: ..." lines from those fields, so using
490// `chain` would duplicate the detail text.
491//
492// SQLSTATE: `client::Error::sqlstate()` may return `Some` for any
493// kind. After Follow-up C, the flat enum carries `sqlstate` on
494// `Server`, `Connection`, `Closed`, and `Cancelled` so callers can
495// match on it programmatically (e.g. SQLSTATE 57014 `query_canceled`
496// arrives via Cancelled and is now exposed structurally). Other
497// variants still drop SQLSTATE — folded into the message via `chain`.
498impl From<hyperdb_api_core::client::Error> for Error {
499 fn from(err: hyperdb_api_core::client::Error) -> Self {
500 use hyperdb_api_core::client::ErrorKind as CoreKind;
501
502 let chain = err.to_string();
503 let kind = err.kind();
504 let sqlstate = err.sqlstate().map(str::to_string);
505 let detail = err.detail().map(str::to_string);
506 let hint = err.hint().map(str::to_string);
507 let message = err.message().to_string();
508
509 match kind {
510 CoreKind::Connection => Error::Connection {
511 message: chain,
512 source: None,
513 sqlstate,
514 },
515 CoreKind::Authentication => Error::Authentication(chain),
516 // Use unchained `message` here: detail/hint are passed as
517 // separate fields and the `Server` Display impl re-renders
518 // them. Using `chain` would duplicate detail text.
519 CoreKind::Query => Error::Server {
520 sqlstate,
521 message,
522 detail,
523 hint,
524 },
525 CoreKind::Protocol => Error::Protocol(chain),
526 // Wire-level I/O failures are reported as Connection errors
527 // (the underlying io::Error is type-erased in core, so we
528 // cannot recover it as a typed `source` here).
529 CoreKind::Io => Error::Connection {
530 message: chain,
531 source: None,
532 sqlstate,
533 },
534 CoreKind::Config => Error::Config(chain),
535 CoreKind::Timeout => Error::Timeout(chain),
536 CoreKind::Cancelled => Error::Cancelled {
537 message: chain,
538 sqlstate,
539 },
540 CoreKind::Closed => Error::Closed {
541 message: chain,
542 sqlstate,
543 },
544 CoreKind::Conversion => Error::Conversion(chain),
545 CoreKind::FeatureNotSupported => Error::FeatureNotSupported(chain),
546 CoreKind::Other => Error::Internal { message: chain },
547 }
548 }
549}
550
551// `Infallible` is the error type for identity `TryFrom`/`TryInto`
552// conversions. Generic APIs that take `T: TryInto<U>` and bound
553// `Error: From<T::Error>` (e.g. `TableDefinition::from_table_name`)
554// require this impl to compile when callers pass a value that is
555// already the target type. The body is unreachable because
556// `Infallible` has no values.
557impl From<std::convert::Infallible> for Error {
558 fn from(_: std::convert::Infallible) -> Self {
559 unreachable!("Infallible has no values")
560 }
561}
562
563/// Result type for Hyper API operations.
564pub type Result<T> = std::result::Result<T, Error>;
565
566#[cfg(test)]
567mod tests {
568 use super::*;
569 use hyperdb_api_core::client::{Error as CoreError, ErrorKind as CoreKind};
570
571 #[test]
572 fn server_display_includes_sqlstate_detail_and_hint() {
573 let err = Error::server(
574 Some("23505".to_string()),
575 "duplicate key value violates unique constraint",
576 Some("Key (id)=(42) already exists.".to_string()),
577 Some("Choose a different key.".to_string()),
578 );
579 let s = err.to_string();
580 assert!(s.contains("server error (23505)"), "got: {s}");
581 assert!(
582 s.contains("duplicate key value violates unique constraint"),
583 "got: {s}"
584 );
585 assert!(
586 s.contains("\nDETAIL: Key (id)=(42) already exists."),
587 "got: {s}"
588 );
589 assert!(s.contains("\nHINT: Choose a different key."), "got: {s}");
590 }
591
592 #[test]
593 fn server_display_omits_missing_optional_fields() {
594 let err = Error::server(None, "syntax error at end of input", None, None);
595 let s = err.to_string();
596 assert_eq!(s, "server error: syntax error at end of input");
597 }
598
599 #[test]
600 fn from_client_error_query_does_not_duplicate_detail() {
601 // Build a client::Error with detail; client::Error::Display
602 // appends ": {detail}" inline. The flat-Error mapping must
603 // not also add "\nDETAIL: {detail}" — that would duplicate the
604 // text. We verify by counting occurrences.
605 let core = CoreError::new_with_details(
606 CoreKind::Query,
607 "duplicate key value",
608 Some("Key (id)=(42) already exists.".to_string()),
609 Some("Choose a different key.".to_string()),
610 Some("23505".to_string()),
611 );
612 let public: Error = core.into();
613 let s = public.to_string();
614 // The detail text should appear exactly once in the rendered
615 // string. (Once on the DETAIL line; not also inline in message.)
616 let count = s.matches("Key (id)=(42) already exists.").count();
617 assert_eq!(count, 1, "detail must appear exactly once; got: {s}");
618 let hint_count = s.matches("Choose a different key.").count();
619 assert_eq!(hint_count, 1, "hint must appear exactly once; got: {s}");
620 // Verify SQLSTATE is preserved.
621 assert_eq!(public.sqlstate(), Some("23505"));
622 }
623
624 #[test]
625 fn from_client_error_exhaustive_over_kinds() {
626 // Smoke test: every ErrorKind maps cleanly with no panic.
627 // (Compilation already enforces exhaustiveness.)
628 for kind in [
629 CoreKind::Connection,
630 CoreKind::Authentication,
631 CoreKind::Query,
632 CoreKind::Protocol,
633 CoreKind::Io,
634 CoreKind::Config,
635 CoreKind::Timeout,
636 CoreKind::Cancelled,
637 CoreKind::Closed,
638 CoreKind::Conversion,
639 CoreKind::FeatureNotSupported,
640 CoreKind::Other,
641 ] {
642 let core = CoreError::new(kind, "test message");
643 let public: Error = core.into();
644 // Each variant's Display must include the message text.
645 assert!(
646 public.to_string().contains("test message"),
647 "{kind:?} mapping lost the message: {public}",
648 );
649 }
650 }
651
652 #[test]
653 fn sqlstate_returns_some_for_server_connection_closed_cancelled() {
654 // Server still surfaces SQLSTATE.
655 let server = Error::server(Some("42P04".to_string()), "db exists", None, None);
656 assert_eq!(server.sqlstate(), Some("42P04"));
657
658 // Connection / Closed / Cancelled now surface SQLSTATE
659 // structurally (Follow-up C).
660 let conn = Error::connection_with_sqlstate("connect failed", "08006");
661 assert_eq!(conn.sqlstate(), Some("08006"));
662
663 let closed = Error::closed_with_sqlstate("admin shutdown", "57P01");
664 assert_eq!(closed.sqlstate(), Some("57P01"));
665
666 let cancelled = Error::cancelled_with_sqlstate("user cancel", "57014");
667 assert_eq!(cancelled.sqlstate(), Some("57014"));
668
669 // Variants without sqlstate field return None.
670 assert_eq!(Error::Conversion("...".into()).sqlstate(), None);
671 assert_eq!(
672 Error::Internal {
673 message: "...".into()
674 }
675 .sqlstate(),
676 None
677 );
678
679 // Cancelled with no SQLSTATE returns None too.
680 assert_eq!(Error::cancelled("user cancel").sqlstate(), None);
681 }
682
683 #[test]
684 fn column_display_formats_name_and_kind() {
685 let err = Error::column("user_id", ColumnErrorKind::Missing);
686 assert_eq!(err.to_string(), "column user_id: column not found");
687
688 let err = Error::column("score", ColumnErrorKind::Null);
689 assert_eq!(err.to_string(), "column score: unexpected NULL");
690
691 let err = Error::column(
692 "count",
693 ColumnErrorKind::TypeMismatch {
694 expected: "i32".into(),
695 actual: "TEXT".into(),
696 },
697 );
698 assert_eq!(
699 err.to_string(),
700 "column count: type mismatch: expected i32, got TEXT"
701 );
702 }
703
704 #[test]
705 fn column_index_out_of_bounds_display() {
706 let err = Error::column_index_out_of_bounds(5, 3);
707 assert_eq!(
708 err.to_string(),
709 "column index 5 out of bounds (row has 3 columns)"
710 );
711 }
712
713 #[test]
714 fn connection_display_with_typed_io_source() {
715 let io_err = std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "refused");
716 let err = Error::connection_with_io("connecting to hyperd", io_err);
717 let s = err.to_string();
718 // Top-level message is the prefixed form.
719 assert!(
720 s.contains("connection error: connecting to hyperd"),
721 "got: {s}"
722 );
723 // The typed source is recoverable via std::error::Error::source().
724 use std::error::Error as StdError;
725 let src = err.source().expect("connection_with_io must expose source");
726 let io_src: &std::io::Error = src
727 .downcast_ref::<std::io::Error>()
728 .expect("source must downcast to io::Error");
729 assert_eq!(io_src.kind(), std::io::ErrorKind::ConnectionRefused);
730 }
731
732 #[test]
733 fn internal_constructor_round_trip() {
734 let err = Error::internal("invariant violated");
735 assert_eq!(err.to_string(), "internal error: invariant violated");
736 }
737
738 #[test]
739 fn invalid_operation_constructor_round_trip() {
740 let err = Error::invalid_operation("cannot mix insert_data with insert_batch");
741 assert_eq!(
742 err.to_string(),
743 "invalid operation: cannot mix insert_data with insert_batch"
744 );
745 assert!(matches!(err, Error::InvalidOperation(_)));
746 }
747
748 #[test]
749 fn serialization_constructor_round_trip() {
750 let err = Error::serialization("expected value at line 1 column 1");
751 assert_eq!(
752 err.to_string(),
753 "serialization error: expected value at line 1 column 1"
754 );
755 assert!(matches!(err, Error::Serialization(_)));
756 }
757}