ffx_command_error/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use errors::FfxError;

/// Represents a recoverable error. Intended to be embedded in `Error`.
#[derive(thiserror::Error, Debug)]
#[error("non-fatal error encountered: {}", .0)]
pub struct NonFatalError(#[source] pub anyhow::Error);

/// A top level error type for ffx tool results
#[derive(thiserror::Error, Debug)]
pub enum Error {
    /// An error that qualifies as a bugcheck
    Unexpected(#[source] anyhow::Error),
    /// A known kind of error that can be reported usefully to the user
    User(#[source] anyhow::Error),
    /// An early-exit that should result in outputting help to the user (like [`argh::EarlyExit`]),
    /// but is not itself an error in any meaningful sense.
    Help {
        /// The command name (argv[0..]) that should be used in supplemental help output
        command: Vec<String>,
        /// The text to output to the user
        output: String,
        /// The exit status
        code: i32,
    },
    /// Something failed before ffx's configuration could be loaded (like an
    /// invalid argument, a failure to read an env config file, etc).
    ///
    /// Errors of this type should include any information the user might need
    /// to recover from the issue, because it will not advise the user to look
    /// in the log files or anything like that.
    Config(#[source] anyhow::Error),
    /// Exit with a specific error code but no output
    ExitWithCode(i32),
}

impl Error {
    /// Attempts to downcast this error into something non-fatal, returning `Ok(e)`
    /// if able to downcast to something non-fatal, else returning the original error.
    pub fn downcast_non_fatal(self) -> Result<anyhow::Error, Self> {
        fn try_downcast(err: anyhow::Error) -> Result<anyhow::Error, anyhow::Error> {
            match err.downcast::<NonFatalError>() {
                Ok(NonFatalError(e)) => Ok(e),
                Err(e) => Err(e),
            }
        }

        match self {
            Self::Help { .. } | Self::ExitWithCode(_) => Err(self),
            Self::User(e) => try_downcast(e).map_err(Self::User),
            Self::Unexpected(e) => try_downcast(e).map_err(Self::Unexpected),
            Self::Config(e) => try_downcast(e).map_err(Self::Config),
        }
    }
}

/// Writes a detailed description of an anyhow error to the formatter
fn write_detailed(f: &mut std::fmt::Formatter<'_>, error: &anyhow::Error) -> std::fmt::Result {
    write!(f, "Error: {}", error)?;
    for (i, e) in error.chain().skip(1).enumerate() {
        write!(f, "\n  {: >3}.  {}", i + 1, e)?;
    }
    Ok(())
}

const BUG_LINE: &str = "BUG: An internal command error occurred.";
impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unexpected(error) => {
                writeln!(f, "{BUG_LINE}")?;
                write_detailed(f, error)
            }
            Self::User(error) | Self::Config(error) => write!(f, "{error}"),
            Self::Help { output, .. } => write!(f, "{output}"),
            Self::ExitWithCode(code) => write!(f, "Exiting with code {code}"),
        }
    }
}

impl From<anyhow::Error> for Error {
    fn from(error: anyhow::Error) -> Self {
        // If it's already an Error, just return it
        match error.downcast::<Self>() {
            Ok(this) => this,
            // this is just a compatibility shim to extract information out of the way
            // we've traditionally divided user and unexpected errors.
            Err(error) => match error.downcast::<FfxError>() {
                Ok(err) => Self::User(err.into()),
                Err(err) => Self::Unexpected(err),
            },
        }
    }
}

impl From<FfxError> for Error {
    fn from(error: FfxError) -> Self {
        Error::User(error.into())
    }
}

impl Error {
    /// Map an argh early exit to our kind of error
    pub fn from_early_exit(command: &[impl AsRef<str>], early_exit: argh::EarlyExit) -> Self {
        let command = Vec::from_iter(command.iter().map(|s| s.as_ref().to_owned()));
        let output = early_exit.output;
        // if argh's early_exit status is Ok() that means it's printing help because
        // of a `--help` argument or `help` as a subcommand was passed. Otherwise
        // it's just an error parsing the arguments. So only map `status: Ok(())`
        // as help output.
        match early_exit.status {
            Ok(_) => Error::Help { command, output, code: 0 },
            Err(_) => Error::Config(anyhow::anyhow!("{}", output)),
        }
    }

    /// Get the exit code this error should correspond to if it bubbles up to `main()`
    pub fn exit_code(&self) -> i32 {
        match self {
            Error::User(err) => {
                if let Some(FfxError::Error(_, code)) = err.downcast_ref() {
                    *code
                } else {
                    1
                }
            }
            Error::Help { code, .. } => *code,
            Error::ExitWithCode(code) => *code,
            _ => 1,
        }
    }
}

/// A convenience Result type
pub type Result<T, E = crate::Error> = core::result::Result<T, E>;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tests::*;
    use anyhow::anyhow;
    use assert_matches::assert_matches;
    use errors::{ffx_error, ffx_error_with_code, IntoExitCode};
    use std::io::{Cursor, Write};

    #[test]
    fn test_write_result_ffx_error() {
        let err = Error::from(ffx_error!(FFX_STR));
        let mut cursor = Cursor::new(Vec::new());

        assert_matches!(write!(&mut cursor, "{err}"), Ok(_));

        assert!(String::from_utf8(cursor.into_inner()).unwrap().contains(FFX_STR));
    }

    #[test]
    fn into_error_from_arbitrary_is_unexpected() {
        let err = anyhow!(ERR_STR);
        assert_matches!(
            Error::from(err),
            Error::Unexpected(_),
            "an arbitrary anyhow error should convert to an 'unexpected' bug check error"
        );
    }

    #[test]
    fn into_error_from_ffx_error_is_user_error() {
        let err = FfxError::Error(anyhow!(FFX_STR), 1);
        assert_matches!(
            Error::from(err),
            Error::User(_),
            "an arbitrary anyhow error should convert to a 'user' error"
        );
    }

    #[test]
    fn into_error_from_contextualized_ffx_error_prints_original_error() {
        let err = Error::from(anyhow::anyhow!(errors::ffx_error!(FFX_STR)).context("boom"));
        assert_eq!(
            &format!("{err}"),
            FFX_STR,
            "an anyhow error with context should print the original error, not the context, when stringified."
        );
    }

    #[test]
    fn test_write_result_arbitrary_error() {
        let err = Error::from(anyhow!(ERR_STR));
        let mut cursor = Cursor::new(Vec::new());

        assert_matches!(write!(&mut cursor, "{err}"), Ok(_));

        let err_str = String::from_utf8(cursor.into_inner()).unwrap();
        assert!(err_str.contains(BUG_LINE));
        assert!(err_str.contains(ERR_STR));
    }

    #[test]
    fn test_result_ext_exit_code_ffx_error() {
        let err = Result::<()>::Err(Error::from(ffx_error_with_code!(42, FFX_STR)));
        assert_eq!(err.exit_code(), 42);
    }

    #[test]
    fn test_from_ok_early_exit() {
        let command = ["testing", "--help"];
        let output = "stuff!".to_owned();
        let status = Ok(());
        let code = 0;

        let early_exit = argh::EarlyExit { output: output.clone(), status };
        let err = Error::from_early_exit(&command, early_exit);
        assert_eq!(err.exit_code(), code);
        assert_matches!(err, Error::Help { command: error_command, output: error_output, code: error_code } if error_command == command && error_output == output && error_code == code);
    }

    #[test]
    fn test_from_error_early_exit() {
        let command = ["testing", "bad", "command"];
        let output = "stuff!".to_owned();
        let status = Err(());
        let code = 1;

        let early_exit = argh::EarlyExit { output: output.clone(), status };
        let err = Error::from_early_exit(&command, early_exit);
        assert_eq!(err.exit_code(), code);
        assert_matches!(err, Error::Config(err) if format!("{err}") == output);
    }

    #[test]
    fn test_downcast_recasts_types() {
        let err = Error::User(anyhow!("boom"));
        assert_matches!(err.downcast_non_fatal(), Err(Error::User(_)));

        let err = Error::Unexpected(anyhow!("boom"));
        assert_matches!(err.downcast_non_fatal(), Err(Error::Unexpected(_)));

        let err = Error::Config(anyhow!("boom"));
        assert_matches!(err.downcast_non_fatal(), Err(Error::Config(_)));

        let err =
            Error::Help { command: vec!["foobar".to_owned()], output: "blorp".to_owned(), code: 1 };
        assert_matches!(err.downcast_non_fatal(), Err(Error::Help { .. }));

        let err = Error::ExitWithCode(2);
        assert_matches!(err.downcast_non_fatal(), Err(Error::ExitWithCode(2)));
    }

    #[test]
    fn test_downcast_non_fatal_recovers_non_fatal_error() {
        static ERR_STR: &'static str = "Oh look it's non fatal";
        let constructors = vec![Error::User, Error::Unexpected, Error::Config];
        for c in constructors.into_iter() {
            let err = c(NonFatalError(anyhow!(ERR_STR)).into());
            let res = err.downcast_non_fatal().expect("expected non-fatal downcast");
            assert_eq!(res.to_string(), ERR_STR.to_owned());
        }
    }
}