Skip to content

Commit 10dd57d

Browse files
fix: Always return HTTP 200 to display error cards in README (#860) (#871)
* working to fix the error * working to fix the error * Delete composer-setup.php --------- Co-authored-by: Jonah Lawrence <jonah@freshidea.com>
1 parent 3dc753a commit 10dd57d

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/card.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,10 @@ function convertSvgToPng(string $svg, int $cardWidth, int $cardHeight): string
804804
*
805805
* @param string|array $output The stats (array) or error message (string) to display
806806
* @param array<string,string>|NULL $params Request parameters
807+
* @param int $errorCode The HTTP error code (used for JSON responses)
807808
* @return array The Content-Type header and the response body, and status code in case of an error
808809
*/
809-
function generateOutput(string|array $output, array $params = null): array
810+
function generateOutput(string|array $output, array $params = null, int $errorCode = 200): array
810811
{
811812
$params = $params ?? $_REQUEST;
812813

@@ -815,7 +816,7 @@ function generateOutput(string|array $output, array $params = null): array
815816
// output JSON data
816817
if ($requestedType === "json") {
817818
// generate array from output
818-
$data = gettype($output) === "string" ? ["error" => $output] : $output;
819+
$data = gettype($output) === "string" ? ["error" => $output, "code" => $errorCode] : $output;
819820
return [
820821
"contentType" => "application/json",
821822
"body" => json_encode($data),
@@ -864,13 +865,15 @@ function generateOutput(string|array $output, array $params = null): array
864865
* Set headers and output response
865866
*
866867
* @param string|array $output The Content-Type header and the response body
867-
* @param int $responseCode The HTTP response code to send
868+
* @param int $responseCode The HTTP response code to send (stored for JSON consumers but always returns 200 for images)
868869
* @return void The function exits after sending the response
869870
*/
870871
function renderOutput(string|array $output, int $responseCode = 200): void
871872
{
872-
$response = generateOutput($output);
873-
http_response_code($response["status"] ?? $responseCode);
873+
$response = generateOutput($output, null, $responseCode);
874+
// Always return HTTP 200 for SVG/PNG so GitHub's image proxy (Camo) displays error cards
875+
// instead of broken images. The original error code is included in JSON responses.
876+
http_response_code(200);
874877
header("Content-Type: {$response["contentType"]}");
875878
exit($response["body"]);
876879
}

0 commit comments

Comments
 (0)