ci_manager/
err_parse.rs

1//! Parsing error messages from the Yocto and other workflows
2use crate::*;
3use crate::{config::commands::WorkflowKind, err_parse::yocto::util::YoctoFailureKind};
4
5use self::yocto::YoctoError;
6
7/// Maximum size of a logfile we'll add to the issue body
8///
9/// The maximum size of a GitHub issue body is 65536
10pub const LOGFILE_MAX_LEN: usize = 5000;
11
12pub mod yocto;
13
14#[derive(Debug)]
15pub enum ErrorMessageSummary {
16    Yocto(YoctoError),
17    Other(String),
18}
19
20impl ErrorMessageSummary {
21    pub fn summary(&self) -> &str {
22        match self {
23            ErrorMessageSummary::Yocto(err) => err.summary(),
24            ErrorMessageSummary::Other(o) => o.as_str(),
25        }
26    }
27    pub fn log(&self) -> Option<&str> {
28        match self {
29            ErrorMessageSummary::Yocto(err) => err.logfile().map(|log| log.contents.as_str()),
30            ErrorMessageSummary::Other(_) => None, // Does not come with a log file
31        }
32    }
33    pub fn logfile_name(&self) -> Option<&str> {
34        match self {
35            ErrorMessageSummary::Yocto(err) => err.logfile().map(|log| log.name.as_str()),
36            ErrorMessageSummary::Other(_) => None, // Does not come with a log file
37        }
38    }
39
40    pub fn failure_label(&self) -> Option<String> {
41        match self {
42            ErrorMessageSummary::Yocto(err) => Some(err.kind().to_string()),
43            ErrorMessageSummary::Other(_) => None,
44        }
45    }
46}
47
48pub fn parse_error_message(
49    err_msg: &str,
50    workflow: WorkflowKind,
51) -> anyhow::Result<ErrorMessageSummary> {
52    let err_msg = if Config::global().trim_timestamp() {
53        log::info!("Trimming timestamps from the log error message");
54        remove_timestamp_prefixes(err_msg)
55    } else {
56        err_msg.into()
57    };
58    let err_msg = if Config::global().trim_ansi_codes() {
59        log::info!("Trimming ansi codes from the log error message");
60        remove_ansi_codes(&err_msg)
61    } else {
62        err_msg
63    };
64    let err_msg = err_msg.to_string();
65
66    let err_msg = match workflow {
67        WorkflowKind::Yocto => {
68            ErrorMessageSummary::Yocto(yocto::parse_yocto_error(&err_msg).unwrap_or_else(|e| {
69                log::warn!("Failed to parse Yocto error, returning error message as is: {e}");
70                YoctoError::new(err_msg, YoctoFailureKind::default(), None)
71            }))
72        }
73        WorkflowKind::Other => ErrorMessageSummary::Other(err_msg.to_string()),
74    };
75    Ok(err_msg)
76}