diff --git a/libraries/src/Exception/ExceptionHandler.php b/libraries/src/Exception/ExceptionHandler.php index 7fbe87fd4440c..9d29c8a5996eb 100644 --- a/libraries/src/Exception/ExceptionHandler.php +++ b/libraries/src/Exception/ExceptionHandler.php @@ -10,10 +10,13 @@ namespace Joomla\CMS\Exception; use Joomla\CMS\Application\CMSApplication; +use Joomla\CMS\Application\Exception\NotAcceptable; use Joomla\CMS\Error\AbstractRenderer; use Joomla\CMS\Event\Application\AfterInitialiseDocumentEvent; use Joomla\CMS\Factory; use Joomla\CMS\Log\Log; +use Joomla\CMS\Router\Exception\RouteNotFoundException; +use Joomla\CMS\Uri\Uri; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -221,18 +224,38 @@ protected static function isException($error) */ protected static function logException(\Throwable $error) { + // Handle common client errors as notices instead of critical errors + if ($error instanceof RouteNotFoundException) { + $level = Log::NOTICE; + $message = \sprintf( + 'Page not found (404): %s. Message: "%s"', + Uri::getInstance()->toString(), + $error->getMessage() + ); + $category = 'client-error'; + } elseif ($error instanceof NotAcceptable) { + $level = Log::NOTICE; + $message = \sprintf( + 'Not acceptable (406): %s. Message: "%s"', + Uri::getInstance()->toString(), + $error->getMessage() + ); + $category = 'client-error'; + } else { + // For all other errors, log a critical error with the full stack trace. + $level = Log::CRITICAL; + $message = \sprintf( + 'Uncaught Throwable of type %1$s thrown with message "%2$s". Stack trace: %3$s', + \get_class($error), + $error->getMessage(), + $error->getTraceAsString() + ); + $category = 'error'; + } + // Try to log the error, but don't let the logging cause a fatal error try { - Log::add( - \sprintf( - 'Uncaught Throwable of type %1$s thrown with message "%2$s". Stack trace: %3$s', - \get_class($error), - $error->getMessage(), - $error->getTraceAsString() - ), - Log::CRITICAL, - 'error' - ); + Log::add($message, $level, $category); } catch (\Throwable) { // Logging failed, don't make a stink about it though }