From 99b77cf7d15e6eabb63e9151dd3ff1ea8e8ac631 Mon Sep 17 00:00:00 2001 From: "dorian.hodin" Date: Tue, 28 Feb 2023 09:53:32 +0100 Subject: [PATCH] Add Error Catch in the right section, and make response is not Json Type --- Source/API/script/index.php | 45 +++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/Source/API/script/index.php b/Source/API/script/index.php index b59f9fc..4eacac5 100644 --- a/Source/API/script/index.php +++ b/Source/API/script/index.php @@ -24,30 +24,31 @@ try{ * @param bool $logErrorDetails -> Display error details in error log */ $errorMiddleware = $app->addErrorMiddleware(true, true, true); - - $app->get('/', function (Request $request, Response $response) { - $parameters = $request->getQueryParams(); - $method = $parameters['method']; - $response->getBody()->write($method); - $listGateway = array("\\Gateway\\GatewayForm", "\\Gateway\\GatewayKeyword", "\\Gateway\\GatewayQuestion"); - foreach ($listGateway as $gateway) // Pour chaque Gateway - { - if (method_exists($gateway, $method)) { - (new $gateway)->$method($parameters); // Si oui, on appelle cette fonction + try { + $app->get('/', function (Request $request, Response $response) { + $parameters = $request->getQueryParams(); + $method = $parameters['method']; + $response->getBody()->write($method); + $listGateway = array("\\Gateway\\GatewayForm", "\\Gateway\\GatewayKeyword", "\\Gateway\\GatewayQuestion"); + foreach ($listGateway as $gateway) // Pour chaque Gateway + { + if (method_exists($gateway, $method)) { + (new $gateway)->$method($parameters); // Si oui, on appelle cette fonction + } + } + $response->getBody()->write("Use the method $method, with the parameters "); + foreach ($parameters as $key => $value) { + $response->getBody()->write("Key =>" . $key . ", value =>" . $value); } - } - $response->getBody()->write("Use the method $method, with the parameters "); - foreach ($parameters as $key => $value) { - $response->getBody()->write("Key =>" . $key . ", value =>" . $value); - } - return $response; - }); + return $response->withHeader('Content-type', 'application/json'); + }); + }catch (TypeError $t){ + echo "Error, wrong parameters key, or no parameters write : ".$t->getMessage(); + } -// Run app + // Run app $app->run(); -}catch (HttpNotFoundException $e){ - echo "Error, not Found : ".$e->getMessage(); -}catch (TypeError $t){ - echo "Error, wrong parameters key, or no parameters write : ".$t->getMessage(); +}catch (HttpNotFoundException $e) { + echo "Error, not Found : " . $e->getMessage(); }