Skip to main content

wp_arrow/
error.rs

1use std::fmt;
2
3/// Errors that can occur during WPL-to-Arrow type conversion.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum WpArrowError {
6    /// The given type name is not a recognized WPL data type.
7    UnsupportedDataType(String),
8    /// An `array<...>` type has an invalid or missing inner type.
9    InvalidArrayInnerType(String),
10    /// A field definition has an empty name.
11    EmptyFieldName,
12    /// A required (non-nullable) field is missing from the record.
13    MissingRequiredField { field_name: String },
14    /// Wrapper for `arrow::error::ArrowError` (stored as String because ArrowError is not Clone/Eq).
15    ArrowBuildError(String),
16    /// A field value does not match the expected WpDataType.
17    ValueConversionError {
18        field_name: String,
19        expected: String,
20        actual: String,
21    },
22    /// The number of FieldDefs does not match the number of columns in the RecordBatch.
23    SchemaMismatch { expected: usize, actual: usize },
24    /// A NaiveDateTime value overflows the i64 nanosecond representation.
25    TimestampOverflow { field_name: String },
26    /// Failed to parse a string value back into the expected type.
27    ParseError { field_name: String, detail: String },
28    /// IPC encoding failed (wraps Arrow IPC writer errors).
29    IpcEncodeError(String),
30    /// IPC decoding failed (malformed frame, incomplete data, etc.).
31    IpcDecodeError(String),
32}
33
34impl fmt::Display for WpArrowError {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            WpArrowError::UnsupportedDataType(t) => {
38                write!(f, "unsupported WPL data type: {t}")
39            }
40            WpArrowError::InvalidArrayInnerType(t) => {
41                write!(f, "invalid array inner type: {t}")
42            }
43            WpArrowError::EmptyFieldName => {
44                write!(f, "field name must not be empty")
45            }
46            WpArrowError::MissingRequiredField { field_name } => {
47                write!(f, "missing required field: {field_name}")
48            }
49            WpArrowError::ArrowBuildError(msg) => {
50                write!(f, "arrow build error: {msg}")
51            }
52            WpArrowError::ValueConversionError {
53                field_name,
54                expected,
55                actual,
56            } => {
57                write!(
58                    f,
59                    "value conversion error for field '{field_name}': expected {expected}, got {actual}"
60                )
61            }
62            WpArrowError::SchemaMismatch { expected, actual } => {
63                write!(
64                    f,
65                    "schema mismatch: expected {expected} columns, got {actual}"
66                )
67            }
68            WpArrowError::TimestampOverflow { field_name } => {
69                write!(
70                    f,
71                    "timestamp overflow for field '{field_name}': value out of i64 nanosecond range"
72                )
73            }
74            WpArrowError::ParseError { field_name, detail } => {
75                write!(f, "parse error for field '{field_name}': {detail}")
76            }
77            WpArrowError::IpcEncodeError(msg) => {
78                write!(f, "IPC encode error: {msg}")
79            }
80            WpArrowError::IpcDecodeError(msg) => {
81                write!(f, "IPC decode error: {msg}")
82            }
83        }
84    }
85}
86
87impl std::error::Error for WpArrowError {}