Skip to content

Commit 1ef6384

Browse files
authored
Change output errors as JSON when type set to json (#165)
1 parent 78322f0 commit 1ef6384

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/index.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
? "Missing token or username in config. Check Contributing.md for details."
2222
: ".env was not found. Check Contributing.md for details.";
2323

24+
if ($requestedType === "json") {
25+
// set content type to JSON
26+
header('Content-Type: application/json');
27+
// echo JSON error message
28+
echo json_encode(array("error" => $message));
29+
exit;
30+
}
31+
2432
$card = generateErrorCard($message);
2533
if ($requestedType === "png") {
2634
echoAsPng($card);
@@ -50,6 +58,13 @@
5058
$contributions = getContributionDates($contributionGraphs);
5159
$stats = getContributionStats($contributions);
5260
} catch (InvalidArgumentException $error) {
61+
if ($requestedType === "json") {
62+
// set content type to JSON
63+
header('Content-Type: application/json');
64+
// echo JSON error message
65+
echo json_encode(array("error" => $error->getMessage()));
66+
exit;
67+
}
5368
$card = generateErrorCard($error->getMessage());
5469
if ($requestedType === "png") {
5570
echoAsPng($card);

0 commit comments

Comments
 (0)