only execute code if exception was thrown by the corresponding controller

Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
This commit is contained in:
Bjoern Schiessle 2017-03-13 12:13:57 +01:00
parent faf836b0c0
commit 4531b4a81d
No known key found for this signature in database
GPG Key ID: 2378A753E2BF04F6
2 changed files with 7 additions and 3 deletions

View File

@ -25,6 +25,7 @@
namespace OCA\Federation\Middleware; namespace OCA\Federation\Middleware;
use OC\HintException; use OC\HintException;
use OCA\Federation\Controller\SettingsController;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Middleware; use OCP\AppFramework\Middleware;
@ -57,6 +58,9 @@ class AddServerMiddleware extends Middleware {
* @return JSONResponse * @return JSONResponse
*/ */
public function afterException($controller, $methodName, \Exception $exception) { public function afterException($controller, $methodName, \Exception $exception) {
if (($controller instanceof SettingsController) === false) {
throw $exception;
}
$this->logger->error($exception->getMessage(), ['app' => $this->appName]); $this->logger->error($exception->getMessage(), ['app' => $this->appName]);
if ($exception instanceof HintException) { if ($exception instanceof HintException) {
$message = $exception->getHint(); $message = $exception->getHint();

View File

@ -26,8 +26,8 @@ namespace OCA\Federation\Tests\Middleware;
use OC\HintException; use OC\HintException;
use OCA\Federation\Controller\SettingsController;
use OCA\Federation\Middleware\AddServerMiddleware; use OCA\Federation\Middleware\AddServerMiddleware;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
@ -44,7 +44,7 @@ class AddServerMiddlewareTest extends TestCase {
/** @var AddServerMiddleware */ /** @var AddServerMiddleware */
private $middleware; private $middleware;
/** @var \PHPUnit_Framework_MockObject_MockObject | Controller */ /** @var \PHPUnit_Framework_MockObject_MockObject | SettingsController */
private $controller; private $controller;
public function setUp() { public function setUp() {
@ -52,7 +52,7 @@ class AddServerMiddlewareTest extends TestCase {
$this->logger = $this->getMockBuilder(ILogger::class)->getMock(); $this->logger = $this->getMockBuilder(ILogger::class)->getMock();
$this->l10n = $this->getMockBuilder(IL10N::class)->getMock(); $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
$this->controller = $this->getMockBuilder(Controller::class) $this->controller = $this->getMockBuilder(SettingsController::class)
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->middleware = new AddServerMiddleware( $this->middleware = new AddServerMiddleware(