Skip to content

Commit 02b4db8

Browse files
authored
fix: static generate error handling (#529)
1 parent 672ea02 commit 02b4db8

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

src/Tempest/Http/src/Static/StaticGenerateCommand.php

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use function Tempest\uri;
1919
use Tempest\View\View;
2020
use Tempest\View\ViewRenderer;
21+
use Throwable;
2122

2223
final readonly class StaticGenerateCommand
2324
{
@@ -57,40 +58,48 @@ public function __invoke(): void
5758

5859
$file = path($publicPath, $fileName);
5960

60-
$response = $this->router->dispatch(
61-
new GenericRequest(
62-
method: Method::GET,
63-
uri: $uri,
64-
),
65-
);
61+
try {
62+
$response = $this->router->dispatch(
63+
new GenericRequest(
64+
method: Method::GET,
65+
uri: $uri,
66+
),
67+
);
6668

67-
if ($response->getStatus() !== Status::OK) {
68-
$this->writeln("- <error>{$uri}</error> > {$response->getStatus()->value}");
69+
if ($response->getStatus() !== Status::OK) {
70+
$this->writeln("- <error>{$uri}</error> > {$response->getStatus()->value}");
6971

70-
continue;
71-
}
72+
continue;
73+
}
7274

73-
$body = $response->getBody();
75+
$body = $response->getBody();
7476

75-
$content = $body instanceof View
76-
? $this->viewRenderer->render($body)
77-
: $body;
77+
$content = $body instanceof View
78+
? $this->viewRenderer->render($body)
79+
: $body;
7880

79-
if (! is_string($content)) {
80-
$this->writeln("- <error>{$uri}</error> > No textual body");
81+
if (! is_string($content)) {
82+
$this->writeln("- <error>{$uri}</error> > No textual body");
8183

82-
continue;
83-
}
84+
continue;
85+
}
8486

85-
$directory = pathinfo($file, PATHINFO_DIRNAME);
87+
$directory = pathinfo($file, PATHINFO_DIRNAME);
8688

87-
if (! is_dir($directory)) {
88-
mkdir($directory, recursive: true);
89-
}
89+
if (! is_dir($directory)) {
90+
mkdir($directory, recursive: true);
91+
}
9092

91-
file_put_contents($file, $content);
93+
file_put_contents($file, $content);
9294

93-
$this->writeln("- <em>{$uri}</em> > <u>{$file}</u>");
95+
$this->writeln("- <em>{$uri}</em> > <u>{$file}</u>");
96+
} catch (Throwable $e) {
97+
$this->writeln("- <error>{$uri}</error> {$e->getMessage()}");
98+
99+
ob_get_clean();
100+
101+
continue;
102+
}
94103
}
95104
}
96105

0 commit comments

Comments
 (0)