Skip to content

Commit fe62365

Browse files
[5.x] replace 404 and 406 CRITICAL errors with NOTICE and useful information (joomla#46296)
1 parent 2b63dc5 commit fe62365

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

libraries/src/Exception/ExceptionHandler.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
namespace Joomla\CMS\Exception;
1111

1212
use Joomla\CMS\Application\CMSApplication;
13+
use Joomla\CMS\Application\Exception\NotAcceptable;
1314
use Joomla\CMS\Error\AbstractRenderer;
1415
use Joomla\CMS\Event\Application\AfterInitialiseDocumentEvent;
1516
use Joomla\CMS\Factory;
1617
use Joomla\CMS\Log\Log;
18+
use Joomla\CMS\Router\Exception\RouteNotFoundException;
19+
use Joomla\CMS\Uri\Uri;
1720

1821
// phpcs:disable PSR1.Files.SideEffects
1922
\defined('_JEXEC') or die;
@@ -221,18 +224,38 @@ protected static function isException($error)
221224
*/
222225
protected static function logException(\Throwable $error)
223226
{
227+
// Handle common client errors as notices instead of critical errors
228+
if ($error instanceof RouteNotFoundException) {
229+
$level = Log::NOTICE;
230+
$message = \sprintf(
231+
'Page not found (404): %s. Message: "%s"',
232+
Uri::getInstance()->toString(),
233+
$error->getMessage()
234+
);
235+
$category = 'client-error';
236+
} elseif ($error instanceof NotAcceptable) {
237+
$level = Log::NOTICE;
238+
$message = \sprintf(
239+
'Not acceptable (406): %s. Message: "%s"',
240+
Uri::getInstance()->toString(),
241+
$error->getMessage()
242+
);
243+
$category = 'client-error';
244+
} else {
245+
// For all other errors, log a critical error with the full stack trace.
246+
$level = Log::CRITICAL;
247+
$message = \sprintf(
248+
'Uncaught Throwable of type %1$s thrown with message "%2$s". Stack trace: %3$s',
249+
\get_class($error),
250+
$error->getMessage(),
251+
$error->getTraceAsString()
252+
);
253+
$category = 'error';
254+
}
255+
224256
// Try to log the error, but don't let the logging cause a fatal error
225257
try {
226-
Log::add(
227-
\sprintf(
228-
'Uncaught Throwable of type %1$s thrown with message "%2$s". Stack trace: %3$s',
229-
\get_class($error),
230-
$error->getMessage(),
231-
$error->getTraceAsString()
232-
),
233-
Log::CRITICAL,
234-
'error'
235-
);
258+
Log::add($message, $level, $category);
236259
} catch (\Throwable) {
237260
// Logging failed, don't make a stink about it though
238261
}

0 commit comments

Comments
 (0)