Skip to content

Commit 64bfd6e

Browse files
Merge pull request #11918 from nextcloud/fix/dev/openapi-exceptions
2 parents f12f8ba + 120591e commit 64bfd6e

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

developer_manual/client_apis/OCS/ocs-openapi.rst

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,33 +185,33 @@ If you are working with an existing API where you can not break compatibility, y
185185
return DataResponse([]);
186186
}
187187
188-
DO NOT throw exceptions
189-
^^^^^^^^^^^^^^^^^^^^^^^
188+
DO NOT throw non-OCS*Exceptions
189+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
190190

191-
Return valid responses with an error message instead.
191+
Only use OCS*Exceptions as any other Exceptions do not produce JSON responses.
192192

193193
.. collapse:: Examples
194194

195195
.. code-block:: php
196196
:caption: Bad
197197
198198
/**
199-
* @throws OCSBadRequestException
199+
* @throws BadRequestException
200200
*/
201201
public function someControllerMethod() {
202202
...
203-
throw new OCSBadRequestException("some message");
203+
throw new BadRequestException([]);
204204
}
205205
206206
.. code-block:: php
207207
:caption: Good
208208
209209
/**
210-
* @return DataResponse<Http::STATUS_BAD_REQUEST, array{message: string}, array{}>
210+
* @throws OCSBadRequestException
211211
*/
212212
public function someControllerMethod() {
213213
...
214-
return DataResponse(["message" => "some message"], Http::STATUS_BAD_REQUEST);
214+
throw new OCSBadRequestException("some message");
215215
}
216216
217217
DO use the same data structures for the same group of responses
@@ -643,7 +643,6 @@ How to handle exceptions
643643
------------------------
644644

645645
Sometimes you want to end with an exception instead of returning a response.
646-
It is better to not do it, but when migrating existing APIs you might have to deal with exceptions.
647646
For this example our ``update`` will throw an exception when the ETag does not match:
648647

649648
.. code-block:: php
@@ -673,8 +672,7 @@ Adding the correct annotation works like this:
673672
}
674673
675674
The description after the exception class name works exactly like the description for the status codes we added earlier.
676-
Note that the resulting response will be in plain text and no longer in JSON.
677-
Therefore it is not recommended to use exceptions to return errors.
675+
Note that you should only used OCS*Exceptions, as any other Exception will result in a plain text body instead of JSON.
678676

679677
How to ignore certain endpoints
680678
-------------------------------

0 commit comments

Comments
 (0)