Skip to content

Commit 5fab1fc

Browse files
committed
fix(response): Make sure JSONResponse returns valid data
Wrap error messages into an array when responding with `JSONResponse`. Signed-off-by: Jonas <[email protected]>
1 parent 6db8ae7 commit 5fab1fc

3 files changed

Lines changed: 11 additions & 11 deletions

File tree

lib/Middleware/SessionMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private function assertUserOrShareToken(ISessionAwareController $controller): vo
143143

144144
public function afterException($controller, $methodName, \Exception $exception): JSONResponse|Response {
145145
if ($exception instanceof InvalidDocumentBaseVersionEtagException) {
146-
return new JSONResponse($this->l10n->t('Editing session has expired. Please reload the page.'), Http::STATUS_PRECONDITION_FAILED);
146+
return new JSONResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED);
147147
}
148148

149149
if ($exception instanceof InvalidSessionException) {

lib/Service/ApiService.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ public function create(?int $fileId = null, ?string $filePath = null, =string $b
8787
} catch (NotFoundException $e) {
8888
return new DataResponse([], Http::STATUS_NOT_FOUND);
8989
} catch (NotPermittedException $e) {
90-
return new DataResponse($this->l10n->t('This file cannot be displayed as download is disabled by the share'), 404);
90+
return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], 404);
9191
}
9292
} elseif ($fileId) {
9393
try {
9494
$file = $this->documentService->getFileById($fileId);
9595
} catch (NotFoundException|NotPermittedException $e) {
9696
$this->logger->error('No permission to access this file', [ 'exception' => $e ]);
97-
return new DataResponse($this->l10n->t('No permission to access this file.'), Http::STATUS_NOT_FOUND);
97+
return new DataResponse(['error' => $this->l10n->t('No permission to access this file.')], Http::STATUS_NOT_FOUND);
9898
}
9999
} else {
100-
return new DataResponse('No valid file argument provided', Http::STATUS_PRECONDITION_FAILED);
100+
return new DataResponse(['error' => 'No valid file argument provided'], Http::STATUS_PRECONDITION_FAILED);
101101
}
102102

103103
$storage = $file->getStorage();
@@ -108,7 +108,7 @@ public function create(?int $fileId = null, ?string $filePath = null, =string $b
108108
$share = $storage->getShare();
109109
$shareAttribtues = $share->getAttributes();
110110
if ($shareAttribtues !== null && $shareAttribtues->getAttribute('permissions', 'download') === false) {
111-
return new DataResponse($this->l10n->t('This file cannot be displayed as download is disabled by the share'), 403);
111+
return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], 403);
112112
}
113113
}
114114

@@ -118,7 +118,7 @@ public function create(?int $fileId = null, ?string $filePath = null, =string $b
118118
$document = $this->documentService->getDocument($file->getId());
119119
$freshSession = $document === null;
120120
if ($baseVersionEtag && $baseVersionEtag !== $document?->getBaseVersionEtag()) {
121-
return new DataResponse($this->l10n->t('Editing session has expired. Please reload the page.'), Http::STATUS_PRECONDITION_FAILED);
121+
return new DataResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED);
122122
}
123123

124124
if ($freshSession) {
@@ -134,7 +134,7 @@ public function create(?int $fileId = null, ?string $filePath = null, =string $b
134134
}
135135
} catch (Exception $e) {
136136
$this->logger->error($e->getMessage(), ['exception' => $e]);
137-
return new DataResponse('Failed to create the document session', 500);
137+
return new DataResponse(['error' => 'Failed to create the document session'], 500);
138138
}
139139

140140
$session = $this->sessionService->initSession($document->getId(), $guestName);
@@ -193,18 +193,18 @@ public function push(Session $session, Document $document, $version, $steps, $aw
193193
$session = $this->sessionService->updateSessionAwareness($session, $awareness);
194194
} catch (DoesNotExistException $e) {
195195
// Session was removed in the meantime. #3875
196-
return new DataResponse($this->l10n->t('Editing session has expired. Please reload the page.'), Http::STATUS_PRECONDITION_FAILED);
196+
return new DataResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED);
197197
}
198198
if (empty($steps)) {
199199
return new DataResponse([]);
200200
}
201201
try {
202202
$result = $this->documentService->addStep($document, $session, $steps, $version, $token);
203203
} catch (InvalidArgumentException $e) {
204-
return new DataResponse($e->getMessage(), 422);
204+
return new DataResponse(['error' => $e->getMessage()], 422);
205205
} catch (DoesNotExistException|NotPermittedException) {
206206
// Either no write access or session was removed in the meantime (#3875).
207-
return new DataResponse($this->l10n->t('Editing session has expired. Please reload the page.'), Http::STATUS_PRECONDITION_FAILED);
207+
return new DataResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED);
208208
}
209209
return new DataResponse($result);
210210
}

src/components/Editor/DocumentStatus.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<div class="document-status">
2525
<NcNoteCard v-if="hasWarning" type="warning">
2626
<p v-if="isLoadingError">
27-
{{ syncError.data.data }}
27+
{{ syncError.data.data.error }}
2828
<!-- Display reload button on PRECONDITION_FAILED response type -->
2929
<a v-if="syncError.data.status === 412" class="button primary" @click="reload">{{ t('text', 'Reload') }}</a>
3030
</p>

0 commit comments

Comments
 (0)