|
10 | 10 | namespace Joomla\CMS\Exception; |
11 | 11 |
|
12 | 12 | use Joomla\CMS\Application\CMSApplication; |
| 13 | +use Joomla\CMS\Application\Exception\NotAcceptable; |
13 | 14 | use Joomla\CMS\Error\AbstractRenderer; |
14 | 15 | use Joomla\CMS\Event\Application\AfterInitialiseDocumentEvent; |
15 | 16 | use Joomla\CMS\Factory; |
16 | 17 | use Joomla\CMS\Log\Log; |
| 18 | +use Joomla\CMS\Router\Exception\RouteNotFoundException; |
| 19 | +use Joomla\CMS\Uri\Uri; |
17 | 20 |
|
18 | 21 | // phpcs:disable PSR1.Files.SideEffects |
19 | 22 | \defined('_JEXEC') or die; |
@@ -221,18 +224,38 @@ protected static function isException($error) |
221 | 224 | */ |
222 | 225 | protected static function logException(\Throwable $error) |
223 | 226 | { |
| 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 | + |
224 | 256 | // Try to log the error, but don't let the logging cause a fatal error |
225 | 257 | 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); |
236 | 259 | } catch (\Throwable) { |
237 | 260 | // Logging failed, don't make a stink about it though |
238 | 261 | } |
|
0 commit comments