Twig icon indicating copy to clipboard operation
Twig copied to clipboard

Add file and line to error message

Open ctippler opened this issue 3 years ago • 3 comments

When an error occures we currently get the message e.g. "An exception has been thrown during the rendering of a template ("Warning: Undefined array key "select"")."

The problem is, that we don't know in which file and where the error occured so it is very hard to find the "cause" of the problem. If we add the File and Line it is quite easy to find the problem:

image

ctippler avatar Dec 30 '22 20:12 ctippler

Can you give a Twig template that exhibits the issue? This PR does not work as you can see in the tests.

fabpot avatar Dec 31 '22 15:12 fabpot

In my case this happens when i call e.g. a twig function and in the twig function i access a undefined array key.

As an example: The view:

{{  "test" | trans }}

{{ app_formatter_test(123)}}

The Twig extension looks like this:

    /**
     * @return \Twig\TwigFunction[]
     */
    public function getFunctions(): array
    {
        return [
            new TwigFunction('app_formatter_test', [$this, 'test'], ['is_safe' => ['html']]),
        ];
    }

    public function test($number) : string
    {

        $x = [];
        $y = $x['asdf']; //this causes the warning/error
        return $number;
    }

image

The twig function is just an example - normaly i have this issue with calls to more complex data models where it is realy hard to find the cause.

Regards, Christian

ctippler avatar Jan 01 '23 21:01 ctippler

The example you gave me emits a warning, which is not cause by Twig. The error message given by PHP gives you the file and line number where it happens, so probably not a good example (at least not one I can use to understand your real-wprld issue).

fabpot avatar Jan 11 '24 21:01 fabpot