Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions libraries/src/Exception/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Joomla\CMS\Event\Application\AfterInitialiseDocumentEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Router\Exception\RouteNotFoundException;
use Joomla\CMS\Application\Exception\NotAcceptable;
use Joomla\CMS\Uri\Uri;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand Down Expand Up @@ -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
}
Expand Down
Loading