Skip to main content

ethereum_mysql/
error.rs

1//! Unified error type for ethereum-mysql type conversions.
2//!
3//! `SqlTypeError` provides a single error type for all conversion failures
4//! across the crate, replacing ad-hoc `&'static str` errors.
5
6use std::fmt;
7
8/// Errors that can occur when converting between Rust primitives and Ethereum SQL types.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum SqlTypeError {
11    /// Attempted to convert a negative value to an unsigned integer type.
12    NegativeValue,
13    /// Value is too large for the target type.
14    Overflow,
15    /// The input string could not be parsed into the target Ethereum type.
16    /// Wraps the underlying parse error from alloy / ruint.
17    InvalidFormat(String),
18}
19
20impl fmt::Display for SqlTypeError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            SqlTypeError::NegativeValue => {
24                write!(f, "cannot convert negative value to unsigned integer")
25            }
26            SqlTypeError::Overflow => {
27                write!(f, "value overflow — exceeds target type range")
28            }
29            SqlTypeError::InvalidFormat(msg) => {
30                write!(f, "invalid format: {msg}")
31            }
32        }
33    }
34}
35
36impl std::error::Error for SqlTypeError {}