Skip to content

Commit

Permalink
add exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Smol-An committed Dec 27, 2023
1 parent 65bea3d commit 1177147
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
17 changes: 15 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Carbon\Carbon;
use App\Connection;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ClientException;
use DiDom\Document;

session_start();
Expand Down Expand Up @@ -136,7 +138,19 @@
$urlName = $urlNameStmt->fetch();

$client = new Client();
$status_code = $client->request('GET', $urlName['name'])->getStatusCode();

try {
$res = $client->request('GET', $urlName['name']);
$this->get('flash')->addMessage('success', 'Страница успешно проверена');
$status_code = $res->getStatusCode();
} catch (ConnectException $e) {
$this->get('flash')->addMessage('failure', 'Произошла ошибка при проверке, не удалось подключиться');
return $response->withRedirect($router->urlFor('url', ['id' => $url_id]));
} catch (ClientException $e) {
$res = $e->getResponse();
$this->get('flash')->addMessage('warning', 'Проверка была выполнена успешно, но сервер ответил c ошибкой');
return $response->withRedirect($router->urlFor('url', ['id' => $url_id]));
}

$document = new Document($urlName['name'], true);
$h1 = $document->first('h1') ? $document->first('h1')->text() : '';
Expand Down Expand Up @@ -172,7 +186,6 @@
':checkCreated_at' => $checkCreated_at
]);

$this->get('flash')->addMessage('success', 'Страница успешно проверена');
return $response->withRedirect($router->urlFor('url', ['id' => $url_id]), 302);
});

Expand Down
8 changes: 8 additions & 0 deletions templates/urls/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
<?php foreach ($flash['success'] as $message) : ?>
<div class="alert alert-success" role="alert"><?= htmlspecialchars($message) ?></div>
<?php endforeach ?>
<?php elseif (isset($flash['warning'])) : ?>
<?php foreach ($flash['warning'] as $message) : ?>
<div class="alert alert-warning" role="alert"><?= htmlspecialchars($message) ?></div>
<?php endforeach ?>
<?php elseif (isset($flash['failure'])) : ?>
<?php foreach ($flash['failure'] as $message) : ?>
<div class="alert alert-danger" role="alert"><?= htmlspecialchars($message) ?></div>
<?php endforeach ?>
<?php endif ?>
</div>
<main>
Expand Down

0 comments on commit 1177147

Please sign in to comment.