Skip to content

Commit

Permalink
fix(response): Make sure JSONResponse returns valid data
Browse files Browse the repository at this point in the history
Wrap error messages into an array when responding with `JSONResponse`.

Signed-off-by: Jonas <jonas@freesources.org>

[skip ci]
  • Loading branch information
mejo- authored and backportbot[bot] committed Mar 21, 2024
1 parent 59bb574 commit ed4ce31
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/Middleware/SessionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ public function create($fileId = null, $filePath = null, ?string $token = null,
} 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();
Expand All @@ -108,7 +108,7 @@ public function create($fileId = null, $filePath = null, ?string $token = null,
$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);
}
}

Expand All @@ -118,7 +118,7 @@ public function create($fileId = null, $filePath = null, ?string $token = null,
$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) {
Expand All @@ -134,7 +134,7 @@ public function create($fileId = null, $filePath = null, ?string $token = null,
}
} 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);
}

$session = $this->sessionService->initSession($document->getId(), $guestName);
Expand Down Expand Up @@ -193,18 +193,18 @@ public function push(Session $session, Document $document, $version, $steps, $aw
$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([]);
}
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);
}
Expand Down

0 comments on commit ed4ce31

Please sign in to comment.