Skip to content

Commit 32f4a06

Browse files
fix: Return proper http response codes for errors (#192)
Co-authored-by: Jonah Lawrence <jonah@freshidea.com>
1 parent 1947209 commit 32f4a06

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

src/card.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,10 @@ function convertSvgToPng(string $svg): string
345345
*
346346
* @param string|array $output The stats (array) or error message (string) to display
347347
*/
348-
function renderOutput(string|array $output): void
348+
function renderOutput(string|array $output, int $responseCode = 200): void
349349
{
350350
$requestedType = $_REQUEST['type'] ?? 'svg';
351+
http_response_code($responseCode);
351352

352353
// output JSON data
353354
if ($requestedType === "json") {

src/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
$message = file_exists(dirname(__DIR__ . '../.env', 1))
1919
? "Missing token in config. Check Contributing.md for details."
2020
: ".env was not found. Check Contributing.md for details.";
21-
renderOutput($message);
21+
renderOutput($message, 500);
2222
}
2323

2424
// set cache to refresh once per hour
@@ -39,5 +39,5 @@
3939
$stats = getContributionStats($contributions);
4040
renderOutput($stats);
4141
} catch (InvalidArgumentException | AssertionError $error) {
42-
renderOutput($error->getMessage());
42+
renderOutput($error->getMessage(), $error->getCode());
4343
}

src/stats.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ function fetchGraphQL(string $query): stdClass
133133
http_response_code(curl_getinfo($ch, CURLINFO_HTTP_CODE));
134134
// Missing SSL certificate
135135
if (str_contains(curl_error($ch), 'unable to get local issuer certificate')) {
136-
throw new AssertionError("You don't have a valid SSL Certificate installed or XAMPP.");
136+
throw new AssertionError("You don't have a valid SSL Certificate installed or XAMPP.", 400);
137137
}
138138
// Handle errors such as "Bad credentials"
139139
if ($obj && $obj->message) {
140-
throw new AssertionError("Error: $obj->message \n<!-- $response -->");
140+
throw new AssertionError("Error: $obj->message \n<!-- $response -->", 401);
141141
}
142142
// TODO: Make the $response part get passed into a custom error and render the commented details in the SVG card generator
143-
throw new AssertionError("An error occurred when getting a response from GitHub.\n<!-- $response -->");
143+
throw new AssertionError("An error occurred when getting a response from GitHub.\n<!-- $response -->", 502);
144144
}
145145
return $obj;
146146
}
@@ -166,17 +166,17 @@ function getContributionYears(string $user): array
166166
$response = fetchGraphQL($query);
167167
// User not found
168168
if (!empty($response->errors) && $response->errors[0]->type === "NOT_FOUND") {
169-
throw new InvalidArgumentException("Could not find a user with that name.");
169+
throw new InvalidArgumentException("Could not find a user with that name.", 404);
170170
}
171171
// API Error
172172
if (!empty($response->errors)) {
173173
// Other errors that contain a message field
174-
throw new InvalidArgumentException($response->errors[0]->message);
174+
throw new InvalidArgumentException($response->errors[0]->message, 500);
175175
}
176176
// API did not return data
177177
if (!isset($response->data) && isset($response->message)) {
178178
// Other errors that contain a message field
179-
throw new InvalidArgumentException($response->message);
179+
throw new InvalidArgumentException($response->message, 204);
180180
}
181181
return $response->data->user->contributionsCollection->contributionYears;
182182
}
@@ -196,7 +196,7 @@ function getContributionDates(array $contributionGraphs): array
196196
$tomorrow = date("Y-m-d", strtotime("tomorrow"));
197197
foreach ($contributionGraphs as $graph) {
198198
if (!empty($graph->errors)) {
199-
throw new AssertionError($graph->data->errors[0]->message);
199+
throw new AssertionError($graph->data->errors[0]->message, 502);
200200
}
201201
$weeks = $graph->data->user->contributionsCollection->contributionCalendar->weeks;
202202
foreach ($weeks as $week) {
@@ -225,7 +225,7 @@ function getContributionStats(array $contributions): array
225225
{
226226
// if no contributions, display error
227227
if (empty($contributions)) {
228-
throw new AssertionError("No contributions found.");
228+
throw new AssertionError("No contributions found.", 204);
229229
}
230230
$today = array_key_last($contributions);
231231
$first = array_key_first($contributions);

0 commit comments

Comments
 (0)