Skip to main content

ixa/
error.rs

1//! Provides [`IxaError`] and wraps other errors.
2use std::error::Error;
3use std::io;
4
5use thiserror::Error;
6
7#[derive(Error, Debug)]
8/// Provides [`IxaError`] and maps to other errors to
9/// convert to an [`IxaError`]
10pub enum IxaError {
11    #[error(transparent)]
12    IoError(#[from] io::Error),
13    #[error(transparent)]
14    JsonError(#[from] serde_json::Error),
15    #[error(transparent)]
16    CsvError(#[from] csv::Error),
17    #[error(transparent)]
18    Utf8Error(#[from] std::string::FromUtf8Error),
19    #[error(transparent)]
20    ParseIntError(#[from] std::num::ParseIntError),
21
22    #[error("failed to encode population data for {item}: {source}")]
23    PopulationEncodeError {
24        item: String,
25        #[source]
26        source: bincode_next::error::EncodeError,
27    },
28    #[error("failed to decode population data for {item}: {source}")]
29    PopulationDecodeError {
30        item: String,
31        #[source]
32        source: bincode_next::error::DecodeError,
33    },
34    #[error("invalid population artifact: {message}")]
35    InvalidPopulation { message: String },
36
37    #[error("duplicate property {name}")]
38    DuplicateProperty { name: String },
39    #[error("entry already exists")]
40    EntryAlreadyExists,
41    #[error("no global property: {name}")]
42    NoGlobalProperty { name: String },
43    #[error("property {name} is not set")]
44    PropertyNotSet { name: String },
45
46    #[error("illegal value for global property `{name}`: {source}")]
47    IllegalGlobalPropertyValue {
48        name: String,
49        source: Box<dyn Error + Send + Sync + 'static>,
50    },
51
52    #[error(
53        "the same property appears in both position {first_index} and {second_index} in the property list"
54    )]
55    DuplicatePropertyInPropertyList {
56        first_index: usize,
57        second_index: usize,
58    },
59
60    #[error("invalid key in pair: {pair}")]
61    InvalidLogLevelKey { pair: String },
62    #[error("invalid value in pair: {pair}")]
63    InvalidLogLevelValue { pair: String },
64    #[error("invalid log level: {level}")]
65    InvalidLogLevel { level: String },
66
67    #[error("invalid log level format: {log_level}")]
68    InvalidLogLevelFormat { log_level: String },
69
70    #[error("invalid runner config section `{section}`: {message}")]
71    InvalidRunnerConfig { section: String, message: String },
72
73    #[error("cannot make edge to self")]
74    CannotMakeEdgeToSelf,
75    #[error("invalid weight")]
76    InvalidWeight,
77    #[error("edge already exists")]
78    EdgeAlreadyExists,
79    #[error("can't sample from empty list")]
80    CannotSampleFromEmptyList,
81
82    #[error("initialization list is missing required properties")]
83    MissingRequiredInitializationProperties,
84}
85
86#[cfg(test)]
87mod tests {
88    use super::IxaError;
89
90    // `anyhow::Error` requires the wrapped error to be `Send + Sync + 'static`.
91    // These tests guard against a regression where a new variant (or a boxed
92    // source) loses those bounds and breaks interop with `anyhow`.
93
94    fn assert_send_sync<T: Send + Sync + 'static>() {}
95
96    #[test]
97    fn ixa_error_is_send_sync() {
98        assert_send_sync::<IxaError>();
99    }
100
101    #[test]
102    fn ixa_error_converts_to_anyhow() {
103        fn returns_anyhow() -> anyhow::Result<()> {
104            Err(IxaError::EntryAlreadyExists)?;
105            Ok(())
106        }
107        let err = returns_anyhow().unwrap_err();
108        assert!(err.downcast_ref::<IxaError>().is_some());
109    }
110}