Skip to content

[ErrorHandler] Add button to copy the path where error is thrown #40052

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
Jul 26, 2021

Conversation

lmillucci
Copy link
Contributor

@lmillucci lmillucci commented Feb 1, 2021

Q A
Branch? 5.x for features
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR

As a developer I want to copy the path where an exception is thrown on my IDE to check what is the problem. At the moment I have to select the path on the error page and copy it. I think that this process could be simplified through a "copy path" button that copies the error path on the clipboard.

I was thinking about something like this:
Screenshot_2021-02-01 MyException (500 Internal Server Error)

(Page style should be improved)
Let me know what you think. If you think it is a good idea I could improve this PR

@carsonbot
Copy link

Hey!

You, my friend, deserve a BIG HUG for making this PR.

To help keep things organized, we don't allow "Draft" pull requests. Could you please click the "ready for review" button or close this PR and open a new one when you are done?

Note that a pull request does not have to be "perfect" or "ready for merge" when you first open it. We just want it to be ready for a first review.

Cheers!

Carsonbot

@lmillucci lmillucci force-pushed the errorhandler-copy-error-path branch from 1da52a0 to 057e2f5 Compare February 1, 2021 08:11
@carsonbot carsonbot closed this Feb 1, 2021
@nicolas-grekas
Copy link
Member

Dunno why @carsonbot closed this one.

@nicolas-grekas nicolas-grekas reopened this Feb 1, 2021
@lmillucci
Copy link
Contributor Author

lmillucci commented Feb 1, 2021

I think it was closed because I didn't read well the first comment
To help keep things organized, we don't allow "Draft" pull requests. Could you please click the "ready for review" button or close this PR and open a new one when you are done?

I'm moving this to "ready for review"

@@ -25,6 +25,7 @@
<span class="trace-method"><?= $trace['function']; ?></span>
<?php } ?>
(line <?= $lineNumber; ?>)
<button class="hidden" data-clipboard-text="<?php echo implode(\DIRECTORY_SEPARATOR, $filePathParts); ?>">Copy path</button>
Copy link
Contributor

Choose a reason for hiding this comment

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

should we suffix :$lineNumber? it works well in most editors AFAIK.

@noniagriconomie
Copy link
Contributor

hello, isnt this "similar" to https://symfony.com/doc/current/reference/configuration/framework.html#ide?

@yceruto
Copy link
Member

yceruto commented Feb 1, 2021

hello, isn't this "similar" to https://symfony.com/doc/current/reference/configuration/framework.html#ide?

Yes, but not all IDEs support that feature, I think it would be worth it.

👍 on my side.

@lmillucci lmillucci force-pushed the errorhandler-copy-error-path branch from 4620b46 to 18d25cd Compare February 3, 2021 13:00
@nicolas-grekas nicolas-grekas added this to the 5.x milestone Feb 4, 2021
@nicolas-grekas
Copy link
Member

Oh does the icon work in dark mode? Can you please update your screenshot?

@lmillucci
Copy link
Contributor Author

These are the updated screenshots:
Screenshot from 2021-02-04 11-56-46
Screenshot_2021-02-04 MyException (500 Internal Server Error)(1)

PS: What about coding standard issues found by fabbot? Should I fix them?

nicolas-grekas
nicolas-grekas previously approved these changes Feb 4, 2021
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

(failures are false positives)

@@ -698,6 +707,14 @@
});
}

/* Prevents from disallowing clicks on "copy to clipboard" elements inside toggles */
var copyToClipboardElements = toggles[i].querySelectorAll('span[data-clipboard-text]')
Copy link
Member

Choose a reason for hiding this comment

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

Why using for (...) here and .forEach at line 40.
Same for addEventListener(element vs element.addEventListener)

Copy link
Member

Choose a reason for hiding this comment

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

It would be even better to use event delegation instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that this code is actually useless because the click listener for toggle it is not attached to span elements. What do you think?

var copyToClipboardElements = toggles[i].querySelectorAll('span[data-clipboard-text]')
for (var k = 0; k < copyToClipboardElements.length; k++) {
addEventListener(copyToClipboardElements[k], 'click', function(e) {
e.stopPropagation();
Copy link
Member

Choose a reason for hiding this comment

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

IMHO we could always stopPropagation (and not ONLY for items in SPAN/toggle). And moving this logic at line 44

@Nyholm
Copy link
Member

Nyholm commented Feb 6, 2021

I am with @noniagriconomie. I am also a bit confused =)

The way I see it, on the error page, you see the relative path to each line in the stack trace. If you click at any of them it will open in PHPStorm... if you dont have it configured that way, you will get the file opened in the broswer and there you will see the absolute path.

Screenshot 2021-02-06 at 17 13 36

@@ -25,6 +25,9 @@
<span class="trace-method"><?= $trace['function']; ?></span>
<?php } ?>
(line <?= $lineNumber; ?>)
<span class="icon icon-copy hidden" data-clipboard-text="<?php echo implode(\DIRECTORY_SEPARATOR, $filePathParts).':'.$lineNumber; ?>">
Copy link
Member

Choose a reason for hiding this comment

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

I think the data-clipboard-text should contain the full path to the file - not the formatted path that links to the IDE.
This means this should contain $trace['file'], html-escaped.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about that... when running app in docker, absolute path is useless, while relative pat works in IDE, and console (when CWD=root of project)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, you're right. We do have a way to map paths from docker to host, but it is bound to the IDE URLs. Can't we use that map somehow to compute absolute paths for the host?

@nicolas-grekas nicolas-grekas dismissed their stale review February 11, 2021 10:10

Good point raised by Nyholm.

@nicolas-grekas
Copy link
Member

I think that allowing a copy/paste of the path of the file is interesting.
In order to account for Docker/etc, we could allow passing a 3rd $format argument to FileLinkFormatter::format(), that would be used instead of $fmt[0] when possible, on the last line here:

public function format(string $file, int $line)
{
if ($fmt = $this->getFileLinkFormat()) {
for ($i = 1; isset($fmt[$i]); ++$i) {
if (0 === strpos($file, $k = $fmt[$i++])) {
$file = substr_replace($file, $fmt[$i], 0, \strlen($k));
break;
}
}
return strtr($fmt[0], ['%f' => $file, '%l' => $line]);

Then we would patch HtmlRenderer::getFileLink to also handle and forward the same optional argument.

@lmillucci works for you? Can you please update the PR to account for all latest comments?

@lmillucci
Copy link
Contributor Author

I think that allowing a copy/paste of the path of the file is interesting.
In order to account for Docker/etc, we could allow passing a 3rd $format argument to FileLinkFormatter::format(), that would be used instead of $fmt[0] when possible, on the last line here:

public function format(string $file, int $line)
{
if ($fmt = $this->getFileLinkFormat()) {
for ($i = 1; isset($fmt[$i]); ++$i) {
if (0 === strpos($file, $k = $fmt[$i++])) {
$file = substr_replace($file, $fmt[$i], 0, \strlen($k));
break;
}
}
return strtr($fmt[0], ['%f' => $file, '%l' => $line]);

Then we would patch HtmlRenderer::getFileLink to also handle and forward the same optional argument.

@lmillucci works for you? Can you please update the PR to account for all latest comments?

I'm not sure about the behavior of the $format argument of FileLinkFormatter::format(). I can add it to the function to override the default format but how should I use it in this situation?

@fabpot
Copy link
Member

fabpot commented Jun 23, 2021

I would display the copy button only on hover to avoid having too many icons on the page.

@lmillucci
Copy link
Contributor Author

I've updated the branch to display the icon only on hover. I'm still not sure about what should I do with FileLinkFormatter

This is an example:
copy_icon_gif

@fabpot
Copy link
Member

fabpot commented Jul 25, 2021

@lmillucci Can you rebase on the current 5.4 branch to resolve the conflicts? Ping me when it's done so that I can merge this PR. Thank you.

@lmillucci lmillucci force-pushed the errorhandler-copy-error-path branch from 3bee7af to d1a60bb Compare July 26, 2021 06:55
@lmillucci
Copy link
Contributor Author

@fabpot rebased 👍

@fabpot fabpot force-pushed the errorhandler-copy-error-path branch from d1a60bb to 5779f86 Compare July 26, 2021 07:21
@fabpot
Copy link
Member

fabpot commented Jul 26, 2021

Thank you @lmillucci.

@fabpot fabpot merged commit a19735b into symfony:5.4 Jul 26, 2021
This was referenced Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants