diff options
author | mwithheld <796986+mwithheld@users.noreply.github.com> | 2021-09-14 11:36:22 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 11:36:22 +0300 |
commit | ea767298be31720dedcad13e43710d9593aca75a (patch) | |
tree | 4b2cfaf73d752915280ad509f61078968c0b9de1 | |
parent | 53c528de02fa8dfbd77b9bfe392fc42976038c2e (diff) |
#17812 Improve handling of NotSupportedBrowserException (#17915)
* #17812 Improve handling of NotSupportedBrowserException
* Fix HttpCodeException namespace
Co-authored-by: Stefan Giehl <stefan@matomo.org>
Co-authored-by: Stefan Giehl <stefan@matomo.org>
-rw-r--r-- | core/Exception/NotSupportedBrowserException.php | 7 | ||||
-rw-r--r-- | core/ExceptionHandler.php | 51 |
2 files changed, 41 insertions, 17 deletions
diff --git a/core/Exception/NotSupportedBrowserException.php b/core/Exception/NotSupportedBrowserException.php index ebcd55f990..37f60d9b82 100644 --- a/core/Exception/NotSupportedBrowserException.php +++ b/core/Exception/NotSupportedBrowserException.php @@ -8,6 +8,11 @@ */ namespace Piwik\Exception; -class NotSupportedBrowserException extends Exception +class NotSupportedBrowserException extends \Piwik\Exception\Exception { + public function __construct($message) + { + // Use HTTP code 400 to avoid server-provided 403 error page. + parent::__construct($message, $code = 400); + } } diff --git a/core/ExceptionHandler.php b/core/ExceptionHandler.php index 6d959c6765..a6af86354f 100644 --- a/core/ExceptionHandler.php +++ b/core/ExceptionHandler.php @@ -13,8 +13,6 @@ use Exception; use Piwik\API\Request; use Piwik\API\ResponseBuilder; use Piwik\Container\ContainerDoesNotExistException; -use Piwik\Exception\NotYetInstalledException; -use Piwik\Http\HttpCodeException; use Piwik\Container\StaticContainer; use Piwik\Plugins\CoreAdminHome\CustomLogo; use Piwik\Plugins\Monolog\Processor\ExceptionToTextProcessor; @@ -72,17 +70,28 @@ class ExceptionHandler */ public static function dieWithHtmlErrorPage($exception) { - if ($exception instanceof HttpCodeException - && $exception->getCode() > 0 - ) { - http_response_code($exception->getCode()); - } elseif ($exception instanceof NotYetInstalledException) { - http_response_code(404); - } else { - http_response_code(500); + // Set an appropriate HTTP response code. + switch (true) { + case ( ($exception instanceof \Piwik\Http\HttpCodeException || $exception instanceof \Piwik\Exception\NotSupportedBrowserException) && $exception->getCode() > 0): + // For these exception types, use the exception-provided error code. + http_response_code($exception->getCode()); + break; + case ($exception instanceof \Piwik\Exception\NotYetInstalledException): + http_response_code(404); + break; + default: + http_response_code(500); } - self::logException($exception); + // Log the error with an appropriate loglevel. + switch (true) { + case ($exception instanceof \Piwik\Exception\NotSupportedBrowserException): + // These unsupported browsers are really a client-side problem, so log only at DEBUG level. + self::logException($exception, Log::DEBUG); + break; + default: + self::logException($exception); + } Common::sendHeader('Content-Type: text/html; charset=utf-8'); @@ -157,13 +166,23 @@ class ExceptionHandler return $result; } - private static function logException($exception) + private static function logException($exception, $loglevel=Log::ERROR) { try { - StaticContainer::get(LoggerInterface::class)->error('Uncaught exception: {exception}', [ - 'exception' => $exception, - 'ignoreInScreenWriter' => true, - ]); + switch ($loglevel) { + case(Log::DEBUG): + StaticContainer::get(LoggerInterface::class)->debug('Uncaught exception: {exception}', [ + 'exception' => $exception, + 'ignoreInScreenWriter' => true, + ]); + break; + case(Log::ERROR): + default: + StaticContainer::get(LoggerInterface::class)->error('Uncaught exception: {exception}', [ + 'exception' => $exception, + 'ignoreInScreenWriter' => true, + ]); + } } catch (DependencyException $ex) { // ignore (occurs if exception is thrown when resolving DI entries) } catch (ContainerDoesNotExistException $ex) { |