sculpin / sculpin

Sculpin — Static Site Generator
https://sculpin.io
MIT License
1.46k stars 194 forks source link

TWIG errors not shown / empty content / mcaros #384

Closed tricoos closed 5 years ago

tricoos commented 6 years ago

It appears that if you have an error in an included TWIG template no error is shown whatsoever. Instead the whole page becomes a "white page" in the browser with not a single byte of content. If this happens in the default/main template then all of the pages are completely empty.

This happens (at least) if you include a twig template in the default template for example: {% import "fileWithErrors.html.twig" as macros %}

Let's assume the file fileWithErrors.html.twig looks like this:

{%macro test(val) %}
    {% if val %}
        Hello!
{% endmacro %}

If you now reload the page after it's re-built all of the pages become empty. No error is shown in the console (using the server script).

It can take a lot of time to find the source of the error if you've created a lot of macros and then reload the page.

beryllium commented 6 years ago

OK, it looks like some of the exception handlers in `src/Sculpin/Bundle/TwigBundle/TwigFormatter.php' don't pass the exceptions up into other areas of the application.

public function formatPage(FormatContext $formatContext)
    {
        try {
            $this->arrayLoader->setTemplate(
                $formatContext->templateId(),
                $this->massageTemplate($formatContext)
            );

            $data = $formatContext->data()->export();

            return $this->twig->render($formatContext->templateId(), $data);
        } catch (\Exception $e) {
            print ' [ ' . get_class($e) . ': ' . $e->getMessage() . " ]\n";
        }
    }

You can see there that it just catches any exceptions (including Twig syntax error exceptions) and prints out an error to the console.

It would definitely help for there to be better error management in cases like this. Exiting with a non-zero status code seems like a possible solution, in both server and non-server generate modes. I'll hopefully have some time to add this soon (unless someone else gets to it first).

beryllium commented 5 years ago

This has been addressed in the latest dev-master (and soon, the next RC of Sculpin 3) - now a red error message will show up and indicate the offending file. Thanks for the bug report!