ffx_command_error/
error.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use errors::FfxError;
6
7/// Represents a recoverable error. Intended to be embedded in `Error`.
8#[derive(thiserror::Error, Debug)]
9#[error("non-fatal error encountered: {}", .0)]
10pub struct NonFatalError(#[source] pub anyhow::Error);
11
12/// A top level error type for ffx tool results
13#[derive(thiserror::Error, Debug)]
14pub enum Error {
15    /// An error that qualifies as a bugcheck
16    Unexpected(#[source] anyhow::Error),
17    /// A known kind of error that can be reported usefully to the user
18    User(#[source] anyhow::Error),
19    /// An early-exit that should result in outputting help to the user (like [`argh::EarlyExit`]),
20    /// but is not itself an error in any meaningful sense.
21    Help {
22        /// The command name (argv[0..]) that should be used in supplemental help output
23        command: Vec<String>,
24        /// The text to output to the user
25        output: String,
26        /// The exit status
27        code: i32,
28    },
29    /// Something failed before ffx's configuration could be loaded (like an
30    /// invalid argument, a failure to read an env config file, etc).
31    ///
32    /// Errors of this type should include any information the user might need
33    /// to recover from the issue, because it will not advise the user to look
34    /// in the log files or anything like that.
35    Config(#[source] anyhow::Error),
36    /// Exit with a specific error code but no output
37    ExitWithCode(i32),
38}
39
40impl Error {
41    /// Attempts to downcast this error into something non-fatal, returning `Ok(e)`
42    /// if able to downcast to something non-fatal, else returning the original error.
43    pub fn downcast_non_fatal(self) -> Result<anyhow::Error, Self> {
44        fn try_downcast(err: anyhow::Error) -> Result<anyhow::Error, anyhow::Error> {
45            match err.downcast::<NonFatalError>() {
46                Ok(NonFatalError(e)) => Ok(e),
47                Err(e) => Err(e),
48            }
49        }
50
51        match self {
52            Self::Help { .. } | Self::ExitWithCode(_) => Err(self),
53            Self::User(e) => try_downcast(e).map_err(Self::User),
54            Self::Unexpected(e) => try_downcast(e).map_err(Self::Unexpected),
55            Self::Config(e) => try_downcast(e).map_err(Self::Config),
56        }
57    }
58
59    /// Attempts to get the original `anyhow::Error` source (this is useful for chaining context
60    /// errors). If successful, returns `Ok(e)` with the error source, but if there's no error
61    /// source that can be returned, returns `self`.
62    pub fn source(self) -> Result<anyhow::Error, Self> {
63        match self {
64            Self::User(e) | Self::Unexpected(e) | Self::Config(e) => Ok(e),
65            Self::Help { .. } | Self::ExitWithCode(_) => Err(self),
66        }
67    }
68}
69
70/// Writes a detailed description of an anyhow error to the formatter
71fn write_detailed(f: &mut std::fmt::Formatter<'_>, error: &anyhow::Error) -> std::fmt::Result {
72    write!(f, "Error: {}", error)?;
73    for (i, e) in error.chain().skip(1).enumerate() {
74        write!(f, "\n  {: >3}.  {}", i + 1, e)?;
75    }
76    Ok(())
77}
78
79fn write_display(f: &mut std::fmt::Formatter<'_>, error: &anyhow::Error) -> std::fmt::Result {
80    write!(f, "{error}")?;
81    let mut previous_error = error.to_string();
82    for e in error.chain().skip(1) {
83        // This is a total hack. When errors are chained together through various thiserror
84        // wrappers, what can happen is the error will use this display function to make itself
85        // into a string, and the display function will show duplicates of the context chain.
86        //
87        // If, for example, we have something like `ffx_bail!` which returns an error, and it is
88        // encapsulated into a `thiserror` enum, and then later wrapped into a
89        // `ffx_command::Error::User`, we will have a context chain with the same error multiple
90        // times in a row. For example, say we have something like:
91        //
92        // ```
93        // let err = ffx_error!(anyhow!("this thing broke"));
94        // let err2 = LogError::FfxError(err);
95        // let err3 = ffx_command::Error::User(err2);
96        // eprintln!("{err3}");
97        // ```
98        //
99        // This will print: "this thing broke: this thing broke"
100        //
101        // This check will prevent that from happening without removing the context chain.
102        let err_string = format!("{}", e);
103        // There have been issues with empty strings in the past when formatting errors. Make
104        // sure to explicitly show that an empty string is in one of the errors so that it can
105        // be caught. This sort of thing used to happen with certain SSH errors.
106        let err_string = if err_string.is_empty() { "\"\"".to_owned() } else { err_string };
107        if err_string == previous_error {
108            continue;
109        }
110        write!(f, ": {}", err_string)?;
111        previous_error = err_string;
112    }
113    Ok(())
114}
115
116const BUG_LINE: &str = "BUG: An internal command error occurred.";
117impl std::fmt::Display for Error {
118    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119        match self {
120            Self::Unexpected(error) => {
121                writeln!(f, "{BUG_LINE}")?;
122                write_detailed(f, error)
123            }
124            Self::User(error) | Self::Config(error) => write_display(f, error),
125            Self::Help { output, .. } => write!(f, "{output}"),
126            Self::ExitWithCode(code) => write!(f, "Exiting with code {code}"),
127        }
128    }
129}
130
131impl From<anyhow::Error> for Error {
132    fn from(error: anyhow::Error) -> Self {
133        // If it's already an Error, just return it
134        match error.downcast::<Self>() {
135            Ok(this) => this,
136            // this is just a compatibility shim to extract information out of the way
137            // we've traditionally divided user and unexpected errors.
138            Err(error) => match error.downcast::<FfxError>() {
139                Ok(err) => Self::User(err.into()),
140                Err(err) => Self::Unexpected(err),
141            },
142        }
143    }
144}
145
146impl From<FfxError> for Error {
147    fn from(error: FfxError) -> Self {
148        Error::User(error.into())
149    }
150}
151
152impl Error {
153    /// Map an argh early exit to our kind of error
154    pub fn from_early_exit(command: &[impl AsRef<str>], early_exit: argh::EarlyExit) -> Self {
155        let command = Vec::from_iter(command.iter().map(|s| s.as_ref().to_owned()));
156        let output = early_exit.output;
157        // if argh's early_exit status is Ok() that means it's printing help because
158        // of a `--help` argument or `help` as a subcommand was passed. Otherwise
159        // it's just an error parsing the arguments. So only map `status: Ok(())`
160        // as help output.
161        match early_exit.status {
162            Ok(_) => Error::Help { command, output, code: 0 },
163            Err(_) => Error::Config(anyhow::anyhow!("{}", output)),
164        }
165    }
166
167    /// Get the exit code this error should correspond to if it bubbles up to `main()`
168    pub fn exit_code(&self) -> i32 {
169        match self {
170            Error::User(err) => {
171                if let Some(FfxError::Error(_, code)) = err.downcast_ref() {
172                    *code
173                } else {
174                    1
175                }
176            }
177            Error::Help { code, .. } => *code,
178            Error::ExitWithCode(code) => *code,
179            _ => 1,
180        }
181    }
182}
183
184/// A convenience Result type
185pub type Result<T, E = crate::Error> = core::result::Result<T, E>;
186
187#[cfg(test)]
188mod tests {
189    use super::*;
190    use crate::tests::*;
191    use anyhow::anyhow;
192    use assert_matches::assert_matches;
193    use errors::{ffx_error, ffx_error_with_code, IntoExitCode};
194    use std::io::{Cursor, Write};
195
196    #[test]
197    fn test_write_result_ffx_error() {
198        let err = Error::from(ffx_error!(FFX_STR));
199        let mut cursor = Cursor::new(Vec::new());
200
201        assert_matches!(write!(&mut cursor, "{err}"), Ok(_));
202
203        assert!(String::from_utf8(cursor.into_inner()).unwrap().contains(FFX_STR));
204    }
205
206    #[test]
207    fn into_error_from_arbitrary_is_unexpected() {
208        let err = anyhow!(ERR_STR);
209        assert_matches!(
210            Error::from(err),
211            Error::Unexpected(_),
212            "an arbitrary anyhow error should convert to an 'unexpected' bug check error"
213        );
214    }
215
216    #[test]
217    fn into_error_from_ffx_error_is_user_error() {
218        let err = FfxError::Error(anyhow!(FFX_STR), 1);
219        assert_matches!(
220            Error::from(err),
221            Error::User(_),
222            "an arbitrary anyhow error should convert to a 'user' error"
223        );
224    }
225
226    #[test]
227    fn into_error_from_contextualized_ffx_error_prints_original_error() {
228        let err = Error::from(anyhow::anyhow!(errors::ffx_error!(FFX_STR)).context("boom"));
229        assert_eq!(
230            &format!("{err}"),
231            FFX_STR,
232            "an anyhow error with context should print the original error, not the context, when stringified."
233        );
234    }
235
236    #[test]
237    fn test_write_result_arbitrary_error() {
238        let err = Error::from(anyhow!(ERR_STR));
239        let mut cursor = Cursor::new(Vec::new());
240
241        assert_matches!(write!(&mut cursor, "{err}"), Ok(_));
242
243        let err_str = String::from_utf8(cursor.into_inner()).unwrap();
244        assert!(err_str.contains(BUG_LINE));
245        assert!(err_str.contains(ERR_STR));
246    }
247
248    #[test]
249    fn test_result_ext_exit_code_ffx_error() {
250        let err = Result::<()>::Err(Error::from(ffx_error_with_code!(42, FFX_STR)));
251        assert_eq!(err.exit_code(), 42);
252    }
253
254    #[test]
255    fn test_from_ok_early_exit() {
256        let command = ["testing", "--help"];
257        let output = "stuff!".to_owned();
258        let status = Ok(());
259        let code = 0;
260
261        let early_exit = argh::EarlyExit { output: output.clone(), status };
262        let err = Error::from_early_exit(&command, early_exit);
263        assert_eq!(err.exit_code(), code);
264        assert_matches!(err, Error::Help { command: error_command, output: error_output, code: error_code } if error_command == command && error_output == output && error_code == code);
265    }
266
267    #[test]
268    fn test_from_error_early_exit() {
269        let command = ["testing", "bad", "command"];
270        let output = "stuff!".to_owned();
271        let status = Err(());
272        let code = 1;
273
274        let early_exit = argh::EarlyExit { output: output.clone(), status };
275        let err = Error::from_early_exit(&command, early_exit);
276        assert_eq!(err.exit_code(), code);
277        assert_matches!(err, Error::Config(err) if format!("{err}") == output);
278    }
279
280    #[test]
281    fn test_downcast_recasts_types() {
282        let err = Error::User(anyhow!("boom"));
283        assert_matches!(err.downcast_non_fatal(), Err(Error::User(_)));
284
285        let err = Error::Unexpected(anyhow!("boom"));
286        assert_matches!(err.downcast_non_fatal(), Err(Error::Unexpected(_)));
287
288        let err = Error::Config(anyhow!("boom"));
289        assert_matches!(err.downcast_non_fatal(), Err(Error::Config(_)));
290
291        let err =
292            Error::Help { command: vec!["foobar".to_owned()], output: "blorp".to_owned(), code: 1 };
293        assert_matches!(err.downcast_non_fatal(), Err(Error::Help { .. }));
294
295        let err = Error::ExitWithCode(2);
296        assert_matches!(err.downcast_non_fatal(), Err(Error::ExitWithCode(2)));
297    }
298
299    #[test]
300    fn test_downcast_non_fatal_recovers_non_fatal_error() {
301        static ERR_STR: &'static str = "Oh look it's non fatal";
302        let constructors = vec![Error::User, Error::Unexpected, Error::Config];
303        for c in constructors.into_iter() {
304            let err = c(NonFatalError(anyhow!(ERR_STR)).into());
305            let res = err.downcast_non_fatal().expect("expected non-fatal downcast");
306            assert_eq!(res.to_string(), ERR_STR.to_owned());
307        }
308    }
309
310    #[test]
311    fn test_error_source() {
312        static ERR_STR: &'static str = "some nonsense";
313        let constructors = vec![Error::User, Error::Unexpected, Error::Config];
314        for cons in constructors.into_iter() {
315            let err = cons(anyhow!(ERR_STR));
316            let res = err.source();
317            assert!(res.is_ok());
318            assert_eq!(res.unwrap().to_string(), ERR_STR.to_owned());
319        }
320    }
321
322    #[test]
323    fn test_error_source_flatten_no_context() {
324        assert_eq!("Some Operation", Error::User(anyhow!("Some Operation")).to_string());
325    }
326
327    // The order of context's is "in-side-out", the root-most error is
328    // created first, and then the context() is attached on all of the
329    // returned values, so they are created in the opposite order that they
330    // are displayed.
331
332    #[test]
333    fn test_error_source_flatten_one_context() {
334        let expected = "Some Other Operation: some failure";
335        let error = anyhow!("some failure");
336        let error = error.context("Some Other Operation");
337        assert_eq!(expected, Error::User(error).to_string());
338    }
339
340    #[test]
341    fn test_error_source_flatten_two_contexts() {
342        let expected = "Some Operation: some context: some failure";
343        let error = anyhow!("some failure");
344        let error = error.context("some context");
345        let error = error.context("Some Operation");
346        assert_eq!(expected, Error::User(error).to_string());
347    }
348
349    #[test]
350    fn test_error_source_flatten_three_contexts() {
351        let expected = "Some Operation: some context: more context: some failure";
352        let error = anyhow!("some failure")
353            .context("more context")
354            .context("some context")
355            .context("Some Operation");
356        assert_eq!(expected, Error::User(error).to_string());
357    }
358
359    #[test]
360    fn test_error_doesnt_duplicate_when_rewrapped() {
361        #[derive(thiserror::Error, Debug)]
362        enum NonsenseErr {
363            #[error(transparent)]
364            Error(#[from] FfxError),
365        }
366        let expected = "This thing broke!";
367        let error = ffx_error!(anyhow!(expected));
368        let error: NonsenseErr = error.into();
369        let error = Error::User(error.into());
370        assert_eq!(
371            error.to_string(),
372            expected.to_owned(),
373            "There should be no duplication from re-wrapping errors"
374        );
375    }
376}