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,33 +2,35 @@
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';
/**
* Instantiate App * Instantiate App
*/ */
$app = AppFactory::create(); $app = AppFactory::create();
// Add Routing Middleware // Add Routing Middleware
$app->addRoutingMiddleware(); $app->addRoutingMiddleware();
/** /**
* Add Error Handling Middleware * Add Error Handling Middleware
* *
* @param bool $displayErrorDetails -> Should be set to false in production * @param bool $displayErrorDetails -> Should be set to false in production
* @param bool $logErrors -> Parameter is passed to the default ErrorHandler * @param bool $logErrors -> Parameter is passed to the default ErrorHandler
* @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 {
$app->get('/', function (Request $request, Response $response) { /**
* Add a route for the API
*/
$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",
@ -48,15 +50,8 @@ 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
$app->run();
}catch (HttpNotFoundException $e) {
echo "Error, not Found : " . $e->getMessage();
}
// Run app
$app->run();
Loading…
Cancel
Save