gh_workflow_parser/
err_msg_parse.rs

1//! Parsing error messages from the Yocto and other workflows
2use crate::{commands::WorkflowKind, err_msg_parse::yocto_err::util::YoctoFailureKind};
3use std::error::Error;
4
5use self::yocto_err::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_err;
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) -> Result<ErrorMessageSummary, Box<dyn Error>> {
52    let err_msg = match workflow {
53        WorkflowKind::Yocto => {
54            ErrorMessageSummary::Yocto(yocto_err::parse_yocto_error(err_msg).unwrap_or_else(|e| {
55                log::warn!("Failed to parse Yocto error: {e}");
56                YoctoError::new(err_msg.to_string(), YoctoFailureKind::default(), None)
57            }))
58        },
59        WorkflowKind::Other => ErrorMessageSummary::Other(err_msg.to_string()),
60    };
61    Ok(err_msg)
62}