Add Exception directory, try to throw first custom exception
continuous-integration/drone/push Build is passing Details

master
dorian.hodin 2 years ago
parent a627f7eff5
commit df099c67a9

@ -0,0 +1,12 @@
<?php
use Slim\Exception\HttpSpecializedException;
class TypeErrorMethod extends HttpSpecializedException {
protected $code = 400;
protected $message = "Bad Parameters";
protected string $title = "400 Bad Parameters";
protected string $description = "The API need a 'method' query params in URL. Exemple :'http://url?method=getSomething'";
}

@ -2,10 +2,9 @@
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Exception\HttpNotFoundException;
use Slim\Factory\AppFactory; use Slim\Factory\AppFactory;
try{
require 'Config/vendor/autoload.php'; require 'Config/vendor/autoload.php';
/** /**
@ -24,11 +23,14 @@ try{
* @param bool $logErrorDetails -> Display error details in error log * @param bool $logErrorDetails -> Display error details in error log
*/ */
$errorMiddleware = $app->addErrorMiddleware(true, true, true); $errorMiddleware = $app->addErrorMiddleware(true, true, true);
try {
/**
* Add a route for the API
*/
$app->get('/', function (Request $request, Response $response) { $app->get('/', function (Request $request, Response $response) {
$parameters = $request->getQueryParams(); $parameters = $request->getQueryParams();
if (empty($parameters['method'])){ if (empty($parameters['method'])){
throw new TypeError("No method specified"); throw new TypeErrorMethod($request);
}else{ }else{
$method = $parameters['method']; $method = $parameters['method'];
} }
@ -36,7 +38,7 @@ try{
$listGateway = array("\\Gateway\\GatewayForm", "\\Gateway\\GatewayKeyword", "\\Gateway\\GatewayQuestion"); $listGateway = array("\\Gateway\\GatewayForm", "\\Gateway\\GatewayKeyword", "\\Gateway\\GatewayQuestion");
foreach ($listGateway as $gateway){ foreach ($listGateway as $gateway){
if (method_exists($gateway, $method)) { if (method_exists($gateway, $method)) {
$response->getBody()->write(print_r((new $gateway)->$method($parameters))); $response->getBody()->write(json_encode((new $gateway)->$method($parameters)));
} }
} }
$temp = array(0 => array("id" => "1", $temp = array(0 => array("id" => "1",
@ -49,14 +51,7 @@ try{
$response->getBody()->write(json_encode($temp)); $response->getBody()->write(json_encode($temp));
return $response->withHeader('Content-type', 'application/json'); 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(); $app->run();
}catch (HttpNotFoundException $e) {
echo "Error, not Found : " . $e->getMessage();
}

Loading…
Cancel
Save