Skip to content

Commit 876f922

Browse files
authored
og-image: Handle 404 errors from avatar downloads gracefully (#11517)
Some of our users appear to have avatar URLs like `https://private-avatars.githubusercontent.com/u/123095?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTEiLCJleHAiOjE3MzQ1MjkwMjAsIm5iZiI6MTczNDUyNzgyMCwicGF0aCI6Ii91LzEyMzA5NSJ9.qPHLwlZHda1ApuLzL5FfADxTiOsNjpp85IGvljRx9bM&v=4&s=64` which result in 404 errors when attempting to download them. This commit adjusts the code to log a warning in such cases and then show the author name without an avatar instead.
1 parent fba06d2 commit 876f922

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

crates/crates_io_og_image/src/lib.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub use error::OgImageError;
88
use crate::formatting::{serialize_bytes, serialize_number, serialize_optional_number};
99
use bytes::Bytes;
1010
use crates_io_env_vars::var;
11+
use reqwest::StatusCode;
1112
use serde::Serialize;
1213
use std::collections::HashMap;
1314
use std::path::{Path, PathBuf};
@@ -252,19 +253,25 @@ impl OgImageGenerator {
252253
} else {
253254
debug!(url = %avatar, "Downloading avatar from URL: {avatar}");
254255
// Download the avatar from the URL
255-
let response = client
256-
.get(*avatar)
257-
.send()
258-
.await
259-
.map_err(|err| OgImageError::AvatarDownloadError {
256+
let response = client.get(*avatar).send().await.map_err(|err| {
257+
OgImageError::AvatarDownloadError {
260258
url: avatar.to_string(),
261259
source: err,
262-
})?
263-
.error_for_status()
264-
.map_err(|err| OgImageError::AvatarDownloadError {
260+
}
261+
})?;
262+
263+
let status = response.status();
264+
if status == StatusCode::NOT_FOUND {
265+
warn!(url = %avatar, "Avatar URL returned 404 Not Found");
266+
continue; // Skip this avatar if not found
267+
}
268+
269+
if let Err(err) = response.error_for_status_ref() {
270+
return Err(OgImageError::AvatarDownloadError {
265271
url: avatar.to_string(),
266272
source: err,
267-
})?;
273+
});
274+
}
268275

269276
let content_length = response.content_length();
270277
debug!(

crates/crates_io_og_image/template/og-image.typ

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,10 @@
283283
let authors-with-avatars = data.authors.map(author => {
284284
let avatar = none
285285
if author.avatar != none {
286-
avatar = "assets/" + avatar_map.at(author.avatar)
286+
let avatar_path = avatar_map.at(author.avatar, default: none)
287+
if avatar_path != none {
288+
avatar = "assets/" + avatar_path
289+
}
287290
}
288291
(name: author.name, avatar: avatar)
289292
})

0 commit comments

Comments
 (0)