From c38f668c6da34a1d2c4afd990c558f8563a467c0 Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Mon, 7 Sep 2015 13:44:44 +0200 Subject: Log exceptions in NotFoundEnvException directly --- environment/environment.php | 39 +++++++++++++----------------------- environment/environmentexception.php | 7 ++++--- environment/notfoundenvexception.php | 2 +- 3 files changed, 19 insertions(+), 29 deletions(-) (limited to 'environment') diff --git a/environment/environment.php b/environment/environment.php index c0ce95f3..d5ee2385 100644 --- a/environment/environment.php +++ b/environment/environment.php @@ -173,19 +173,18 @@ class Environment { * * @return File|Folder * - * @throws EnvironmentException + * @throws NotFoundEnvException */ public function getNodeFromUserFolder($path) { - $node = false; $folder = $this->userFolder; if ($folder === null) { - $this->logAndThrowNotFound("Could not access the user's folder"); + throw new NotFoundEnvException("Could not access the user's folder"); } else { try { $node = $folder->get($path); } catch (NotFoundException $exception) { $message = 'Could not find anything at: ' . $exception->getMessage(); - $this->logAndThrowNotFound($message); + throw new NotFoundEnvException($message); } } @@ -199,12 +198,12 @@ class Environment { * * @return Node * - * @throws EnvironmentException + * @throws NotFoundEnvException */ public function getResourceFromId($resourceId) { $resourcesArray = $this->userFolder->getById($resourceId); if ($resourcesArray[0] === null) { - $this->logAndThrowNotFound('Could not locate file linked to ID: ' . $resourceId); + throw new NotFoundEnvException('Could not locate file linked to ID: ' . $resourceId); } return $resourcesArray[0]; @@ -223,17 +222,17 @@ class Environment { * Returns the virtual root where the user lands after logging in or when following a link * * @return Folder - * - * @throws EnvironmentException + * @throws NotFoundEnvException */ public function getVirtualRootFolder() { $rootFolder = $this->userFolder; if (!empty($this->sharedNodeId)) { $node = $this->getSharedNode(); - if ($node->getType() === 'dir') { + $nodeType = $node->getType(); + if ($nodeType === 'dir') { $rootFolder = $node; } else { - $this->logAndThrowNotFound($node->getPath() . ' is not a folder'); + throw new NotFoundEnvException($node->getPath() . ' is not a folder'); } } @@ -253,6 +252,7 @@ class Environment { * Returns the name of the user sharing files publicly * * @return string + * @throws NotFoundEnvException */ public function getDisplayName() { $user = null; @@ -262,7 +262,7 @@ class Environment { $user = $this->userManager->get($userId); } if ($user === null) { - $this->logAndThrowNotFound('Could not find user'); + throw new NotFoundEnvException('Could not find user'); } return $user->getDisplayName(); @@ -320,9 +320,10 @@ class Environment { */ public function getPathFromVirtualRoot($node) { $path = $node->getPath(); + $nodeType = $node->getType(); - if ($node->getType() === 'dir') { - // Needed because fromRootToFolder always ends with a slash + // Needed because fromRootToFolder always ends with a slash + if ($nodeType === 'dir') { $path .= '/'; } @@ -370,16 +371,4 @@ class Environment { return $origShareRelPath; } - /** - * Logs the error and raises an exception - * - * @param string $message - * - * @throws NotFoundEnvException - */ - private function logAndThrowNotFound($message) { - $this->logger->error($message . ' (404)'); - throw new NotFoundEnvException($message); - } - } diff --git a/environment/environmentexception.php b/environment/environmentexception.php index da5fa568..1aea909d 100644 --- a/environment/environmentexception.php +++ b/environment/environmentexception.php @@ -12,12 +12,12 @@ namespace OCA\Gallery\Environment; -use Exception; +use OCP\Util; /** - * Thrown when the service cannot reply to a request + * Thrown when the Environment runs into a problem */ -class EnvironmentException extends Exception { +class EnvironmentException extends \Exception { /** * Constructor @@ -25,6 +25,7 @@ class EnvironmentException extends Exception { * @param string $msg the message contained in the exception */ public function __construct($msg) { + Util::writeLog('gallery', 'Exception' . $msg, Util::ERROR); parent::__construct($msg); } } diff --git a/environment/notfoundenvexception.php b/environment/notfoundenvexception.php index 6504a11e..85030480 100644 --- a/environment/notfoundenvexception.php +++ b/environment/notfoundenvexception.php @@ -13,6 +13,6 @@ namespace OCA\Gallery\Environment; /** - * Thrown when the service cannot reply to a request + * Thrown when the Environment cannot find or access a node */ class NotFoundEnvException extends EnvironmentException {} -- cgit v1.2.3