]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix(API): Use a distinct exception so apps can react to it and customize the return 41779/head
authorJoas Schilling <coding@schilljs.com>
Mon, 27 Nov 2023 16:59:14 +0000 (17:59 +0100)
committerJoas Schilling <coding@schilljs.com>
Tue, 28 Nov 2023 05:11:57 +0000 (06:11 +0100)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/private/AppFramework/Http/Dispatcher.php
lib/public/AppFramework/Http/ParameterOutOfRangeException.php [new file with mode: 0644]
tests/lib/AppFramework/Http/DispatcherTest.php

index 78a101dcdf66c21c8e7ace0a44de0a9a1d2208ac..c34742a864a08d3666f70526e24abbec6f0e55bb 100644 (file)
@@ -66,6 +66,7 @@ return array(
     'OCP\\AppFramework\\Http\\IOutput' => $baseDir . '/lib/public/AppFramework/Http/IOutput.php',
     'OCP\\AppFramework\\Http\\JSONResponse' => $baseDir . '/lib/public/AppFramework/Http/JSONResponse.php',
     'OCP\\AppFramework\\Http\\NotFoundResponse' => $baseDir . '/lib/public/AppFramework/Http/NotFoundResponse.php',
+    'OCP\\AppFramework\\Http\\ParameterOutOfRangeException' => $baseDir . '/lib/public/AppFramework/Http/ParameterOutOfRangeException.php',
     'OCP\\AppFramework\\Http\\RedirectResponse' => $baseDir . '/lib/public/AppFramework/Http/RedirectResponse.php',
     'OCP\\AppFramework\\Http\\RedirectToDefaultAppResponse' => $baseDir . '/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php',
     'OCP\\AppFramework\\Http\\Response' => $baseDir . '/lib/public/AppFramework/Http/Response.php',
index 2026dfec112ad00ed33552c2af9a84ebeae78cb7..743c5f752ce63e097771db69a090d2cc9d34f174 100644 (file)
@@ -99,6 +99,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
         'OCP\\AppFramework\\Http\\IOutput' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/IOutput.php',
         'OCP\\AppFramework\\Http\\JSONResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/JSONResponse.php',
         'OCP\\AppFramework\\Http\\NotFoundResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/NotFoundResponse.php',
+        'OCP\\AppFramework\\Http\\ParameterOutOfRangeException' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/ParameterOutOfRangeException.php',
         'OCP\\AppFramework\\Http\\RedirectResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/RedirectResponse.php',
         'OCP\\AppFramework\\Http\\RedirectToDefaultAppResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php',
         'OCP\\AppFramework\\Http\\Response' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/Response.php',
index 5649060ba7621c6d4d96ed5167027a351ed05d7b..6e946f2bfa358950e9ed1b725181b53168954a9b 100644 (file)
@@ -38,11 +38,11 @@ use OC\AppFramework\Utility\ControllerMethodReflector;
 use OC\DB\ConnectionAdapter;
 use OCP\AppFramework\Controller;
 use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\Http\ParameterOutOfRangeException;
 use OCP\AppFramework\Http\Response;
 use OCP\Diagnostics\IEventLogger;
 use OCP\IConfig;
 use OCP\IRequest;
-use OutOfRangeException;
 use Psr\Container\ContainerInterface;
 use Psr\Log\LoggerInterface;
 
@@ -255,18 +255,18 @@ class Dispatcher {
 
        /**
         * @psalm-param mixed $value
-        * @throws OutOfRangeException
+        * @throws ParameterOutOfRangeException
         */
        private function ensureParameterValueSatisfiesRange(string $param, $value): void {
                $rangeInfo = $this->reflector->getRange($param);
                if ($rangeInfo) {
                        if ($value < $rangeInfo['min'] || $value > $rangeInfo['max']) {
-                               throw new OutOfRangeException(sprintf(
-                                       'Parameter %s must be between %d and %d',
+                               throw new ParameterOutOfRangeException(
                                        $param,
+                                       $value,
                                        $rangeInfo['min'],
                                        $rangeInfo['max'],
-                               ));
+                               );
                        }
                }
        }
diff --git a/lib/public/AppFramework/Http/ParameterOutOfRangeException.php b/lib/public/AppFramework/Http/ParameterOutOfRangeException.php
new file mode 100644 (file)
index 0000000..22518d8
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\AppFramework\Http;
+
+/**
+ * @since 29.0.0
+ */
+class ParameterOutOfRangeException extends \OutOfRangeException {
+       /**
+        * @since 29.0.0
+        */
+       public function __construct(
+               protected string $parameterName,
+               protected int $actualValue,
+               protected int $minValue,
+               protected int $maxValue,
+       ) {
+               parent::__construct(
+                       sprintf(
+                               'Parameter %s must be between %d and %d',
+                               $this->parameterName,
+                               $this->minValue,
+                               $this->maxValue,
+                       )
+               );
+       }
+
+       /**
+        * @since 29.0.0
+        */
+       public function getParameterName(): string {
+               return $this->parameterName;
+       }
+
+       /**
+        * @since 29.0.0
+        */
+       public function getActualValue(): int {
+               return $this->actualValue;
+       }
+
+       /**
+        * @since 29.0.0
+        */
+       public function getMinValue(): int {
+               return $this->minValue;
+       }
+
+       /**
+        * @since 29.0.0
+        */
+       public function getMaxValue(): int {
+               return $this->maxValue;
+       }
+}
index d9d9eb82a6846b0052070e599d3456d83c4bd066..aa74fe5c6eac80532d1685d778e2d5d853c4b2af 100644 (file)
@@ -31,6 +31,7 @@ use OCP\AppFramework\Controller;
 use OCP\AppFramework\Http;
 use OCP\AppFramework\Http\DataResponse;
 use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\Http\ParameterOutOfRangeException;
 use OCP\AppFramework\Http\Response;
 use OCP\Diagnostics\IEventLogger;
 use OCP\IConfig;
@@ -560,7 +561,7 @@ class DispatcherTest extends \Test\TestCase {
                );
 
                if ($throw) {
-                       $this->expectException(\OutOfRangeException::class);
+                       $this->expectException(ParameterOutOfRangeException::class);
                }
 
                $this->invokePrivate($this->dispatcher, 'ensureParameterValueSatisfiesRange', ['myArgument', $input]);