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);