use serde_json::Value;
use crate::{
path_segment, percent_encode_path_segment, Client, RequestProtocol, WorkflowDescription,
};
const TERMINAL_HISTORY_PAGE_LIMIT: usize = 20;
impl Client {
pub(crate) async fn with_terminal_failure_detail(
&self,
mut description: WorkflowDescription,
workflow_id: &str,
selected_run_id: Option<&str>,
) -> WorkflowDescription {
let terminal = description
.closed_reason
.as_deref()
.or(description.status.as_deref())
.unwrap_or_default()
.to_ascii_lowercase();
let event_type = match terminal.as_str() {
"failed" => "WorkflowFailed",
"timed_out" | "timedout" => "WorkflowTimedOut",
_ => return description,
};
let has_detail = description.failure.as_ref().is_some_and(Value::is_object)
|| description.exception.is_some()
|| !description.failures.is_empty()
|| description.error.is_some()
|| (event_type == "WorkflowTimedOut"
&& description
.raw
.get("reason")
.and_then(Value::as_str)
.is_some());
if has_detail {
return description;
}
let Some(run_id) = description
.run_id
.clone()
.or_else(|| selected_run_id.map(str::to_string))
else {
return description;
};
let workflow_id = description
.workflow_id
.clone()
.unwrap_or_else(|| workflow_id.to_string());
let Some(payload) = self
.terminal_history_payload(&workflow_id, &run_id, event_type)
.await
else {
return description;
};
let mut failure = serde_json::Map::new();
for key in [
"message",
"exception_type",
"exception_class",
"failure_category",
"non_retryable",
"failure_id",
"exception",
] {
if let Some(value) = payload.get(key).filter(|value| !value.is_null()) {
failure.insert(key.to_string(), value.clone());
}
}
if let Some(kind) = payload.get("timeout_kind").and_then(Value::as_str) {
failure.insert("reason".to_string(), Value::String(kind.to_string()));
}
failure.insert(
"detail_source".to_string(),
Value::String("history".to_string()),
);
description.failure = Some(Value::Object(failure));
description
}
async fn terminal_history_payload(
&self,
workflow_id: &str,
run_id: &str,
event_type: &str,
) -> Option<Value> {
let base = format!(
"/workflows/{}/runs/{}/history",
path_segment("workflow_id", workflow_id).ok()?,
path_segment("run_id", run_id).ok()?,
);
let mut next_page_token: Option<String> = None;
let mut payload = None;
for _ in 0..TERMINAL_HISTORY_PAGE_LIMIT {
let path = match &next_page_token {
Some(token) => format!(
"{base}?next_page_token={}",
percent_encode_path_segment(token)
),
None => base.clone(),
};
let page: Value = self
.request_json(
reqwest::Method::GET,
&path,
RequestProtocol::ControlPlane,
Option::<&Value>::None,
)
.await
.ok()?;
for event in page.get("events").and_then(Value::as_array)? {
if event.get("event_type").and_then(Value::as_str) == Some(event_type) {
payload = event
.get("payload")
.filter(|value| value.is_object())
.cloned();
}
}
next_page_token = page
.get("next_page_token")
.and_then(Value::as_str)
.filter(|token| !token.is_empty())
.map(str::to_string);
if next_page_token.is_none() {
return payload;
}
}
None
}
}