From 3f918b29498550c9eea239baabc88cb85eac9756 Mon Sep 17 00:00:00 2001 From: "dorian.hodin" Date: Thu, 2 Mar 2023 11:28:51 +0100 Subject: [PATCH] Use the new custom error when method not found --- Source/API/script/ExceptionHandle/HttpNotFoundError.php | 2 +- Source/API/script/index.php | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Source/API/script/ExceptionHandle/HttpNotFoundError.php b/Source/API/script/ExceptionHandle/HttpNotFoundError.php index 41cde83..472c2ee 100644 --- a/Source/API/script/ExceptionHandle/HttpNotFoundError.php +++ b/Source/API/script/ExceptionHandle/HttpNotFoundError.php @@ -9,7 +9,7 @@ use Throwable; class HttpNotFoundError extends HttpSpecializedException { protected $code = 404; - protected $message = "Wrong URL, verify the right call of the API. Exemple :'http://url?method=getSomething&id=5'"; + protected $message = "Method not found on the API, verify the method's name."; public function __construct(ServerRequestInterface $request,?Throwable $previous = null) { diff --git a/Source/API/script/index.php b/Source/API/script/index.php index 7286c8c..0ec8d0c 100644 --- a/Source/API/script/index.php +++ b/Source/API/script/index.php @@ -1,5 +1,6 @@ addRoutingMiddleware(); * @param bool $logErrors -> Parameter is passed to the default ErrorHandler * @param bool $logErrorDetails -> Display error details in error log */ -$errorMiddleware = $app->addErrorMiddleware(true, true, true); +$errorMiddleware = $app->addErrorMiddleware(true, true, false); /** * Add a route for the API @@ -37,11 +38,16 @@ $app->get('/', function (Request $request, Response $response) { } unset($parameters['method']); $listGateway = array("\\Gateway\\GatewayForm", "\\Gateway\\GatewayKeyword", "\\Gateway\\GatewayQuestion"); + $ok = false; foreach ($listGateway as $gateway){ if (method_exists($gateway, $method)) { + $ok = true; $response->getBody()->write(json_encode((new $gateway)->$method($parameters))); } } + if (!$ok){ + throw new HttpNotFoundError($request); + } $temp = array(0 => array("id" => "1", "0" => "1", "title" => "Votre avis nous intéresse !",