Skip to content

Commit

Permalink
fix: enhance error handling in fetchAllCommunities function
Browse files Browse the repository at this point in the history
- Wrapped the fetchAllCommunities logic in try-catch blocks to improve error handling and user feedback.
- Updated return values to include error messages when no chain or user is found, and when fetching on-chain communities fails.
- Improved logging for better debugging and visibility of fetched data and matched communities.
  • Loading branch information
tinypell3ts committed Jan 15, 2025
1 parent 90a6bb7 commit 6804b1f
Showing 1 changed file with 49 additions and 36 deletions.
85 changes: 49 additions & 36 deletions src/lib/openformat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,49 +49,60 @@ export async function getChainFromCommunityOrCookie(
}

export async function fetchAllCommunities() {
const chain = await getChainFromCommunityOrCookie();
try {
const chain = await getChainFromCommunityOrCookie();

if (!chain) {
console.log("No chain found for chainName:", chain);
return null;
}
if (!chain) {
console.log("No chain found for chainName:", chain);
return { data: [], error: "No chain found." };
}

const user = await getCurrentUser();
const dbCommunities = await getCommunities();
const user = await getCurrentUser();
const dbCommunities = await getCommunities();

if (!user) {
return null;
}
if (!user) {
return { data: [], error: "User not found." };
}

const query = `
query ($owner: String!) {
apps(
where: {owner_contains_nocase: $owner}
orderBy: createdAt
orderDirection: desc
) {
id
name
owner {
id
const query = `
query ($owner: String!) {
apps(
where: {owner_contains_nocase: $owner}
orderBy: createdAt
orderDirection: desc
) {
id
name
owner {
id
}
}
}
`;
try {
const data = await request<{
apps: { id: string; name: string; owner: { id: string } }[];
}>(chain.SUBGRAPH_URL, query, {
owner: user.wallet_address,
});

console.log("Fetched data from subgraph:", data);

const matchedCommunities = data.apps.map((app) => ({
...app,
metadata: dbCommunities.find((dbComm) => dbComm.id === app.id || dbComm.slug === app.id),
}));

console.log("Matched communities:", matchedCommunities);

return { data: matchedCommunities || [], error: null };
} catch {
return { data: [], error: "Failed to fetch onchain communities. Please try again." };
}
} catch {
return { data: [], error: "Failed to fetch communities. Please try again later." };
}
}
`;
const data = await request<{
apps: { id: string; name: string; owner: { id: string } }[];
}>(chain.SUBGRAPH_URL, query, {
owner: user.wallet_address,
});

// Match subgraph communities with database communities
const matchedCommunities = data.apps.map((app) => ({
...app,
metadata: dbCommunities.find((dbComm) => dbComm.id === app.id || dbComm.slug === app.id),
}));

return matchedCommunities;
}

export const fetchCommunity = cache(async (slugOrId: string) => {
const communityFromDb = await getCommunity(slugOrId);
Expand Down Expand Up @@ -313,6 +324,8 @@ export async function generateLeaderboard(slugOrId: string): Promise<Leaderboard
? "aurora"
: chain.apiChainName === ChainName.TURBO
? "turbo"
: chain.apiChainName === ChainName.BASE
? "base"
: "arbitrum-sepolia"
);
const response = await apiClient.get(`/leaderboard?${params}`);
Expand Down

0 comments on commit 6804b1f

Please sign in to comment.