diff --git a/Source/API/script/Config/config.php b/Source/API/script/Config/config.php index ac9f8f9..6c17b53 100644 --- a/Source/API/script/Config/config.php +++ b/Source/API/script/Config/config.php @@ -2,7 +2,6 @@ namespace Config; - use PDOException; require_once __DIR__ ."/Connection.php"; @@ -21,9 +20,7 @@ class ConnectClass{ $connection = new Connection($dsn,$login,$password); }catch (PDOException){ - - http_response_code(404); - return http_response_code(); + throw new PDOException(); } return $connection; } diff --git a/Source/API/script/ExceptionHandle/HttpNotFoundError.php b/Source/API/script/ExceptionHandle/HttpNotFoundError.php index 472c2ee..138a82c 100644 --- a/Source/API/script/ExceptionHandle/HttpNotFoundError.php +++ b/Source/API/script/ExceptionHandle/HttpNotFoundError.php @@ -9,6 +9,7 @@ use Throwable; class HttpNotFoundError extends HttpSpecializedException { protected $code = 404; + protected string $title = "Method Not Found"; 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/ExceptionHandle/PDOError.php b/Source/API/script/ExceptionHandle/PDOError.php new file mode 100644 index 0000000..2db529f --- /dev/null +++ b/Source/API/script/ExceptionHandle/PDOError.php @@ -0,0 +1,20 @@ +message, $previous); + } + +} \ No newline at end of file diff --git a/Source/API/script/ExceptionHandle/TypeErrorMethod.php b/Source/API/script/ExceptionHandle/TypeErrorMethod.php index 11cf046..8e62105 100644 --- a/Source/API/script/ExceptionHandle/TypeErrorMethod.php +++ b/Source/API/script/ExceptionHandle/TypeErrorMethod.php @@ -9,6 +9,7 @@ use Throwable; class TypeErrorMethod extends HttpSpecializedException { protected $code = 400; + protected string $title = "Method query params is not specified"; protected $message = "Bad Parameters, The API need a 'method' query params in URL. Exemple :'http://url?method=getSomething'"; public function __construct(ServerRequestInterface $request,?Throwable $previous = null) diff --git a/Source/API/script/Gateway/GatewayForm.php b/Source/API/script/Gateway/GatewayForm.php index f8f01e1..03d7b12 100644 --- a/Source/API/script/Gateway/GatewayForm.php +++ b/Source/API/script/Gateway/GatewayForm.php @@ -2,17 +2,20 @@ namespace Gateway; -use Config\Connection; use PDO; -use Config\ConnectClass; +use PDOException; class GatewayForm { - private Connection $connection; + private \Config\Connection $connection; public function __construct() { - $this->connection = (new ConnectClass)->connect(); + try{ + $this->connection = (new \Config\ConnectClass)->connect(); + }catch(PDOException $e){ + throw new PDOException(); + } } public function insertForm(array $parameters): void diff --git a/Source/API/script/Gateway/GatewayKeyword.php b/Source/API/script/Gateway/GatewayKeyword.php index 4d751d3..e98b567 100644 --- a/Source/API/script/Gateway/GatewayKeyword.php +++ b/Source/API/script/Gateway/GatewayKeyword.php @@ -5,6 +5,7 @@ namespace Gateway; use Config\Connection; use Config\ConnectClass; use PDO; +use PDOException; class GatewayKeyword { @@ -12,8 +13,11 @@ class GatewayKeyword public function __construct() { - $this->connection = (new ConnectClass())->connect(); - } + try{ + $this->connection = (new ConnectClass)->connect(); + }catch(PDOException $e){ + throw new PDOException(); + } } public function insertKeyword(array $keyword): void { diff --git a/Source/API/script/Gateway/GatewayListResponseOfCandidate.php b/Source/API/script/Gateway/GatewayListResponseOfCandidate.php index fd19794..f8625f1 100644 --- a/Source/API/script/Gateway/GatewayListResponseOfCandidate.php +++ b/Source/API/script/Gateway/GatewayListResponseOfCandidate.php @@ -5,6 +5,7 @@ namespace Gateway; use Config\Connection; use Config\ConnectClass; use PDO; +use PDOException; class GatewayListResponseOfCandidate { @@ -12,8 +13,11 @@ class GatewayListResponseOfCandidate public function __construct() { - $this->connection = (new ConnectClass())->connect(); - } + try{ + $this->connection = (new ConnectClass)->connect(); + }catch(PDOException $e){ + throw new PDOException(); + } } public function getDetailsListResponsesOfCandidate(array $idListResponse): array { diff --git a/Source/API/script/Gateway/GatewayPossibleResponse.php b/Source/API/script/Gateway/GatewayPossibleResponse.php index 37146da..e3c901c 100644 --- a/Source/API/script/Gateway/GatewayPossibleResponse.php +++ b/Source/API/script/Gateway/GatewayPossibleResponse.php @@ -5,6 +5,7 @@ namespace Gateway; use Config\Connection; use Config\ConnectClass; use PDO; +use PDOException; class GatewayPossibleResponse { @@ -12,8 +13,11 @@ class GatewayPossibleResponse public function __construct() { - $this->connection = (new ConnectClass())->connect(); - } + try{ + $this->connection = (new ConnectClass)->connect(); + }catch(PDOException $e){ + throw new PDOException(); + } } public function getPossibleResponseByQuestion(array $idQuestion): array { diff --git a/Source/API/script/Gateway/GatewayQuestion.php b/Source/API/script/Gateway/GatewayQuestion.php index 3891933..67b84b2 100644 --- a/Source/API/script/Gateway/GatewayQuestion.php +++ b/Source/API/script/Gateway/GatewayQuestion.php @@ -5,6 +5,7 @@ namespace Gateway; use Config\Connection; use Config\ConnectClass; use PDO; +use PDOException; class GatewayQuestion { @@ -12,8 +13,11 @@ class GatewayQuestion public function __construct() { - $this->connection = (new ConnectClass())->connect(); - } + try{ + $this->connection = (new ConnectClass)->connect(); + }catch(PDOException $e){ + throw new PDOException(); + } } public function insertQuestion(QuestionAPI $question, int $idForm): void { diff --git a/Source/API/script/Gateway/GatewayResponse.php b/Source/API/script/Gateway/GatewayResponse.php index 6c5b290..3aad165 100644 --- a/Source/API/script/Gateway/GatewayResponse.php +++ b/Source/API/script/Gateway/GatewayResponse.php @@ -5,6 +5,7 @@ namespace Gateway; use Config\Connection; use Config\ConnectClass; use PDO; +use PDOException; class GatewayResponse { @@ -12,8 +13,11 @@ class GatewayResponse public function __construct() { - $this->connection = (new ConnectClass())->connect(); - } + try{ + $this->connection = (new ConnectClass)->connect(); + }catch(PDOException $e){ + throw new PDOException(); + } } public function getResponsesByIdListCandidate(array $listResponsesOfCandidateId): array { diff --git a/Source/API/script/index.php b/Source/API/script/index.php index 651d27f..33a070f 100644 --- a/Source/API/script/index.php +++ b/Source/API/script/index.php @@ -1,12 +1,12 @@ get('/', function (Request $request, Response $response) { $listGateway = array("\\Gateway\\GatewayForm", "\\Gateway\\GatewayKeyword", "\\Gateway\\GatewayQuestion"); $ok = false; foreach ($listGateway as $gateway){ - echo implode(",",get_class_methods((new $gateway))); - if (method_exists($gateway, $method)) { - $ok = true; - $response->getBody()->write(json_encode((new $gateway)->$method($parameters))); + try { + echo implode(",", get_class_methods((new $gateway))); + if (method_exists($gateway, $method)) { + $ok = true; + $response->getBody()->write(json_encode((new $gateway)->$method($parameters))); + } + }catch (PDOException $e){ + throw new PDOError($request); } } if (!$ok){