Skip to content

Commit

Permalink
Unify error responses and add logging where appropriate
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Jul 13, 2021
1 parent 260c667 commit ae4a163
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

namespace OCA\Text\Controller;

use Exception;
use OCA\Text\AppInfo\Application;
use OCA\Text\Service\WorkspaceService;
use OCP\AppFramework\Http;
Expand All @@ -57,11 +58,13 @@
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\StorageNotAvailableException;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;

class WorkspaceController extends OCSController {

Expand All @@ -86,7 +89,10 @@ class WorkspaceController extends OCSController {
/** @var IEventDispatcher */
private $eventDispatcher;

public function __construct($appName, IRequest $request, IRootFolder $rootFolder, IManager $shareManager, IDirectEditingManager $directEditingManager, IURLGenerator $urlGenerator, WorkspaceService $workspaceService, IEventDispatcher $eventDispatcher, $userId) {
/** @var LoggerInterface */
private $logger;

public function __construct($appName, IRequest $request, IRootFolder $rootFolder, IManager $shareManager, IDirectEditingManager $directEditingManager, IURLGenerator $urlGenerator, WorkspaceService $workspaceService, IEventDispatcher $eventDispatcher, LoggerInterface $logger, $userId) {
parent::__construct($appName, $request);
$this->rootFolder = $rootFolder;
$this->shareManager = $shareManager;
Expand All @@ -95,6 +101,7 @@ public function __construct($appName, IRequest $request, IRootFolder $rootFolder
$this->directEditingManager = $directEditingManager;
$this->urlGenerator = $urlGenerator;
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -130,10 +137,11 @@ public function folder(string $path = '/'): DataResponse {
]
]);
}
} catch (NotFoundException $e) {
return new DataResponse(['message' => 'No valid folder found'], Http::STATUS_BAD_REQUEST);
} catch (StorageNotAvailableException $e) {
return new DataResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
} catch (NotFoundException | NotPermittedException $e) {
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
} catch (Exception $e) {
$this->logger->error('Failed to get workspace file', ['exception' => $e]);
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
}
}

Expand Down Expand Up @@ -163,12 +171,11 @@ public function publicFolder(string $shareToken, string $path = '/'): DataRespon
]
]);
}
} catch (NotFoundException $e) {
return new DataResponse(['message' => 'No valid folder found'], Http::STATUS_BAD_REQUEST);
} catch (ShareNotFound $e) {
return new DataResponse(['message' => 'No valid folder found'], Http::STATUS_BAD_REQUEST);
} catch (StorageNotAvailableException $e) {
return new DataResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
} catch (NotFoundException | ShareNotFound | StorageNotAvailableException $e) {
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
} catch (Exception $e) {
$this->logger->error('Failed to get public workspace file', ['exception' => $e]);
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
}
}

Expand All @@ -192,7 +199,7 @@ public function direct(string $path): DataResponse {
]);
}
} catch (Exception $e) {
$this->logger->logException($e, ['message' => 'Exception when creating a new file through direct editing']);
$this->logger->error('Exception when creating a new file through direct editing', ['exception' => $e]);
return new DataResponse('Failed to create file', Http::STATUS_FORBIDDEN);
}
}
Expand Down

0 comments on commit ae4a163

Please sign in to comment.