1
//! Declare error types.
2

            
3
use std::path::PathBuf;
4

            
5
use tor_basic_utils::PathExt as _;
6
use tor_error::{ErrorKind, HasKind};
7

            
8
/// An error related to an option passed to Arti via a configuration
9
/// builder.
10
//
11
// API NOTE: When possible, we should expose this error type rather than
12
// wrapping it in `TorError`. It can provide specific information about  what
13
// part of the configuration was invalid.
14
//
15
// This is part of the public API.
16
#[derive(Debug, Clone, thiserror::Error)]
17
#[non_exhaustive]
18
pub enum ConfigBuildError {
19
    /// A mandatory field was not present.
20
    #[error("Field was not provided: {field}")]
21
    MissingField {
22
        /// The name of the missing field.
23
        field: String,
24
    },
25
    /// A single field had a value that proved to be unusable.
26
    #[error("Value of {field} was incorrect: {problem}")]
27
    Invalid {
28
        /// The name of the invalid field
29
        field: String,
30
        /// A description of the problem.
31
        problem: String,
32
    },
33
    /// Multiple fields are inconsistent.
34
    #[error("Fields {fields:?} are inconsistent: {problem}")]
35
    Inconsistent {
36
        /// The names of the inconsistent fields
37
        fields: Vec<String>,
38
        /// The problem that makes them inconsistent
39
        problem: String,
40
    },
41
    /// The requested configuration is not supported in this build
42
    #[error("Field {field:?} specifies a configuration not supported in this build: {problem}")]
43
    // TODO should we report the cargo feature, if applicable?  And if so, of `arti`
44
    // or of the underlying crate?  This seems like a can of worms.
45
    NoCompileTimeSupport {
46
        /// The names of the (primary) field requesting the unsupported configuration
47
        field: String,
48
        /// The description of the problem
49
        problem: String,
50
    },
51
}
52

            
53
impl From<derive_builder::UninitializedFieldError> for ConfigBuildError {
54
4
    fn from(val: derive_builder::UninitializedFieldError) -> Self {
55
4
        ConfigBuildError::MissingField {
56
4
            field: val.field_name().to_string(),
57
4
        }
58
4
    }
59
}
60

            
61
impl From<derive_builder::SubfieldBuildError<ConfigBuildError>> for ConfigBuildError {
62
2
    fn from(e: derive_builder::SubfieldBuildError<ConfigBuildError>) -> Self {
63
2
        let (field, problem) = e.into_parts();
64
2
        problem.within(field)
65
2
    }
66
}
67

            
68
impl ConfigBuildError {
69
    /// Return a new ConfigBuildError that prefixes its field name with
70
    /// `prefix` and a dot.
71
    #[must_use]
72
559
    pub fn within(&self, prefix: &str) -> Self {
73
        use ConfigBuildError::*;
74
880
        let addprefix = |field: &str| format!("{}.{}", prefix, field);
75
559
        match self {
76
6
            MissingField { field } => MissingField {
77
6
                field: addprefix(field),
78
6
            },
79
2
            Invalid { field, problem } => Invalid {
80
2
                field: addprefix(field),
81
2
                problem: problem.clone(),
82
2
            },
83
551
            Inconsistent { fields, problem } => Inconsistent {
84
868
                fields: fields.iter().map(|f| addprefix(f)).collect(),
85
551
                problem: problem.clone(),
86
            },
87
            NoCompileTimeSupport { field, problem } => NoCompileTimeSupport {
88
                field: addprefix(field),
89
                problem: problem.clone(),
90
            },
91
        }
92
559
    }
93
}
94

            
95
impl HasKind for ConfigBuildError {
96
    fn kind(&self) -> ErrorKind {
97
        ErrorKind::InvalidConfig
98
    }
99
}
100

            
101
/// An error caused when attempting to reconfigure an existing Arti client, or one of its modules.
102
#[derive(Debug, Clone, thiserror::Error)]
103
#[non_exhaustive]
104
pub enum ReconfigureError {
105
    /// Tried to change a field that cannot change on a running client.
106
    #[error("Cannot change {field} on a running client.")]
107
    CannotChange {
108
        /// The field (or fields) that we tried to change.
109
        field: String,
110
    },
111

            
112
    /// The requested configuration is not supported in this situation
113
    ///
114
    /// Something, probably discovered at runtime, is not compatible with
115
    /// the specified configuration.
116
    ///
117
    /// This ought *not* to be returned when the configuration is simply not supported
118
    /// by this build of arti -
119
    /// that should be reported at config build type as `ConfigBuildError::Unsupported`.
120
    #[error("Configuration not supported in this situation: {0}")]
121
    UnsupportedSituation(String),
122

            
123
    /// There was a programming error somewhere in our code, or the calling code.
124
    #[error("Programming error")]
125
    Bug(#[from] tor_error::Bug),
126
}
127

            
128
impl HasKind for ReconfigureError {
129
    fn kind(&self) -> ErrorKind {
130
        ErrorKind::InvalidConfigTransition
131
    }
132
}
133

            
134
/// An error that occurs while trying to read and process our configuration.
135
#[derive(Debug, Clone, thiserror::Error)]
136
#[non_exhaustive]
137
pub enum ConfigError {
138
    /// We encoundered a problem checking file permissions (for example, no such file)
139
    #[error("Problem accessing configuration file(s)")]
140
    FileAccess(#[source] fs_mistrust::Error),
141
    /// We encoundered a problem checking file permissions (for example, no such file)
142
    ///
143
    /// This variant name is misleading - see the docs for [`fs_mistrust::Error`].
144
    /// Please use [`ConfigError::FileAccess`] instead.
145
    #[deprecated = "use ConfigError::FileAccess instead"]
146
    #[error("Problem accessing configuration file(s)")]
147
    Permissions(#[source] fs_mistrust::Error),
148
    /// Our underlying configuration library gave an error while loading our
149
    /// configuration.
150
    #[error("Couldn't load configuration")]
151
    Load(#[source] ConfigLoadError),
152
    /// Encountered an IO error with a configuration file or directory.
153
    ///
154
    /// Note that some IO errors may be reported as `Load` errors,
155
    /// due to limitations of the underlying library.
156
    #[error("IoError while {} {}", action, path.display_lossy())]
157
    Io {
158
        /// The action while we were trying to perform
159
        action: &'static str,
160
        /// The path we were trying to do it to.
161
        path: PathBuf,
162
        /// The underlying problem
163
        #[source]
164
        err: std::sync::Arc<std::io::Error>,
165
    },
166
}
167

            
168
/// An error that occurred while trying to look up a configuration value.
169
#[derive(Clone, Debug, thiserror::Error)]
170
#[non_exhaustive]
171
pub enum ConfigGetValueError {
172
    /// Some internal error occurred.
173
    #[error("Internal error")]
174
    Bug(#[from] tor_error::Bug),
175
}
176

            
177
/// Wrapper for our an error type from our underlying configuration library.
178
#[derive(Debug, Clone)]
179
pub struct ConfigLoadError(figment::Error);
180

            
181
impl ConfigError {
182
    /// Wrap `err` as a ConfigError.
183
    ///
184
    /// This is not a From implementation, since we don't want to expose our
185
    /// underlying configuration library.
186
8
    pub(crate) fn from_cfg_err(err: figment::Error) -> Self {
187
        // TODO: It would be lovely to extract IO errors from figment::Error
188
        // and report them as Error::Io.  Unfortunately, it doesn't seem
189
        // possible to do that given the design of figment::Error.
190
8
        ConfigError::Load(ConfigLoadError(err))
191
8
    }
192
}
193

            
194
impl std::fmt::Display for ConfigLoadError {
195
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
196
        let s = self.0.to_string();
197
        write!(f, "{}", s)?;
198
        if s.contains("invalid escape") || s.contains("invalid hex escape") {
199
            write!(
200
                f,
201
                "   (If you wanted to include a literal \\ character, you need to escape it by writing two in a row: \\\\)"
202
            )?;
203
        }
204
        Ok(())
205
    }
206
}
207

            
208
impl std::error::Error for ConfigLoadError {
209
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
210
        // A `ConfigLoadError` isn't really a new higher-level error,
211
        // it just wraps an existing figment error and formats it a little differently.
212
        // Our `Display` implementation writes the `self.0` error message,
213
        // so here in `source()` we skip `self.0` and return *its* source error.
214
        // Otherwise an error formatter which iterates over error sources would print the same
215
        // error message twice.
216
        self.0.source()
217
    }
218
}
219

            
220
#[cfg(test)]
221
mod test {
222
    // @@ begin test lint list maintained by maint/add_warning @@
223
    #![allow(clippy::bool_assert_comparison)]
224
    #![allow(clippy::clone_on_copy)]
225
    #![allow(clippy::dbg_macro)]
226
    #![allow(clippy::mixed_attributes_style)]
227
    #![allow(clippy::print_stderr)]
228
    #![allow(clippy::print_stdout)]
229
    #![allow(clippy::single_char_pattern)]
230
    #![allow(clippy::unwrap_used)]
231
    #![allow(clippy::unchecked_time_subtraction)]
232
    #![allow(clippy::useless_vec)]
233
    #![allow(clippy::needless_pass_by_value)]
234
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
235
    use super::*;
236

            
237
    #[test]
238
    fn within() {
239
        let e1 = ConfigBuildError::MissingField {
240
            field: "lettuce".to_owned(),
241
        };
242
        let e2 = ConfigBuildError::Invalid {
243
            field: "tomato".to_owned(),
244
            problem: "too crunchy".to_owned(),
245
        };
246
        let e3 = ConfigBuildError::Inconsistent {
247
            fields: vec!["mayo".to_owned(), "avocado".to_owned()],
248
            problem: "pick one".to_owned(),
249
        };
250

            
251
        assert_eq!(
252
            &e1.within("sandwich").to_string(),
253
            "Field was not provided: sandwich.lettuce"
254
        );
255
        assert_eq!(
256
            &e2.within("sandwich").to_string(),
257
            "Value of sandwich.tomato was incorrect: too crunchy"
258
        );
259
        assert_eq!(
260
            &e3.within("sandwich").to_string(),
261
            r#"Fields ["sandwich.mayo", "sandwich.avocado"] are inconsistent: pick one"#
262
        );
263
    }
264

            
265
    #[derive(derive_builder::Builder, Debug, Clone)]
266
    #[builder(build_fn(error = "ConfigBuildError"))]
267
    #[allow(dead_code)]
268
    struct Cephalopod {
269
        // arms have suction cups for their whole length
270
        arms: u8,
271
        // Tentacles have suction cups at the ends
272
        tentacles: u8,
273
    }
274

            
275
    #[test]
276
    fn build_err() {
277
        let squid = CephalopodBuilder::default().arms(8).tentacles(2).build();
278
        let octopus = CephalopodBuilder::default().arms(8).build();
279
        assert!(squid.is_ok());
280
        let squid = squid.unwrap();
281
        assert_eq!(squid.arms, 8);
282
        assert_eq!(squid.tentacles, 2);
283
        assert!(octopus.is_err());
284
        assert_eq!(
285
            &octopus.unwrap_err().to_string(),
286
            "Field was not provided: tentacles"
287
        );
288
    }
289

            
290
    #[derive(derive_builder::Builder, Debug)]
291
    #[builder(build_fn(error = "ConfigBuildError"))]
292
    #[allow(dead_code)]
293
    struct Pet {
294
        #[builder(sub_builder)]
295
        best_friend: Cephalopod,
296
    }
297

            
298
    #[test]
299
    fn build_subfield_err() {
300
        let mut petb = PetBuilder::default();
301
        petb.best_friend().tentacles(3);
302
        let pet = petb.build();
303
        assert_eq!(
304
            pet.unwrap_err().to_string(),
305
            "Field was not provided: best_friend.arms"
306
        );
307
    }
308
}