Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cli/src/summary_output/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ impl ErrorSummary {
exit_code: context.exit_code,
}
}

/// What went wrong, for a reader who already has `message`: Trunk's explanation when its API
/// gave one, else the error with its causes.
pub fn reason(&self) -> String {
self.explanation.clone().unwrap_or_else(|| {
std::iter::once(self.error.as_str())
.chain(self.causes.iter().map(String::as_str))
.collect::<Vec<_>>()
.join(": ")
})
}
}
2 changes: 1 addition & 1 deletion cli/src/summary_output/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn error_message(error: &ErrorSummary) -> String {
match &error.message {
Some(message) => {
lines.push(message.clone());
lines.extend(error.explanation.clone());
lines.push(error.reason());
}
None => lines.push(error.error.clone()),
}
Expand Down
6 changes: 2 additions & 4 deletions cli/src/summary_output/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ pub fn render_error(error: &ErrorSummary) -> anyhow::Result<Vec<Line>> {
if let Some(message) = &error.message {
lines.push(Line::from_iter([Span::new_unstyled_lossy(message)]));
lines.push(Line::default());
if let Some(explanation) = &error.explanation {
lines.push(Line::from_iter([Span::new_unstyled_lossy(explanation)]));
lines.push(Line::default());
}
lines.push(Line::from_iter([Span::new_unstyled_lossy(error.reason())]));
lines.push(Line::default());
} else {
lines.push(Line::from_iter([Span::new_unstyled_lossy(&error.error)]));
}
Expand Down
27 changes: 27 additions & 0 deletions cli/tests/summary/error_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ fn prints_a_client_error_explanation_beneath_the_base_message() {
);
}

#[test]
fn prints_the_error_and_its_causes_beneath_the_base_message() {
let error = anyhow::anyhow!(
"--allow-forked-pr-uploads without a Trunk API token uploads only from a GitHub Actions run of a pull request from a fork; test results will not be uploaded."
)
.context("Failed to create bundle upload");
let report = ErrorReport::new(
error,
String::from("fake-org-slug"),
Some(String::from("There was an unexpected error")),
);

assert_eq!(
error_summary(&report),
[
"Error Encountered",
"There was an unexpected error",
"",
"Failed to create bundle upload: --allow-forked-pr-uploads without a Trunk API token uploads only from a GitHub Actions run of a pull request from a fork; test results will not be uploaded.",
"",
"For more help, contact us at https://slack.trunk.io/",
"",
"Errors occurred during execution, using quarantining exit code",
]
);
}

#[test]
fn prints_connection_refused_beneath_the_base_message() {
let report = ErrorReport::new(
Expand Down
16 changes: 16 additions & 0 deletions cli/tests/summary/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ fn warns_about_an_upload_error_before_the_failures() {
);
}

#[test]
fn warns_with_the_reason_an_upload_failed() {
let mut result = upload_result(3);
result.error_report = Some(ErrorReport::new(
anyhow::anyhow!("uploads only from a pull request from a fork")
.context("Failed to create bundle upload"),
String::from("test-org"),
Some(String::from(UPLOAD_ERROR_MESSAGE)),
));

assert_eq!(
upload_annotations(&result)[0],
"::warning::There was an unexpected error that occurred while uploading test results%0AFailed to create bundle upload: uploads only from a pull request from a fork",
);
}

#[test]
fn warns_that_results_with_old_timestamps_will_be_dropped() {
let mut result = upload_result(5);
Expand Down
Loading