Skip to content

Commit 796acb1

Browse files
authored
fix: Handle more GraphQL possible errors (#186)
1 parent 47b35f2 commit 796acb1

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/card.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ function convertSvgToPng(string $svg): string
315315
$svg = trim($svg);
316316

317317
// remove style and animations
318-
$svg = preg_replace('/(<style>\X*<\/style>)/m', '', $svg);
318+
$svg = preg_replace('/(<style>\X*?<\/style>)/m', '', $svg);
319319
$svg = preg_replace('/(opacity: 0;)/m', 'opacity: 1;', $svg);
320320
$svg = preg_replace('/(animation: fadein.*?;)/m', 'opacity: 1;', $svg);
321321
$svg = preg_replace('/(animation: currentstreak.*?;)/m', 'font-size: 28px;', $svg);

src/stats.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,24 @@ function fetchGraphQL(string $query): stdClass
101101
{
102102
$ch = getGraphQLCurlHandle($query);
103103
$response = curl_exec($ch);
104+
curl_close($ch);
105+
$obj = json_decode($response);
104106
// handle curl errors
105-
if ($response === false) {
107+
if ($response === false || $obj === null || curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400) {
108+
// set response code to curl error code
109+
http_response_code(curl_getinfo($ch, CURLINFO_HTTP_CODE));
110+
// Missing SSL certificate
106111
if (str_contains(curl_error($ch), 'unable to get local issuer certificate')) {
107112
throw new AssertionError("You don't have a valid SSL Certificate installed or XAMPP.");
108113
}
109-
throw new AssertionError("An error occurred when getting a response from GitHub.");
114+
// Handle errors such as "Bad credentials"
115+
if ($obj && $obj->message) {
116+
throw new AssertionError("Error: $obj->message \n<!-- $response -->");
117+
}
118+
// TODO: Make the $response part get passed into a custom error and render the commented details in the SVG card generator
119+
throw new AssertionError("An error occurred when getting a response from GitHub.\n<!-- $response -->");
110120
}
111-
curl_close($ch);
112-
return json_decode($response);
121+
return $obj;
113122
}
114123

115124
/**
@@ -140,6 +149,11 @@ function getContributionYears(string $user): array
140149
// Other errors that contain a message field
141150
throw new InvalidArgumentException($response->data->errors[0]->message);
142151
}
152+
// API did not return data
153+
if (!isset($response->data) && isset($response->message)) {
154+
// Other errors that contain a message field
155+
throw new InvalidArgumentException($response->message);
156+
}
143157
return $response->data->user->contributionsCollection->contributionYears;
144158
}
145159

0 commit comments

Comments
 (0)