Skip to content

Commit 8ff2f12

Browse files
committed
Improve error handling
1 parent 18ce20f commit 8ff2f12

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "putyourlightson/craft-datastar-module",
33
"description": "A template-driven, reactive hypermedia framework for Craft.",
4-
"version": "1.0.0-RC.7",
4+
"version": "1.0.0-RC.8",
55
"type": "yii-module",
66
"license": "mit",
77
"require": {

src/helpers/Action.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ public static function getUrl(string $route, array $params = []): string
5959

6060
private static function addCsrfToken(array|string $options): string
6161
{
62-
$token = Craft::$app->getRequest()->getCsrfToken();
6362
$csrfHeader = Request::CSRF_HEADER;
63+
$token = Craft::$app->getRequest()->getCsrfToken();
6464

6565
if (is_array($options)) {
66-
return self::addCsrfToArray($options, $token, $csrfHeader);
66+
return self::addCsrfToArray($options, $csrfHeader, $token);
6767
}
6868

69-
return self::addCsrfToString($options, $token, $csrfHeader);
69+
return self::addCsrfToString($options, $csrfHeader, $token);
7070
}
7171

72-
private static function addCsrfToArray(array $options, string $token, string $csrfHeader): string
72+
private static function addCsrfToArray(array $options, string $csrfHeader, string $token): string
7373
{
7474
$headers = $options['headers'] ?? [];
7575
$headers[$csrfHeader] = $token;
@@ -78,7 +78,7 @@ private static function addCsrfToArray(array $options, string $token, string $cs
7878
return Json::encode($options);
7979
}
8080

81-
private static function addCsrfToString(string $options, string $token, string $csrfHeader): string
81+
private static function addCsrfToString(string $options, string $csrfHeader, string $token): string
8282
{
8383
if (preg_match('/headers:\s*\{/i', $options)) {
8484
return preg_replace(

src/services/SseService.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Craft;
99
use craft\base\Component;
1010
use craft\web\Response;
11+
use Exception;
1112
use putyourlightson\datastar\Datastar;
1213
use putyourlightson\datastar\helpers\Request;
1314
use starfederation\datastar\events\EventInterface;
@@ -18,7 +19,6 @@
1819
use starfederation\datastar\events\RemoveElements;
1920
use starfederation\datastar\ServerSentEventGenerator;
2021
use Throwable;
21-
use yii\web\BadRequestHttpException;
2222

2323
class SseService extends Component
2424
{
@@ -210,10 +210,6 @@ public function location(string $uri, array $options = []): static
210210
*/
211211
public function renderTemplate(string $template, array $variables = []): static
212212
{
213-
if (!Craft::$app->getView()->doesTemplateExist($template)) {
214-
$this->throwException('Template `' . $template . '` does not exist.');
215-
}
216-
217213
$signals = $this->readSignals();
218214
$variables = array_merge(
219215
[Datastar::getInstance()->settings->signalsVariableName => $signals],
@@ -282,20 +278,19 @@ public function shouldCloseSession(bool $value): static
282278
}
283279

284280
/**
285-
* Throws an exception with the appropriate formats for easier debugging.
281+
* Throws an exception or logs a console error, for easier debugging.
286282
*
287283
* @phpstan-return never
288284
*/
289-
public function throwException(Throwable|string $exception): void
285+
public function throwException(Throwable $exception): void
290286
{
291-
Craft::$app->getRequest()->getHeaders()->set('Accept', 'text/html');
292-
Craft::$app->getResponse()->format = Response::FORMAT_HTML;
293-
294-
if ($exception instanceof Throwable) {
287+
if (!$this->isStreamedResponse) {
295288
throw $exception;
296289
}
297290

298-
throw new BadRequestHttpException($exception);
291+
$this->executeScript('console.error(' . json_encode($exception->getMessage()) . ');');
292+
flush();
293+
exit();
299294
}
300295

301296
/**
@@ -367,7 +362,7 @@ private function verifySseMethodInProcess(EventInterface $event): void
367362
if ($method === 'patchElements') {
368363
$message .= ' Ensure that you are not setting or removing signals inside `{% patchelements %}` or `{% executescript %}` tags.';
369364
}
370-
$this->throwException($message);
365+
$this->throwException(new Exception($message));
371366
}
372367
}
373368
}

0 commit comments

Comments
 (0)