Skip to main content

rstest_bdd/datatable/
error.rs

1//! Error types surfaced by the datatable runtime.
2
3use std::error::Error as StdError;
4
5use thiserror::Error;
6
7/// Errors that can arise when converting a Gherkin data table into typed rows.
8///
9/// `DataTableError` values surface through the generated step wrappers,
10/// ensuring that failures are reported with helpful row and column context.
11#[derive(Debug, Error)]
12#[non_exhaustive]
13pub enum DataTableError {
14    /// Raised when a typed parser expects a header row but the table is empty.
15    #[error("data table requires a header row")]
16    MissingHeader,
17    /// Raised when the header row repeats a column name.
18    #[error("data table header contains duplicate column '{column}'")]
19    DuplicateHeader {
20        /// Name of the column that appeared more than once in the header row.
21        column: String,
22    },
23    /// Raised when a row contains more or fewer cells than expected.
24    #[error("data table row {row_number} has {actual} cells but expected {expected}")]
25    UnevenRow {
26        /// 1-based index of the row that failed, including any header.
27        row_number: usize,
28        /// Number of cells required for each row.
29        expected: usize,
30        /// Number of cells present in the offending row.
31        actual: usize,
32    },
33    /// Raised when a column name lookup fails.
34    #[error("data table row {row_number} is missing column '{column}'")]
35    MissingColumn {
36        /// 1-based index of the row that failed, including any header.
37        row_number: usize,
38        /// Name of the column that was requested.
39        column: String,
40    },
41    /// Raised when positional access references an out-of-range column index.
42    #[error("data table row {row_number} is missing cell {column_index}")]
43    MissingCell {
44        /// 1-based index of the row that failed, including any header.
45        row_number: usize,
46        /// 1-based index of the missing column.
47        column_index: usize,
48    },
49    /// Raised when parsing an entire row fails.
50    #[error("row {row_number}: {source}")]
51    RowParse {
52        /// 1-based index of the row that failed, including any header.
53        row_number: usize,
54        /// Root cause reported by the row parser.
55        #[source]
56        source: Box<dyn StdError + Send + Sync>,
57    },
58    /// Raised when parsing an individual cell fails.
59    #[error("row {row_number}, column {column_index}{column_label}: {source}")]
60    CellParse {
61        /// 1-based index of the row that failed, including any header.
62        row_number: usize,
63        /// 1-based index of the column that failed to parse.
64        column_index: usize,
65        /// Display-formatted column label used when a header was present.
66        column_label: String,
67        /// Root cause reported by the cell parser.
68        #[source]
69        source: Box<dyn StdError + Send + Sync>,
70    },
71    /// Raised when cached table access fails (for example, due to a poisoned lock).
72    #[error("failed to access cached data table")]
73    CacheLockFailure,
74}
75
76impl DataTableError {
77    /// Construct a cell-parse error with a human-readable column label.
78    pub(crate) fn cell_parse<E>(
79        row_number: usize,
80        column_index: usize,
81        column_label: Option<String>,
82        err: E,
83    ) -> Self
84    where
85        E: StdError + Send + Sync + 'static,
86    {
87        let column_label = column_label
88            .map(|label| format!(" ({label})"))
89            .unwrap_or_default();
90        Self::CellParse {
91            row_number,
92            column_index: column_index + 1,
93            column_label,
94            source: Box::new(err),
95        }
96    }
97}