Skip to content

Commit fb5acf6

Browse files
kiatngsreichel
andauthored
Fix issue #4501 Exception noise from OAuth and REST (API2 ) (#4642)
* Fix issue #4501 Exception noise from OAuth and REST (API2 ) * Update app/code/core/Mage/Api2/Model/Resource.php * Update app/code/core/Mage/Api2/Exception.php Co-authored-by: Sven Reichel <[email protected]> * Update app/code/core/Mage/Api2/Exception.php Co-authored-by: Sven Reichel <[email protected]> * Update app/code/core/Mage/Api2/Exception.php Co-authored-by: Sven Reichel <[email protected]> * Update app/code/core/Mage/Api2/Exception.php --------- Co-authored-by: Sven Reichel <[email protected]>
1 parent 0ed04dd commit fb5acf6

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,33 @@
1414
*/
1515
class Mage_Api2_Exception extends Exception
1616
{
17+
/**
18+
* Log the exception in the log file?
19+
*/
20+
protected bool $shouldLog = true;
21+
1722
/**
1823
* Exception constructor
1924
*
2025
* @param string $message
2126
* @param int $code
27+
* @param bool $shouldLog
2228
*/
23-
public function __construct($message, $code)
29+
public function __construct($message, $code, $shouldLog = true)
2430
{
2531
if ($code <= 100 || $code >= 599) {
2632
throw new Exception(sprintf('Invalid Exception code "%d"', $code));
2733
}
2834

35+
$this->shouldLog = $shouldLog;
2936
parent::__construct($message, $code);
3037
}
38+
39+
/**
40+
* Check if exception should be logged
41+
*/
42+
public function shouldLog(): bool
43+
{
44+
return $this->shouldLog;
45+
}
3146
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,10 @@ protected function _render($data)
601601
*
602602
* @param string $message
603603
* @param int $code
604+
* @param bool $shouldLog Log the error in the log file?
604605
* @throws Mage_Api2_Exception
605606
*/
606-
protected function _critical($message, $code = null)
607+
protected function _critical($message, $code = null, $shouldLog = true)
607608
{
608609
if ($code === null) {
609610
$errors = $this->_getCriticalErrors();
@@ -615,7 +616,10 @@ protected function _critical($message, $code = null)
615616
}
616617
$code = $errors[$message];
617618
}
618-
throw new Mage_Api2_Exception($message, $code);
619+
620+
Mage::dispatchEvent('api2_resource_critical', ['resource' => $this, 'message' => $message, 'code' => $code]);
621+
622+
throw new Mage_Api2_Exception($message, $code, $shouldLog);
619623
}
620624

621625
/**

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public function run()
9393
if ($response->isException()) {
9494
throw new Mage_Api2_Exception('Unhandled simple errors.', self::HTTP_INTERNAL_ERROR);
9595
}
96+
} catch (Mage_Api2_Exception $e) {
97+
if ($e->shouldLog()) {
98+
Mage::logException($e);
99+
}
100+
$this->_renderException($e, $renderer, $response);
96101
} catch (Exception $e) {
97102
Mage::logException($e);
98103
$this->_renderException($e, $renderer, $response);

0 commit comments

Comments
 (0)