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
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::{error, fmt};

/// An error that occurs while running a `cargo nextest` command.
#[derive(Debug)]
pub enum CommandError {
    /// Executing the process resulted in an error.
    Exec(std::io::Error),

    /// The command exited with a non-zero code.
    CommandFailed {
        /// The exit code for the process. Exit codes can be cross-referenced against
        /// [`NextestExitCode`](crate::NextestExitCode).
        exit_code: Option<i32>,

        /// Standard error for the process.
        stderr: Vec<u8>,
    },

    /// Error parsing JSON output.
    Json(serde_json::Error),
}

impl fmt::Display for CommandError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Exec(_) => {
                write!(f, "`cargo nextest` process execution failed")
            }
            Self::CommandFailed { exit_code, stderr } => {
                let exit_code_str =
                    exit_code.map_or(String::new(), |code| format!(" with exit code {code}"));
                let stderr = String::from_utf8_lossy(stderr);
                write!(
                    f,
                    "`cargo nextest` failed{exit_code_str}, stderr:\n{stderr}\n"
                )
            }
            Self::Json(_) => {
                write!(f, "parsing `cargo nextest` JSON output failed")
            }
        }
    }
}

impl error::Error for CommandError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Self::Exec(err) => Some(err),
            Self::CommandFailed { .. } => None,
            Self::Json(err) => Some(err),
        }
    }
}