Skip to content

Commit ca1fec9

Browse files
authored
Merge pull request #2221 from Urgau/app-error-network-error
Return the inner network error instead of 500
2 parents 4c55f00 + ebd489a commit ca1fec9

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/errors.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,27 @@ pub struct AppError(anyhow::Error);
6161

6262
impl IntoResponse for AppError {
6363
fn into_response(self) -> Response {
64-
tracing::error!("{:?}", &self.0);
65-
(
66-
StatusCode::INTERNAL_SERVER_ERROR,
67-
format!("Something went wrong: {}\n\n{REPORT_TO}", self.0),
68-
)
69-
.into_response()
64+
tracing::error!("app error: {:?}", &self.0);
65+
66+
// Let's avoid returning 500 when it's a network error and instead return the status
67+
// code of the network request (useful for GHA logs which can get 404, 410 from GitHub).
68+
if let Some(err) = self.0.downcast_ref::<reqwest::Error>()
69+
&& let Some(status) = err.status()
70+
{
71+
(
72+
status,
73+
format!(
74+
"Something went wrong: {}\n\nNetwork error: {err}\n\n{REPORT_TO}",
75+
self.0
76+
),
77+
)
78+
} else {
79+
(
80+
StatusCode::INTERNAL_SERVER_ERROR,
81+
format!("Something went wrong: {}\n\n{REPORT_TO}", self.0),
82+
)
83+
}
84+
.into_response()
7085
}
7186
}
7287

src/gha_logs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ pub async fn gha_logs(
8888
logs,
8989
} = &*'logs: {
9090
if let Some(logs) = ctx.gha_logs.write().await.get(&log_uuid) {
91-
tracing::info!("gha_logs: cache hit for {log_uuid}");
91+
tracing::info!("gha_logs: cache hit for log {log_uuid}");
9292
break 'logs logs;
9393
}
9494

95-
tracing::info!("gha_logs: cache miss for {log_uuid}");
95+
tracing::info!("gha_logs: cache miss for log {log_uuid}");
9696

9797
let repo = github::IssueRepository {
9898
organization: owner.to_string(),
@@ -104,7 +104,7 @@ pub async fn gha_logs(
104104
.github
105105
.workflow_run_job(&repo, log_id)
106106
.await
107-
.context("unable to fetch job details")?;
107+
.with_context(|| format!("unable to fetch the job details for log {log_id}"))?;
108108

109109
// To minimize false positives in paths linked to the GitHub repositories, we
110110
// restrict matching to only the second-level directories of the repository.
@@ -164,7 +164,7 @@ pub async fn gha_logs(
164164
.github
165165
.raw_job_logs(&repo, log_id)
166166
.await
167-
.context("unable to get the raw logs")?;
167+
.with_context(|| format!("unable to get the raw logs for log {log_id}"))?;
168168

169169
let json_logs =
170170
serde_json::to_string(&*logs).context("unable to JSON-ify the raw logs")?;

0 commit comments

Comments
 (0)