|
8 | 8 | use Craft; |
9 | 9 | use craft\base\Component; |
10 | 10 | use craft\web\Response; |
| 11 | +use Exception; |
11 | 12 | use putyourlightson\datastar\Datastar; |
12 | 13 | use putyourlightson\datastar\helpers\Request; |
13 | 14 | use starfederation\datastar\events\EventInterface; |
|
18 | 19 | use starfederation\datastar\events\RemoveElements; |
19 | 20 | use starfederation\datastar\ServerSentEventGenerator; |
20 | 21 | use Throwable; |
21 | | -use yii\web\BadRequestHttpException; |
22 | 22 |
|
23 | 23 | class SseService extends Component |
24 | 24 | { |
@@ -210,10 +210,6 @@ public function location(string $uri, array $options = []): static |
210 | 210 | */ |
211 | 211 | public function renderTemplate(string $template, array $variables = []): static |
212 | 212 | { |
213 | | - if (!Craft::$app->getView()->doesTemplateExist($template)) { |
214 | | - $this->throwException('Template `' . $template . '` does not exist.'); |
215 | | - } |
216 | | - |
217 | 213 | $signals = $this->readSignals(); |
218 | 214 | $variables = array_merge( |
219 | 215 | [Datastar::getInstance()->settings->signalsVariableName => $signals], |
@@ -282,20 +278,19 @@ public function shouldCloseSession(bool $value): static |
282 | 278 | } |
283 | 279 |
|
284 | 280 | /** |
285 | | - * Throws an exception with the appropriate formats for easier debugging. |
| 281 | + * Throws an exception or logs a console error, for easier debugging. |
286 | 282 | * |
287 | 283 | * @phpstan-return never |
288 | 284 | */ |
289 | | - public function throwException(Throwable|string $exception): void |
| 285 | + public function throwException(Throwable $exception): void |
290 | 286 | { |
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) { |
295 | 288 | throw $exception; |
296 | 289 | } |
297 | 290 |
|
298 | | - throw new BadRequestHttpException($exception); |
| 291 | + $this->executeScript('console.error(' . json_encode($exception->getMessage()) . ');'); |
| 292 | + flush(); |
| 293 | + exit(); |
299 | 294 | } |
300 | 295 |
|
301 | 296 | /** |
@@ -367,7 +362,7 @@ private function verifySseMethodInProcess(EventInterface $event): void |
367 | 362 | if ($method === 'patchElements') { |
368 | 363 | $message .= ' Ensure that you are not setting or removing signals inside `{% patchelements %}` or `{% executescript %}` tags.'; |
369 | 364 | } |
370 | | - $this->throwException($message); |
| 365 | + $this->throwException(new Exception($message)); |
371 | 366 | } |
372 | 367 | } |
373 | 368 | } |
0 commit comments