From a3db2bf7092f08ec1780ea5a27087c0f7c3c1ec3 Mon Sep 17 00:00:00 2001 From: Peter Dulacka Date: Mon, 10 Aug 2020 20:41:22 +0000 Subject: [PATCH] Add validation error handling in REMP Mailer API client remp2020/crm-remp-mailer-module#2 Signed-off-by: Peter Dulacka --- src/Models/Api/Client.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Models/Api/Client.php b/src/Models/Api/Client.php index abe0814..360bbcf 100644 --- a/src/Models/Api/Client.php +++ b/src/Models/Api/Client.php @@ -8,6 +8,7 @@ use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\ServerException; use GuzzleHttp\RequestOptions; +use Nette\Http\IResponse; use Nette\Utils\Json; use Tracy\Debugger; use Tracy\ILogger; @@ -169,6 +170,12 @@ public function getMailLogs(?string $email, ?string $filter, ?int $limit, ?int $ $records[$i]->sent_at = new \DateTime($row->sent_at); } return $records; + } catch (ClientException $e) { + if ($e->getResponse()->getStatusCode() == IResponse::S400_BAD_REQUEST) { + Debugger::log($e, ILogger::ERROR); + return null; + } + throw $e; } catch (ServerException | ConnectException $e) { Debugger::log($e, ILogger::ERROR); return null; @@ -195,6 +202,12 @@ public function countMailLogs(string $email, array $statuses, ?\DateTime $from, ]); return Json::decode($logsCount->getBody()->getContents(), true); + } catch (ClientException $e) { + if ($e->getResponse()->getStatusCode() == IResponse::S400_BAD_REQUEST) { + Debugger::log($e, ILogger::ERROR); + return null; + } + throw $e; } catch (ServerException | ConnectException $e) { Debugger::log($e, ILogger::ERROR); return null; @@ -337,9 +350,13 @@ public function getUserPreferences($userId, $email, ?bool $subscribed = null) return Json::decode($result->getBody(), Json::FORCE_ARRAY); } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() == 404) { + if ($e->getResponse()->getStatusCode() == IResponse::S404_NOT_FOUND) { return []; } + if ($e->getResponse()->getStatusCode() == IResponse::S400_BAD_REQUEST) { + Debugger::log($e, ILogger::ERROR); + return null; + } throw $e; } catch (ServerException | ConnectException $e) { Debugger::log($e, ILogger::ERROR);