Skip to content

Commit c9d35f7

Browse files
committed
chore(cleanup): error handling
* `checkSharePermissions` has not been throwing `NotPermittedException` for a while. See #3765 . * `InvalidArgumentException` was not being handled. * throw `NotFoundException` if file cannot be found and there is no share token. Signed-off-by: Max <max@nextcloud.com>
1 parent b1645f9 commit c9d35f7

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

lib/Controller/PublicSessionController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,8 @@ public function create(string $token, ?string $filePath = null, ?string $baseVer
8080
*/
8181
try {
8282
$this->fileService->checkSharePermissions($token, Constants::PERMISSION_READ);
83-
} catch (NotFoundException) {
83+
} catch (NotFoundException | \InvalidArgumentException) {
8484
return new DataResponse([], Http::STATUS_NOT_FOUND);
85-
} catch (NotPermittedException) {
86-
return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], Http::STATUS_NOT_FOUND);
8785
}
8886

8987
return $this->apiService->create($file, $baseVersionEtag, $token, $guestName);

lib/Service/FileService.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public function getFileForSession(Session $session, ?string $shareToken = null):
4040
if (!$session->isGuest()) {
4141
try {
4242
return $this->getFileById($session->getDocumentId(), $session->getUserId());
43-
} catch (NotFoundException) {
43+
} catch (NotFoundException $e) {
44+
if ($shareToken === null) {
45+
throw $e;
46+
}
4447
// We may still have a user session but on a public share link so move on
4548
}
4649
}
@@ -115,7 +118,8 @@ public function getFileById(int $fileId, string $userId): File {
115118
}
116119

117120
/**
118-
* @throws NotFoundException
121+
* @throws NotFoundException if the share cannot be found based on the token
122+
* @throws \InvalidArgumentException if the share is not a File share and path is omitted.
119123
*/
120124
public function getFileByShareToken(string $shareToken, ?string $path = null): File {
121125
try {
@@ -131,7 +135,7 @@ public function getFileByShareToken(string $shareToken, ?string $path = null): F
131135
if ($node instanceof File) {
132136
return $node;
133137
}
134-
throw new \InvalidArgumentException('No proper share data');
138+
throw new \InvalidArgumentException('Invalid share data.');
135139
}
136140

137141
public function isReadOnly(File $file, ?string $token): bool {
@@ -166,7 +170,7 @@ public function isDownloadDisabled(File $file): bool {
166170
*
167171
* @return void
168172
*
169-
* @throws NotFoundException|NotPermittedException
173+
* @throws NotFoundException
170174
*
171175
* @psalm-param 1|2 $permission
172176
*/

0 commit comments

Comments
 (0)