From d0546e54e76e48dba1fe4b62f8f163fb66d1dd2d Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Wed, 3 Jun 2026 13:58:18 +0200 Subject: [PATCH] feat: capture php errors when running broken-link-checker For test and development tiers only,return HTTP/500 when a PHP error is encountered, so that the broken-link-checker can report on it, and also so it is more obvious during local development. Test-bot: skip --- _common/KeymanSentry.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/_common/KeymanSentry.php b/_common/KeymanSentry.php index 197fcea..14e2416 100644 --- a/_common/KeymanSentry.php +++ b/_common/KeymanSentry.php @@ -7,11 +7,33 @@ require_once __DIR__ . '/KeymanHosts.php'; require_once __DIR__ . '/Assets.php'; + if(KeymanHosts::Instance()->Tier() == KeymanHosts::TIER_DEVELOPMENT || + KeymanHosts::Instance()->Tier() == KeymanHosts::TIER_TEST) { + // For testing broken pages, we want to send HTTP 500 so that + // broken-link-checker will report it; in order to do this we + // need to cache the page output so that headers are not sent + // too early + ob_start(); + } + class KeymanSentry { static function init($dsn) { \Sentry\init([ 'dsn' => $dsn, - 'environment' => KeymanHosts::Instance()->TierName() + 'environment' => KeymanHosts::Instance()->TierName(), + 'before_send' => function (\Sentry\Event $event) { + // Don't send events from localhost or dev environments + if (KeymanHosts::Instance()->Tier() == KeymanHosts::TIER_DEVELOPMENT || + KeymanHosts::Instance()->Tier() == KeymanHosts::TIER_TEST) { + if(headers_sent()) { + echo "

Fatal error: not setting 500 because headers already sent.

"; + } else { + header("HTTP/1.1 500 Internal Server Error"); + } + return null; + } + return $event; + }, ]); }