From 35d22348208321d0dd6f56e035ddb91fc4653c16 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Tue, 22 Oct 2024 08:20:04 +0200 Subject: [PATCH] fix(files_sharing): Cleanup error messages Signed-off-by: provokateurin --- .../lib/Controller/ShareAPIController.php | 19 ++++++++++--------- apps/files_sharing/tests/ApiTest.php | 2 +- .../Controller/ShareAPIControllerTest.php | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/apps/files_sharing/lib/Controller/ShareAPIController.php b/apps/files_sharing/lib/Controller/ShareAPIController.php index 266c1f9474ce0..7de1376b95158 100644 --- a/apps/files_sharing/lib/Controller/ShareAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareAPIController.php @@ -190,7 +190,7 @@ protected function formatShare(IShare $share, Node $recipientNode = null): array if ($isOwnShare) { $result['item_permissions'] = $node->getPermissions(); } - + // If we're on the recipient side, the node permissions // are bound to the share permissions. So we need to // adjust the permissions to the share permissions if necessary. @@ -675,7 +675,7 @@ public function createShare( $expireDateTime = $this->parseDate($expireDate); $share->setExpirationDate($expireDateTime); } catch (\Exception $e) { - throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD')); + throw new OCSNotFoundException($e->getMessage(), $e); } } else { // Client sent empty string for expire date. @@ -826,12 +826,11 @@ public function createShare( try { $share = $this->shareManager->createShare($share); } catch (GenericShareException $e) { - \OC::$server->getLogger()->logException($e); $code = $e->getCode() === 0 ? 403 : $e->getCode(); throw new OCSException($e->getHint(), $code); } catch (\Exception $e) { - \OC::$server->getLogger()->logException($e); - throw new OCSForbiddenException($e->getMessage(), $e); + $this->logger->error($e->getMessage(), ['exception' => $e]); + throw new OCSForbiddenException('Failed to create share.', $e); } $output = $this->formatShare($share); @@ -1352,11 +1351,11 @@ public function updateShare( $share->setExpirationDate(null); } elseif ($expireDate !== null) { try { - $expireDate = $this->parseDate($expireDate); + $expireDateTime = $this->parseDate($expireDate); + $share->setExpirationDate($expireDateTime); } catch (\Exception $e) { throw new OCSBadRequestException($e->getMessage(), $e); } - $share->setExpirationDate($expireDate); } try { @@ -1365,7 +1364,8 @@ public function updateShare( $code = $e->getCode() === 0 ? 403 : $e->getCode(); throw new OCSException($e->getHint(), (int)$code); } catch (\Exception $e) { - throw new OCSBadRequestException($e->getMessage(), $e); + $this->logger->error($e->getMessage(), ['exception' => $e]); + throw new OCSBadRequestException('Failed to update share.', $e); } return new DataResponse($this->formatShare($share)); @@ -1456,7 +1456,8 @@ public function acceptShare(string $id): DataResponse { $code = $e->getCode() === 0 ? 403 : $e->getCode(); throw new OCSException($e->getHint(), (int)$code); } catch (\Exception $e) { - throw new OCSBadRequestException($e->getMessage(), $e); + $this->logger->error($e->getMessage(), ['exception' => $e]); + throw new OCSBadRequestException('Failed to accept share.', $e); } return new DataResponse(); diff --git a/apps/files_sharing/tests/ApiTest.php b/apps/files_sharing/tests/ApiTest.php index cdb6d2ddcca0c..1dc52d9f58674 100644 --- a/apps/files_sharing/tests/ApiTest.php +++ b/apps/files_sharing/tests/ApiTest.php @@ -1317,7 +1317,7 @@ public function testPublicLinkExpireDate($date, $valid) { $this->assertTrue($valid); } catch (OCSNotFoundException $e) { $this->assertFalse($valid); - $this->assertEquals('Invalid date, date format must be YYYY-MM-DD', $e->getMessage()); + $this->assertEquals('Invalid date. Format must be YYYY-MM-DD', $e->getMessage()); $ocs->cleanup(); return; } diff --git a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php index 1c7937ea01fb6..62aaf14f82e89 100644 --- a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php @@ -2208,7 +2208,7 @@ public function testCreateShareValidExpireDate() { public function testCreateShareInvalidExpireDate() { $this->expectException(\OCP\AppFramework\OCS\OCSNotFoundException::class); - $this->expectExceptionMessage('Invalid date, date format must be YYYY-MM-DD'); + $this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD'); $ocs = $this->mockFormatShare();