Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify error responses and add logging where appropriate #1716

Merged
merged 1 commit into from
Jul 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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