From 51ea17a3d9f7ab361be11efcd139c49833dc9d7e Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 20 Mar 2024 16:39:33 +0100 Subject: [PATCH] fix(response): Make sure JSONResponse returns valid data Wrap error messages into an array when responding with `JSONResponse`. Signed-off-by: Jonas --- lib/Middleware/SessionMiddleware.php | 2 +- lib/Service/ApiService.php | 18 +++++++++--------- src/components/Editor/DocumentStatus.vue | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/Middleware/SessionMiddleware.php b/lib/Middleware/SessionMiddleware.php index 6697dc544d4..e72646e404d 100644 --- a/lib/Middleware/SessionMiddleware.php +++ b/lib/Middleware/SessionMiddleware.php @@ -143,7 +143,7 @@ private function assertUserOrShareToken(ISessionAwareController $controller): vo public function afterException($controller, $methodName, \Exception $exception): JSONResponse|Response { if ($exception instanceof InvalidDocumentBaseVersionEtagException) { - return new JSONResponse($this->l10n->t('Editing session has expired. Please reload the page.'), Http::STATUS_PRECONDITION_FAILED); + return new JSONResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED); } if ($exception instanceof InvalidSessionException) { diff --git a/lib/Service/ApiService.php b/lib/Service/ApiService.php index 1be3bf51d97..488b6986db0 100644 --- a/lib/Service/ApiService.php +++ b/lib/Service/ApiService.php @@ -85,17 +85,17 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b } catch (NotFoundException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } catch (NotPermittedException $e) { - return new DataResponse($this->l10n->t('This file cannot be displayed as download is disabled by the share'), 404); + return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], 404); } } elseif ($fileId) { try { $file = $this->documentService->getFileById($fileId); } catch (NotFoundException|NotPermittedException $e) { $this->logger->error('No permission to access this file', [ 'exception' => $e ]); - return new DataResponse($this->l10n->t('No permission to access this file.'), Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => $this->l10n->t('No permission to access this file.')], Http::STATUS_NOT_FOUND); } } else { - return new DataResponse('No valid file argument provided', Http::STATUS_PRECONDITION_FAILED); + return new DataResponse(['error' => 'No valid file argument provided'], Http::STATUS_PRECONDITION_FAILED); } $storage = $file->getStorage(); @@ -106,7 +106,7 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b $share = $storage->getShare(); $shareAttribtues = $share->getAttributes(); if ($shareAttribtues !== null && $shareAttribtues->getAttribute('permissions', 'download') === false) { - return new DataResponse($this->l10n->t('This file cannot be displayed as download is disabled by the share'), 403); + return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], 403); } } @@ -116,7 +116,7 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b $document = $this->documentService->getDocument($file->getId()); $freshSession = $document === null; if ($baseVersionEtag && $baseVersionEtag !== $document?->getBaseVersionEtag()) { - return new DataResponse($this->l10n->t('Editing session has expired. Please reload the page.'), Http::STATUS_PRECONDITION_FAILED); + return new DataResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED); } if ($freshSession) { @@ -132,7 +132,7 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b } } catch (Exception $e) { $this->logger->error($e->getMessage(), ['exception' => $e]); - return new DataResponse('Failed to create the document session', 500); + return new DataResponse(['error' => 'Failed to create the document session'], 500); } /** @var Document $document */ @@ -193,7 +193,7 @@ public function push(Session $session, Document $document, int $version, array $ $session = $this->sessionService->updateSessionAwareness($session, $awareness); } catch (DoesNotExistException $e) { // Session was removed in the meantime. #3875 - return new DataResponse($this->l10n->t('Editing session has expired. Please reload the page.'), Http::STATUS_PRECONDITION_FAILED); + return new DataResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED); } if (empty($steps)) { return new DataResponse([]); @@ -201,10 +201,10 @@ public function push(Session $session, Document $document, int $version, array $ try { $result = $this->documentService->addStep($document, $session, $steps, $version, $token); } catch (InvalidArgumentException $e) { - return new DataResponse($e->getMessage(), 422); + return new DataResponse(['error' => $e->getMessage()], 422); } catch (DoesNotExistException|NotPermittedException) { // Either no write access or session was removed in the meantime (#3875). - return new DataResponse($this->l10n->t('Editing session has expired. Please reload the page.'), Http::STATUS_PRECONDITION_FAILED); + return new DataResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED); } return new DataResponse($result); } diff --git a/src/components/Editor/DocumentStatus.vue b/src/components/Editor/DocumentStatus.vue index 014ac7a25ee..0061099826c 100644 --- a/src/components/Editor/DocumentStatus.vue +++ b/src/components/Editor/DocumentStatus.vue @@ -24,7 +24,7 @@

- {{ syncError.data.data }} + {{ syncError.data.data.error }} {{ t('text', 'Reload') }}