Skip to content

[VarDumper] Dump PHP+Twig code excerpts in backtraces #15838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2015

Conversation

nicolas-grekas
Copy link
Member

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

ExceptionCaster::filterTrace() is deprecated and replaced by a more flexible backtrace processing that allows one to register casters for amending/changing dumped backtraces. This is especially useful for dumping source map information/excerpts (like e.g. twig template source).

Here is a comparison generated with this code snippet (see also the expected output in testThrowingCaster):

namespace Symfony\Component\VarDumper\Caster;

require 'vendor/autoload.php';

function bar()
{
    return foo();
}

function foo()
{
    dump(new \Exception('baz'));
}

bar('aaaaarg');

Before:
before

After:
after

@nicolas-grekas nicolas-grekas force-pushed the dump-excerpt branch 2 times, most recently from c1104ca to 730262d Compare September 19, 2015 14:23
@nicolas-grekas nicolas-grekas mentioned this pull request Sep 19, 2015
4 tasks
@nicolas-grekas nicolas-grekas force-pushed the dump-excerpt branch 3 times, most recently from 4f3a2ac to 2b4379b Compare September 19, 2015 14:55
@nicolas-grekas nicolas-grekas added the DX DX = Developer eXperience (anything that improves the experience of using Symfony) label Sep 19, 2015
@nicolas-grekas nicolas-grekas force-pushed the dump-excerpt branch 2 times, most recently from 36c164e to 4b9b8d8 Compare September 20, 2015 11:51
@nicolas-grekas
Copy link
Member Author

Tips: to get the same kind of output using debug_backtrace(), use:

dump(['here' => new TraceStub(debug_backtrace())]);

@nicolas-grekas
Copy link
Member Author

Rebased

@fabpot
Copy link
Member

fabpot commented Sep 25, 2015

👍

}
extract($frame->value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't you prevent overwriting of variables here to avoid issues ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need, overwritting $a could even be useful to some

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this could also overwrite $frame, which would break the following code

@nicolas-grekas
Copy link
Member Author

Updated

@fabpot
Copy link
Member

fabpot commented Sep 30, 2015

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 89578f1 into symfony:2.8 Sep 30, 2015
fabpot added a commit that referenced this pull request Sep 30, 2015
…(nicolas-grekas)

This PR was merged into the 2.8 branch.

Discussion
----------

[VarDumper] Dump PHP+Twig code excerpts in backtraces

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

ExceptionCaster::filterTrace() is deprecated and replaced by a more flexible backtrace processing that allows one to register casters for amending/changing dumped backtraces. This is especially useful for dumping source map information/excerpts (like e.g. twig template source).

Here is a comparison generated with this code snippet (see also the expected output in  testThrowingCaster):
```php

namespace Symfony\Component\VarDumper\Caster;

require 'vendor/autoload.php';

function bar()
{
    return foo();
}

function foo()
{
    dump(new \Exception('baz'));
}

bar('aaaaarg');
```

Before:
![before](https://cloud.githubusercontent.com/assets/243674/9976794/88f0259a-5eef-11e5-81a8-3cb9b44cfb00.png)

After:
![after](https://cloud.githubusercontent.com/assets/243674/9976747/6bbac068-5eed-11e5-99dc-a4fd5d3172b5.png)

Commits
-------

89578f1 [VarDumper] Dump PHP+Twig code excerpts in backtraces
@nicolas-grekas nicolas-grekas deleted the dump-excerpt branch September 30, 2015 16:48
@fabpot fabpot mentioned this pull request Nov 16, 2015
Copy link
Contributor

@Aliance Aliance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filterTrace method is deprecated since version 2.8 and will be removed in 3.0. Use the castTraceStub method instead.

Can not understand how should I change them. I have this code:

$trace = $exception->getTrace();
ExceptionCaster::filterTrace($trace, false);

This code transforms $trace variable from

[
    [
        'function' => '%methodName%',
        'class' => '%FQCN%',
        'type' => '->',
        'args' => [/* ... */],
    ],
    // ...
]

to this format:

[
    [
        'call' => '%FQCN%->%methodName%',
        'file' => '',
    ],
    // ...
]

How can I do the same by using ExceptionCaster::castTraceStub? I'm tried to use It like this:

        $trace = $exception->getTrace();
        $trace = ExceptionCaster::castTraceStub(
            new TraceStub($trace),
            $trace,
            new Stub(),
            true
        );

return $a;
}
$stub->class = '';
$stub->handle = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need a $stub here? It does not pass by reference, just set 2 properties and that's all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aliance this is modifying an object. So it has an effect

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, well then I'm not understanding smth. Can you answer my question above?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mb @nicolas-grekas can help me?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please open an issue, comments on commits are hard to track.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Ready Status: Needs Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants