From 96782c38961b05fdc28ae930d6b42abc8d86f7df Mon Sep 17 00:00:00 2001 From: Ng Kiat Siong Date: Sat, 22 Feb 2025 14:03:49 +0800 Subject: [PATCH 1/2] Fix issue #4501 Exception noise from OAuth and REST (API2 ) --- app/code/core/Mage/Api2/Exception.php | 20 +++++++++++++++++++- app/code/core/Mage/Api2/Model/Resource.php | 10 +++++++--- app/code/core/Mage/Api2/Model/Server.php | 5 +++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/code/core/Mage/Api2/Exception.php b/app/code/core/Mage/Api2/Exception.php index cf1a4d228d6..d8a1dfbdc26 100644 --- a/app/code/core/Mage/Api2/Exception.php +++ b/app/code/core/Mage/Api2/Exception.php @@ -22,18 +22,36 @@ */ class Mage_Api2_Exception extends Exception { + /** + * Log the exception in the log file? + * @var bool + */ + protected $_shouldLog = true; + /** * Exception constructor * * @param string $message * @param int $code + * @param bool $shouldLog */ - public function __construct($message, $code) + public function __construct($message, $code, $shouldLog = true) { if ($code <= 100 || $code >= 599) { throw new Exception(sprintf('Invalid Exception code "%d"', $code)); } + $this->_shouldLog = $shouldLog; parent::__construct($message, $code); } + + /** + * Check if exception should be logged + * + * @return bool + */ + public function shouldLog() + { + return $this->_shouldLog; + } } diff --git a/app/code/core/Mage/Api2/Model/Resource.php b/app/code/core/Mage/Api2/Model/Resource.php index c22360c42d0..32559f0c719 100644 --- a/app/code/core/Mage/Api2/Model/Resource.php +++ b/app/code/core/Mage/Api2/Model/Resource.php @@ -613,21 +613,25 @@ protected function _render($data) * * @param string $message * @param int $code + * @param bool $shouldLog Log the error in the log file? * @throws Mage_Api2_Exception */ - protected function _critical($message, $code = null) + protected function _critical($message, $code = null, $shouldLog = true) { if ($code === null) { $errors = $this->_getCriticalErrors(); if (!isset($errors[$message])) { throw new Exception( sprintf('Invalid error "%s" or error code missed.', $message), - Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR, + Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR ); } $code = $errors[$message]; } - throw new Mage_Api2_Exception($message, $code); + + Mage::dispatchEvent('api2_resource_critical', ['resource' => $this, 'message' => $message, 'code' => $code]); + + throw new Mage_Api2_Exception($message, $code, $shouldLog); } /** diff --git a/app/code/core/Mage/Api2/Model/Server.php b/app/code/core/Mage/Api2/Model/Server.php index 1897a52cb2f..4e3da639720 100644 --- a/app/code/core/Mage/Api2/Model/Server.php +++ b/app/code/core/Mage/Api2/Model/Server.php @@ -101,6 +101,11 @@ public function run() if ($response->isException()) { throw new Mage_Api2_Exception('Unhandled simple errors.', self::HTTP_INTERNAL_ERROR); } + } catch (Mage_Api2_Exception $e) { + if ($e->shouldLog()) { + Mage::logException($e); + } + $this->_renderException($e, $renderer, $response); } catch (Exception $e) { Mage::logException($e); $this->_renderException($e, $renderer, $response); From 12db8ae262fa08d79d41fe9ade237cc2cf17f3e8 Mon Sep 17 00:00:00 2001 From: Ng Kiat Siong Date: Sat, 22 Feb 2025 14:22:51 +0800 Subject: [PATCH 2/2] Update app/code/core/Mage/Api2/Model/Resource.php --- app/code/core/Mage/Api2/Model/Resource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/core/Mage/Api2/Model/Resource.php b/app/code/core/Mage/Api2/Model/Resource.php index 32559f0c719..a6daa69ff19 100644 --- a/app/code/core/Mage/Api2/Model/Resource.php +++ b/app/code/core/Mage/Api2/Model/Resource.php @@ -623,7 +623,7 @@ protected function _critical($message, $code = null, $shouldLog = true) if (!isset($errors[$message])) { throw new Exception( sprintf('Invalid error "%s" or error code missed.', $message), - Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR + Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR, ); } $code = $errors[$message];