Skip to content

Commit 96782c3

Browse files
committed
Fix issue #4501 Exception noise from OAuth and REST (API2 )
1 parent cf16ac8 commit 96782c3

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

app/code/core/Mage/Api2/Exception.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,36 @@
2222
*/
2323
class Mage_Api2_Exception extends Exception
2424
{
25+
/**
26+
* Log the exception in the log file?
27+
* @var bool
28+
*/
29+
protected $_shouldLog = true;
30+
2531
/**
2632
* Exception constructor
2733
*
2834
* @param string $message
2935
* @param int $code
36+
* @param bool $shouldLog
3037
*/
31-
public function __construct($message, $code)
38+
public function __construct($message, $code, $shouldLog = true)
3239
{
3340
if ($code <= 100 || $code >= 599) {
3441
throw new Exception(sprintf('Invalid Exception code "%d"', $code));
3542
}
3643

44+
$this->_shouldLog = $shouldLog;
3745
parent::__construct($message, $code);
3846
}
47+
48+
/**
49+
* Check if exception should be logged
50+
*
51+
* @return bool
52+
*/
53+
public function shouldLog()
54+
{
55+
return $this->_shouldLog;
56+
}
3957
}

app/code/core/Mage/Api2/Model/Resource.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,21 +613,25 @@ protected function _render($data)
613613
*
614614
* @param string $message
615615
* @param int $code
616+
* @param bool $shouldLog Log the error in the log file?
616617
* @throws Mage_Api2_Exception
617618
*/
618-
protected function _critical($message, $code = null)
619+
protected function _critical($message, $code = null, $shouldLog = true)
619620
{
620621
if ($code === null) {
621622
$errors = $this->_getCriticalErrors();
622623
if (!isset($errors[$message])) {
623624
throw new Exception(
624625
sprintf('Invalid error "%s" or error code missed.', $message),
625-
Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR,
626+
Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR
626627
);
627628
}
628629
$code = $errors[$message];
629630
}
630-
throw new Mage_Api2_Exception($message, $code);
631+
632+
Mage::dispatchEvent('api2_resource_critical', ['resource' => $this, 'message' => $message, 'code' => $code]);
633+
634+
throw new Mage_Api2_Exception($message, $code, $shouldLog);
631635
}
632636

633637
/**

app/code/core/Mage/Api2/Model/Server.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ public function run()
101101
if ($response->isException()) {
102102
throw new Mage_Api2_Exception('Unhandled simple errors.', self::HTTP_INTERNAL_ERROR);
103103
}
104+
} catch (Mage_Api2_Exception $e) {
105+
if ($e->shouldLog()) {
106+
Mage::logException($e);
107+
}
108+
$this->_renderException($e, $renderer, $response);
104109
} catch (Exception $e) {
105110
Mage::logException($e);
106111
$this->_renderException($e, $renderer, $response);

0 commit comments

Comments
 (0)