Skip to content

Commit

Permalink
Add validation error handling in REMP Mailer API client
Browse files Browse the repository at this point in the history
#2

Signed-off-by: Peter Dulacka <dulacka@gmail.com>
  • Loading branch information
rootpd committed Aug 10, 2020
1 parent bb663fa commit a3db2bf
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Models/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a3db2bf

Please sign in to comment.