|
| 1 | +//! Errors the native config stages report exactly as the Python stages raise them. |
| 2 | +
|
| 3 | +/// The Python exception class one error raises. |
| 4 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 5 | +pub enum ErrorClass { |
| 6 | + /// `CompileInputError`. |
| 7 | + CompileInput, |
| 8 | + /// `ConfigValueTypeError` for a config key that must hold a string. |
| 9 | + ConfigValueType, |
| 10 | + /// `ResourceIdentityError` for an identity that is not canonical snake_case. |
| 11 | + ResourceIdentity, |
| 12 | + /// `DiscoveryConflictError` for equally specific path-default keys. |
| 13 | + DiscoveryConflict, |
| 14 | +} |
| 15 | + |
| 16 | +/// One error with the class, message, code and help the Python stage raises. |
| 17 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 18 | +pub struct ConfigError { |
| 19 | + pub class: ErrorClass, |
| 20 | + /// The message; empty for `ConfigValueTypeError`, which Python words from the value's type. |
| 21 | + pub message: String, |
| 22 | + /// The diagnostic code when it is not the class default. |
| 23 | + pub code: Option<&'static str>, |
| 24 | + pub help: Option<String>, |
| 25 | + /// The config key a `ConfigValueTypeError` names. |
| 26 | + pub key: Option<String>, |
| 27 | +} |
| 28 | + |
| 29 | +impl ConfigError { |
| 30 | + /// A `CompileInputError` with this message. |
| 31 | + pub fn compile(message: String) -> Self { |
| 32 | + Self { |
| 33 | + class: ErrorClass::CompileInput, |
| 34 | + message, |
| 35 | + code: None, |
| 36 | + help: None, |
| 37 | + key: None, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// The `ConfigValueTypeError` `get_config_str` raises for a non-string `key`. |
| 42 | + pub fn config_value_type(key: &str) -> Self { |
| 43 | + Self { |
| 44 | + class: ErrorClass::ConfigValueType, |
| 45 | + message: String::new(), |
| 46 | + code: None, |
| 47 | + help: None, |
| 48 | + key: Some(key.to_owned()), |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// This error of another class. |
| 53 | + pub fn with_class(mut self, class: ErrorClass) -> Self { |
| 54 | + self.class = class; |
| 55 | + self |
| 56 | + } |
| 57 | + |
| 58 | + /// This error with help text. |
| 59 | + pub fn with_help(mut self, help: impl Into<String>) -> Self { |
| 60 | + self.help = Some(help.into()); |
| 61 | + self |
| 62 | + } |
| 63 | + |
| 64 | + /// This error with a diagnostic code. |
| 65 | + pub fn with_code(mut self, code: &'static str) -> Self { |
| 66 | + self.code = Some(code); |
| 67 | + self |
| 68 | + } |
| 69 | +} |
0 commit comments