Skip to content

Commit 2cafbef

Browse files
committed
jobs/readmes: Simplify error handling
1 parent a4dfd47 commit 2cafbef

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

src/worker/jobs/readmes.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::tasks::spawn_blocking;
55
use crate::worker::Environment;
66
use crates_io_markdown::text_to_html;
77
use crates_io_worker::BackgroundJob;
8+
use diesel::result::DatabaseErrorKind;
9+
use diesel::result::Error::DatabaseError;
810
use diesel_async::AsyncConnection;
911
use diesel_async::scoped_futures::ScopedFutureExt;
1012
use serde::{Deserialize, Serialize};
@@ -70,31 +72,39 @@ impl BackgroundJob for RenderAndUploadReadme {
7072
let mut conn = env.deadpool.get().await?;
7173
conn.transaction(|conn| {
7274
async move {
73-
let (crate_name, vers): (String, String) =
74-
match Version::record_readme_rendering(job.version_id, conn)
75-
.await
76-
.and(
77-
versions::table
78-
.find(job.version_id)
79-
.inner_join(crates::table)
80-
.select((crates::name, versions::num))
81-
.first(conn)
82-
.await,
83-
) {
84-
Ok(r) => r,
85-
Err(diesel::result::Error::DatabaseError(
86-
diesel::result::DatabaseErrorKind::ForeignKeyViolation,
87-
..,
88-
))
89-
| Err(diesel::result::Error::NotFound) => {
90-
warn!(
91-
"Skipping rendering README for version {}: no version found",
92-
job.version_id
93-
);
94-
return Ok(());
95-
}
96-
Err(err) => return Err(err.into()),
97-
};
75+
match Version::record_readme_rendering(job.version_id, conn).await {
76+
Ok(_) => {}
77+
Err(DatabaseError(DatabaseErrorKind::ForeignKeyViolation, ..)) => {
78+
warn!(
79+
"Skipping README rendering recording for version {}: version not found",
80+
job.version_id
81+
);
82+
return Ok(());
83+
}
84+
Err(err) => {
85+
warn!(
86+
"Failed to record README rendering for version {}: {err}",
87+
job.version_id,
88+
);
89+
return Err(err.into());
90+
}
91+
}
92+
93+
let result = versions::table
94+
.find(job.version_id)
95+
.inner_join(crates::table)
96+
.select((crates::name, versions::num))
97+
.first::<(String, String)>(conn)
98+
.await
99+
.optional()?;
100+
101+
let Some((crate_name, vers)) = result else {
102+
warn!(
103+
"Skipping README rendering for version {}: version not found",
104+
job.version_id
105+
);
106+
return Ok(());
107+
};
98108

99109
tracing::Span::current().record("krate.name", tracing::field::display(&crate_name));
100110

0 commit comments

Comments
 (0)