Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Commit 84db344

Browse files
committed
Fix error when no data
1 parent a71fcad commit 84db344

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Controller/DefaultController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Controller;
66

77
use App\Repository\LastUpdateRepository;
8+
use RuntimeException;
89
use Symfony\Component\HttpFoundation\Response;
910

1011
final class DefaultController
@@ -16,7 +17,11 @@ public function __construct(
1617

1718
public function index(): Response
1819
{
19-
$lastUpdate = $this->repository->get()->format('Y-m-d H:i:s');
20+
try {
21+
$lastUpdate = $this->repository->get()->format('Y-m-d H:i:s');
22+
} catch (RuntimeException $e) {
23+
$lastUpdate = $e->getMessage();
24+
}
2025

2126
return new Response(<<<HTML
2227
<html>

src/Repository/LastUpdateRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DateTimeImmutable;
99
use PDO;
1010
use PDOException;
11+
use RuntimeException;
1112

1213
final class LastUpdateRepository
1314
{
@@ -20,8 +21,11 @@ public function __construct(
2021
public function get(): DateTimeImmutable
2122
{
2223
$stmt = $this->db->query('SELECT * FROM last_update;');
24+
if (false === ($date = $stmt->fetchColumn())) {
25+
throw new RuntimeException('Data has never been updated.');
26+
}
2327

24-
return new DateTimeImmutable($stmt->fetchColumn());
28+
return new DateTimeImmutable($date);
2529
}
2630

2731
public function save(): void

0 commit comments

Comments
 (0)