Skip to content

Commit

Permalink
Fix issue OpenMage#4501 Exception noise from OAuth and REST (API2 )
Browse files Browse the repository at this point in the history
  • Loading branch information
kiatng committed Feb 22, 2025
1 parent cf16ac8 commit 96782c3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
20 changes: 19 additions & 1 deletion app/code/core/Mage/Api2/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 7 additions & 3 deletions app/code/core/Mage/Api2/Model/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions app/code/core/Mage/Api2/Model/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 96782c3

Please sign in to comment.