Skip to content

Commit 4c3d9be

Browse files
authored
fix: Catch error if failed to get contributions twice (DenverCoder1#424)
1 parent daddafd commit 4c3d9be

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/stats.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ function executeContributionGraphRequests(string $user, array $years): array
8383
removeGitHubToken($tokens[$year]);
8484
}
8585
error_log("First attempt to decode response for $user's $year contributions failed. $message");
86+
error_log("Contents: $contents");
8687
// retry request
8788
$query = buildContributionGraphQuery($user, $year);
8889
$token = getGitHubToken();
@@ -96,6 +97,7 @@ function executeContributionGraphRequests(string $user, array $years): array
9697
removeGitHubToken($token);
9798
}
9899
error_log("Failed to decode response for $user's $year contributions after 2 attempts. $message");
100+
error_log("Contents: $contents");
99101
continue;
100102
}
101103
}
@@ -120,7 +122,11 @@ function getContributionGraphs(string $user): array
120122
// get the list of years the user has contributed and the current year's contribution graph
121123
$currentYear = intval(date("Y"));
122124
$responses = executeContributionGraphRequests($user, [$currentYear]);
123-
$contributionYears = $responses[$currentYear]->data->user->contributionsCollection->contributionYears;
125+
$contributionYears = $responses[$currentYear]->data->user->contributionsCollection->contributionYears ?? [];
126+
// if there are no contribution years, an API error must have occurred
127+
if (empty($contributionYears)) {
128+
throw new AssertionError("Failed to retrieve contributions. This is likely a GitHub API issue.", 500);
129+
}
124130
// remove the current year from the list since it's already been fetched
125131
$contributionYears = array_filter($contributionYears, function ($year) use ($currentYear) {
126132
return $year !== $currentYear;

0 commit comments

Comments
 (0)