From 15e5a432b631a9eb8a464f3d6082702e6cb348cc Mon Sep 17 00:00:00 2001 From: Julius Haertl Date: Mon, 27 Feb 2017 11:27:49 +0100 Subject: [PATCH] Improve logging of exceptions catched by SharingMiddleware --- lib/AppInfo/Application.php | 7 ++--- lib/Middleware/SharingMiddleware.php | 29 ++++++++++++++---- .../unit/Middleware/SharingMiddlewareTest.php | 30 +++++++++---------- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index aee4a6f04..befeb8f6e 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -42,11 +42,8 @@ class Application extends App { $container->registerService('SharingMiddleware', function($container) use ($server) { return new SharingMiddleware( - $container, - $server->getRequest(), - $server->getUserSession(), - $container->query('ControllerMethodReflector'), - $container->query('OCA\Deck\Service\PermissionService') + $server->getLogger(), + $server->getConfig() ); }); $container->registerMiddleware('SharingMiddleware'); diff --git a/lib/Middleware/SharingMiddleware.php b/lib/Middleware/SharingMiddleware.php index 5ade88467..c1df3aef9 100644 --- a/lib/Middleware/SharingMiddleware.php +++ b/lib/Middleware/SharingMiddleware.php @@ -23,18 +23,32 @@ namespace OCA\Deck\Middleware; - - - use OCA\Deck\StatusException; -use \OCP\AppFramework\Middleware; - - +use OCP\AppFramework\Middleware; use OCP\AppFramework\Http\JSONResponse; +use OCP\ILogger; +use OCP\Util; +use OCP\IConfig; class SharingMiddleware extends Middleware { + /** @var ILogger */ + private $logger; + /** @var IConfig */ + private $config; + + /** + * SharingMiddleware constructor. + * + * @param ILogger $logger + * @param IConfig $config + */ + public function __construct(ILogger $logger, IConfig $config) { + $this->logger = $logger; + $this->config = $config; + } + /** * Return JSON error response if the user has no sufficient permission * @@ -46,6 +60,9 @@ class SharingMiddleware extends Middleware { */ public function afterException($controller, $methodName, \Exception $exception) { if ($exception instanceof StatusException) { + if($this->config->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { + $this->logger->logException($exception); + } return new JSONResponse([ "status" => $exception->getStatus(), "message" => $exception->getMessage() diff --git a/tests/unit/Middleware/SharingMiddlewareTest.php b/tests/unit/Middleware/SharingMiddlewareTest.php index dd59b6387..9757197d9 100644 --- a/tests/unit/Middleware/SharingMiddlewareTest.php +++ b/tests/unit/Middleware/SharingMiddlewareTest.php @@ -23,30 +23,28 @@ namespace OCA\Deck\Middleware; -use OC\AppFramework\DependencyInjection\DIContainer; -use OC\AppFramework\Utility\ControllerMethodReflector; -use OC\AppFramework\Utility\SimpleContainer; -use OCA\Deck\Db\DeckMapper; -use OCA\Deck\Db\IPermissionMapper; use OCA\Deck\NoPermissionException; use OCA\Deck\NotFoundException; -use OCA\Deck\Service\BoardService; -use OCA\Deck\Service\PermissionService; -use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; -use OCP\IContainer; -use OCP\IGroupManager; -use OCP\IRequest; -use OCP\IUser; -use OCP\IUserSession; -use OCA\Deck\Db\AclMapper; +use OCP\ILogger; +use OCP\IConfig; -class SharingMiddlewareTest extends \PHPUnit_Framework_TestCase { +class SharingMiddlewareTest extends \Test\TestCase { + + /** @var ILogger */ + private $logger; + /** @var IConfig */ + private $config; private $sharingMiddleware; public function setUp() { - $this->sharingMiddleware = new SharingMiddleware(); + $this->logger = $this->createMock(ILogger::class); + $this->config = $this->createMock(IConfig::class); + $this->sharingMiddleware = new SharingMiddleware( + $this->logger, + $this->config + ); }