1010
1111use Closure ;
1212use OCA \Polls \Exceptions \Exception ;
13- use OCA \Polls \Exceptions \NoUpdatesException ;
1413use OCP \AppFramework \Controller ;
15- use OCP \AppFramework \Db \DoesNotExistException ;
1614use OCP \AppFramework \Http ;
1715use OCP \AppFramework \Http \Attribute \NoAdminRequired ;
1816use OCP \AppFramework \Http \JSONResponse ;
@@ -32,77 +30,23 @@ public function __construct(
3230 /**
3331 * response
3432 * @param Closure $callback Callback function
33+ * @param int $successStatus HTTP status code for success
3534 */
3635 #[NoAdminRequired]
37- protected function response (Closure $ callback ): JSONResponse {
38- return $ this ->handleResponse ($ callback , Http::STATUS_OK , Exception::class);
39- }
40-
41- /**
42- * response
43- * @param Closure $callback Callback function
44- */
45- #[NoAdminRequired]
46- protected function responseLong (Closure $ callback ): JSONResponse {
47- return $ this ->handleResponse ($ callback , Http::STATUS_OK , NoUpdatesException::class, Http::STATUS_NOT_MODIFIED );
48- }
49-
50- /**
51- * responseCreate
52- * @param Closure $callback Callback function
53- */
54- #[NoAdminRequired]
55- protected function responseCreate (Closure $ callback ): JSONResponse {
56- return $ this ->handleResponse ($ callback , Http::STATUS_CREATED , Exception::class);
57- }
58-
59- /**
60- * responseDeleteTolerant
61- * @param Closure $callback Callback function
62- */
63- #[NoAdminRequired]
64- protected function responseDeleteTolerant (Closure $ callback ): JSONResponse {
65- return $ this ->handleResponse (
66- $ callback ,
67- Http::STATUS_OK ,
68- DoesNotExistException::class,
69- Http::STATUS_OK ,
70- 'Not found, assume already deleted ' ,
71- Exception::class
72- );
73- }
74-
75- private function handleResponse (
36+ protected function response (
7637 Closure $ callback ,
77- int $ successStatus ,
78- string $ primaryException ,
79- int $ fallbackStatus = null ,
80- string $ fallbackMessage = null ,
81- string $ secondaryException = null
38+ int $ successStatus = Http::STATUS_OK ,
8239 ): JSONResponse {
8340 try {
8441 return new JSONResponse ($ callback (), $ successStatus );
85- } catch (\Exception $ e ) {
86- if (is_a ($ e , $ primaryException , true )) {
87- if ($ fallbackStatus !== null ) {
88- return new JSONResponse (['message ' => $ fallbackMessage ?? '' ], $ fallbackStatus );
89- }
90- if ($ e instanceof Exception) {
91- return new JSONResponse (['message ' => $ e ->getMessage ()], $ e ->getStatus ());
92- }
93- }
42+ } catch (Exception $ e ) {
9443
95- if (
96- $ secondaryException !== null &&
97- is_a ($ e , $ secondaryException , true ) &&
98- $ e instanceof Exception
99- ) {
100- return new JSONResponse (['message ' => $ e ->getMessage ()], $ e ->getStatus ());
44+ if ($ e ->getStatus () === Http::STATUS_NOT_MODIFIED ) {
45+ return new JSONResponse (statusCode: $ e ->getStatus ());
10146 }
10247
103- throw $ e ;
48+ return new JSONResponse ([ ' message ' => $ e -> getMessage ()], $ e -> getStatus ()) ;
10449 }
10550 }
10651
10752}
108-
0 commit comments