Skip to content

Commit

Permalink
Fixed spammy patreon error when patreon is down
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoAbove committed Sep 24, 2024
1 parent 6eff290 commit 471b326
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions server/patreon.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,48 @@ const loadUser = async (req, res, next) => {
};

const loadPatreonClient = async (req, res, next) => {
const patreonUser = await getIdentity(req.user.patreonData.access_token);
if (patreonUser.errors) {
res.status(401).send(null);
return;
}
try {
const response = await fetch(
`https://www.patreon.com/api/oauth2/v2/identity?${new URLSearchParams({
"fields[user]": ["full_name", "url", "image_url"],
})}`,
{
headers: {
Authorization: `Bearer ${req.user.patreonData.access_token}`,
"Content-Type": "application/json",
},
},
);

req.patreonUser = patreonUser.data;
next();
if (!response.ok) {
console.error(`Patreon API error: ${response.status} ${response.statusText}`);
if (response.status === 503) {
// Service Unavailable, we might want to retry after a delay
return res.status(503).json({ error: "Patreon service temporarily unavailable" });
}
// For other errors, we'll send a generic error message
return res.status(500).json({ error: "Error fetching Patreon data" });
}

const contentType = response.headers.get("content-type");
if (!contentType || !contentType.includes("application/json")) {
// Handle non-JSON responses
console.error("Unexpected content type from Patreon API:", contentType);
return res.status(500).json({ error: "Unexpected response from Patreon" });
}

const patreonUser = await response.json();
if (patreonUser.errors) {
console.error("Patreon API returned errors:", patreonUser.errors);
return res.status(401).json({ error: "Unauthorized" });
}

req.patreonUser = patreonUser.data;
next();
} catch (error) {
console.error("Error in loadPatreonClient:", error);
res.status(500).json({ error: "Internal server error" });
}
};

const gatherMeData = (user, patreonUser) => {
Expand Down

0 comments on commit 471b326

Please sign in to comment.