Skip to content

og-image: Handle 404 errors from avatar downloads gracefully #11517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2025
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
25 changes: 16 additions & 9 deletions crates/crates_io_og_image/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use error::OgImageError;
use crate::formatting::{serialize_bytes, serialize_number, serialize_optional_number};
use bytes::Bytes;
use crates_io_env_vars::var;
use reqwest::StatusCode;
use serde::Serialize;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -252,19 +253,25 @@ impl OgImageGenerator {
} else {
debug!(url = %avatar, "Downloading avatar from URL: {avatar}");
// Download the avatar from the URL
let response = client
.get(*avatar)
.send()
.await
.map_err(|err| OgImageError::AvatarDownloadError {
let response = client.get(*avatar).send().await.map_err(|err| {
OgImageError::AvatarDownloadError {
url: avatar.to_string(),
source: err,
})?
.error_for_status()
.map_err(|err| OgImageError::AvatarDownloadError {
}
})?;

let status = response.status();
if status == StatusCode::NOT_FOUND {
warn!(url = %avatar, "Avatar URL returned 404 Not Found");
continue; // Skip this avatar if not found
}

if let Err(err) = response.error_for_status_ref() {
return Err(OgImageError::AvatarDownloadError {
url: avatar.to_string(),
source: err,
})?;
});
}

let content_length = response.content_length();
debug!(
Expand Down
5 changes: 4 additions & 1 deletion crates/crates_io_og_image/template/og-image.typ
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@
let authors-with-avatars = data.authors.map(author => {
let avatar = none
if author.avatar != none {
avatar = "assets/" + avatar_map.at(author.avatar)
let avatar_path = avatar_map.at(author.avatar, default: none)
if avatar_path != none {
avatar = "assets/" + avatar_path
}
}
(name: author.name, avatar: avatar)
})
Expand Down