dataexcept 0.1.0-beta.2

Structured errors and failure observability for data, ML, and data-engineering systems
Documentation
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Typed data-ingestion and validation errors.
//!
//! These types mirror the stable concepts exposed by the Python `DataExcept`
//! project while using ordinary Rust structs and `std::error::Error`.
//!
//! Conversion into [`DataError`] preserves the language-neutral envelope type
//! names and public attributes used by the Python implementation.

#![allow(clippy::module_name_repetitions)]

use std::{error::Error, fmt};

use serde_json::{Value, json};

use crate::{DataError, FailureMetadata, redaction::redact_if_url};

const MODULE: &str = "dataexcept::data";

/// Raised when loading data from a source fails.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataLoadingError {
    /// File path, URL, or other source description.
    pub source: String,
    /// Human-readable description of the underlying failure.
    pub original: String,
}

impl DataLoadingError {
    /// Creates a data-loading error.
    #[must_use]
    pub fn new(source: impl Into<String>, original: impl Into<String>) -> Self {
        let source = source.into();

        Self {
            source: redact_if_url(&source, true),
            original: original.into(),
        }
    }

    /// Creates a data-loading error from an existing Rust error.
    #[must_use]
    pub fn from_error(source: impl Into<String>, error: &(impl Error + ?Sized)) -> Self {
        Self::new(source, error.to_string())
    }
}

impl fmt::Display for DataLoadingError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "Failed to load data from '{}': {}",
            self.source, self.original
        )
    }
}

impl Error for DataLoadingError {}

impl From<DataLoadingError> for DataError {
    fn from(value: DataLoadingError) -> Self {
        let message = value.to_string();

        Self::new("DataLoadingError", message)
            .with_module(MODULE)
            .with_attribute("source", json!(value.source))
            .with_attribute("original", json!(value.original))
            .with_failure(FailureMetadata::unknown())
    }
}

/// Raised when input data has an unexpected format.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataFormatError {
    /// Allowed format identifiers.
    pub expected_formats: Vec<String>,
    /// Format that was actually observed.
    pub found_format: String,
}

impl DataFormatError {
    /// Creates a data-format error.
    #[must_use]
    pub fn new<I, S>(expected_formats: I, found_format: impl Into<String>) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            expected_formats: expected_formats.into_iter().map(Into::into).collect(),
            found_format: found_format.into(),
        }
    }
}

impl fmt::Display for DataFormatError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "Expected data format {}; got {}",
            self.expected_formats.join(", "),
            self.found_format
        )
    }
}

impl Error for DataFormatError {}

impl From<DataFormatError> for DataError {
    fn from(value: DataFormatError) -> Self {
        let message = value.to_string();

        Self::new("DataFormatError", message)
            .with_module(MODULE)
            .with_attribute("expected_formats", json!(value.expected_formats))
            .with_attribute("found_format", json!(value.found_format))
            .with_failure(FailureMetadata::unknown())
    }
}

/// Raised when a field or value fails a validation rule.
#[derive(Debug, Clone, PartialEq)]
pub struct DataValidationError {
    /// Name of the invalid field.
    pub field: String,
    /// Invalid value in a JSON-safe representation.
    pub value: Value,
    message: String,
}

impl DataValidationError {
    /// Creates a validation error using the standard `DataExcept` message.
    #[must_use]
    pub fn new(field: impl Into<String>, value: Value) -> Self {
        let field = field.into();
        let message = format!("Invalid value for '{field}': {value}");

        Self {
            field,
            value,
            message,
        }
    }

    /// Creates a validation error with a caller-supplied message.
    #[must_use]
    pub fn with_message(
        field: impl Into<String>,
        value: Value,
        message: impl Into<String>,
    ) -> Self {
        Self {
            field: field.into(),
            value,
            message: message.into(),
        }
    }

    /// Returns the rendered validation message.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }
}

impl fmt::Display for DataValidationError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl Error for DataValidationError {}

impl From<DataValidationError> for DataError {
    fn from(value: DataValidationError) -> Self {
        Self::new("DataValidationError", value.message)
            .with_module(MODULE)
            .with_attribute("field", json!(value.field))
            .with_attribute("value", value.value)
            .with_failure(FailureMetadata::unknown())
    }
}

/// Raised when a required feature or value is missing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MissingDataError {
    /// Name of the missing feature.
    pub feature: String,
    message: String,
}

impl MissingDataError {
    /// Creates a missing-data error using the standard `DataExcept` message.
    #[must_use]
    pub fn new(feature: impl Into<String>) -> Self {
        let feature = feature.into();
        let message = format!("Missing required feature: '{feature}'");

        Self { feature, message }
    }

    /// Creates a missing-data error with a caller-supplied message.
    #[must_use]
    pub fn with_message(feature: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            feature: feature.into(),
            message: message.into(),
        }
    }

    /// Returns the rendered missing-data message.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }
}

impl fmt::Display for MissingDataError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl Error for MissingDataError {}

impl From<MissingDataError> for DataError {
    fn from(value: MissingDataError) -> Self {
        Self::new("MissingDataError", value.message)
            .with_module(MODULE)
            .with_attribute("feature", json!(value.feature))
            .with_failure(FailureMetadata::unknown())
    }
}

/// Raised when an observed schema differs from the expected schema.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SchemaMismatchError {
    /// Expected schema description.
    pub expected: String,
    /// Observed schema description.
    pub found: String,
}

impl SchemaMismatchError {
    /// Creates a schema-mismatch error.
    #[must_use]
    pub fn new(expected: impl Into<String>, found: impl Into<String>) -> Self {
        Self {
            expected: expected.into(),
            found: found.into(),
        }
    }
}

impl fmt::Display for SchemaMismatchError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "Schema mismatch. Expected: {}, Found: {}",
            self.expected, self.found
        )
    }
}

impl Error for SchemaMismatchError {}

impl From<SchemaMismatchError> for DataError {
    fn from(value: SchemaMismatchError) -> Self {
        let message = value.to_string();

        Self::new("SchemaMismatchError", message)
            .with_module(MODULE)
            .with_attribute("expected", json!(value.expected))
            .with_attribute("found", json!(value.found))
            .with_failure(FailureMetadata::unknown())
    }
}

/// Raised when a required tabular column is missing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MissingColumnError {
    /// Missing column name.
    pub column: String,
    /// Optional `DataFrame` or table name.
    pub dataframe: Option<String>,
}

impl MissingColumnError {
    /// Creates a missing-column error.
    #[must_use]
    pub fn new(column: impl Into<String>, dataframe: Option<impl Into<String>>) -> Self {
        Self {
            column: column.into(),
            dataframe: dataframe.map(Into::into),
        }
    }
}

impl fmt::Display for MissingColumnError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "Missing required column '{}'", self.column)?;
        if let Some(dataframe) = &self.dataframe {
            write!(formatter, " in DataFrame '{dataframe}'")?;
        }
        Ok(())
    }
}

impl Error for MissingColumnError {}

impl From<MissingColumnError> for DataError {
    fn from(value: MissingColumnError) -> Self {
        let message = value.to_string();
        let mut error = Self::new("MissingColumnError", message)
            .with_module(MODULE)
            .with_attribute("column", json!(value.column))
            .with_failure(FailureMetadata::unknown());

        if let Some(dataframe) = value.dataframe {
            error = error.with_attribute("dataframe", json!(dataframe));
        }

        error
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::{
        DataFormatError, DataLoadingError, DataValidationError, MissingColumnError,
        MissingDataError, SchemaMismatchError,
    };
    use crate::{DataError, FailureKind};

    #[test]
    fn loading_error_redacts_source_credentials() {
        let error = DataLoadingError::new(
            "https://user:secret@example.com/data.csv?token=SECRETVALUE",
            "request failed",
        );

        assert!(!error.source.contains("secret"));
        assert!(!error.source.contains("SECRETVALUE"));
        assert!(error.source.contains("example.com"));
        assert!(error.source.contains("/data.csv"));
    }

    #[test]
    fn format_error_preserves_expected_formats() {
        let error = DataFormatError::new(["csv", "parquet"], "xml");

        assert_eq!(
            error.to_string(),
            "Expected data format csv, parquet; got xml"
        );

        let structured = DataError::from(error);
        let envelope = structured.to_envelope();

        assert_eq!(envelope.error_type, "DataFormatError");
        assert_eq!(
            envelope.attributes.expect("attributes should exist")["expected_formats"],
            json!(["csv", "parquet"])
        );
    }

    #[test]
    fn validation_error_matches_python_envelope_names() {
        let error = DataValidationError::new("age", json!(-1));
        let structured = DataError::from(error);
        let envelope = structured.to_envelope();

        assert_eq!(envelope.error_type, "DataValidationError");
        assert_eq!(envelope.module, "dataexcept::data");
        assert_eq!(
            envelope.attributes.expect("attributes should exist")["field"],
            "age"
        );
        assert_eq!(
            envelope
                .failure
                .expect("failure metadata should exist")
                .kind,
            FailureKind::Unknown
        );
    }

    #[test]
    fn missing_data_has_standard_message() {
        let error = MissingDataError::new("income");
        assert_eq!(error.to_string(), "Missing required feature: 'income'");
    }

    #[test]
    fn schema_mismatch_has_stable_attributes() {
        let error = SchemaMismatchError::new("id: integer", "id: string");
        let envelope = DataError::from(error).to_envelope();
        let attributes = envelope.attributes.expect("attributes should exist");

        assert_eq!(attributes["expected"], "id: integer");
        assert_eq!(attributes["found"], "id: string");
    }

    #[test]
    fn missing_column_carries_optional_dataframe() {
        let error = MissingColumnError::new("customer_id", Some("sales"));
        assert_eq!(
            error.to_string(),
            "Missing required column 'customer_id' in DataFrame 'sales'"
        );

        let envelope = DataError::from(error).to_envelope();
        assert_eq!(
            envelope.attributes.expect("attributes should exist")["dataframe"],
            "sales"
        );
    }
}