-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[ErrorHandler] Improve error messages in CLI #42261
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
Conversation
Sounds like a plan to me... I would like to add (if possible)
|
FYI. I'm not in favor of removing the stack trace (completely) in the CLI as there are times this is critically important. |
For completeness, something I suggested on Slack: are there disadvantages to always using
I would say this should be included in the exception message. We often try to make exception messages actionable, so please open an issue if you find a vague non-actionable exception message :) |
I think it could be improved, but wouldn't consider it a critical priority by any means. Right now we can configure our console output through means of verbosity flags. Perhaps it would be do-able to describe the output of error messages in means of smaller message parts. With those parts being the 3 points being named. On another note, I generally find the exception messages with red backgrounds rather difficult to read on the jetbrains built-in terminals, because of the white text. Maybe it's some configuration about graphics, I don't know, but some other colleagues of mine are also struggling with it. Not sure if that falls anywhere within the scope of this topic, though :) |
$result .= $textBold.$textBrightRed.\get_class($exception).$resetStyle."\n"; | ||
$result .= $exception->getMessage()."\n\n"; | ||
|
||
$sourceCode =file($exception->getFile()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing space?
I like the idea, I think we need to move forward and merge something along the lines of this PR. We will always be able to improve in the future (so, we don't need it to be perfect). |
@javiereguiluz friendly ping :) can we test if for sf6.2 ? cc @fabpot |
With the release of 6.2 beta1 last week, it has reached feature freeze now. Let's focus on getting the ecosystem ready for Symfony 6.2 and get back to new features after SymfonyCon 😃 |
ho yes indeed! |
friendly ping @javiereguiluz cf my previous comment 🙂 I know it is yet one another ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍
I'm still working on this. I'm not fully ready yet to commit the changes, but I'll try to do soon. Thanks. |
10455d8
to
2c7b2fc
Compare
Here's a screenshot of the latest changes: Comments:
Some questions:
Thanks! |
@javiereguiluz personally I much prefer it over the old version. Not quite as much as collision, but I also don't know what to suggest to really improve it further. Regarding the stack trace: I would suggest to only display it on verbose or higher. |
that's only true at normal verbosity. In verbose mode, it also shows the stack trace. |
I ❤️ the Collision display (I'd not seen it before this post) but for a fairer comparison, how does it handle an exception where you need to see a stack trace? |
], '', __FILE__); | ||
$projectPrefix = \dirname($vendorPrefix).DIRECTORY_SEPARATOR; | ||
|
||
return str_replace($projectPrefix, '', $absolutePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead you can use Path directly:
Path::makeRelative($absolutePath, $projectPrefix);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The component currently doesn't depend on symfony/filesystem
- and probably not worth trying to include it just for this.
$terminalSupportsLinks = 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR') && (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100); | ||
$href = sprintf('file://%s#L%d', $filePath, $lineNumber); | ||
|
||
return $terminalSupportsLinks ? "\033]8;;{$href}\033\\{$content}\033]8;;\033\\" : $content; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would there be no way to extract this bit? This is repeated from
symfony/src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Lines 445 to 446 in 5a2ee5a
$this->handlesHrefGracefully ??= 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR') | |
&& (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repeating that isn't ideal - add a CliDumper::handlesHrefGracefully()
public static function? It's not a blocker for me - it can be done later or not.
@javiereguiluz I'd love to be able to merge this one in 6.3 :) |
Hello 👋 I have understood that the console component may/will have some improvements for v6.4/v7.0 So i am asking if this could/should be also part of it? |
f21c7c0
to
87530ef
Compare
So solving the inconsistency issue is no longer in scope of this PR? |
@ostrolucky yes. Actually, it's not a bad inconsistency. See the previous error: some env var is missing and you see this error: ![]() I'd say that the error message is both very clear and very concise. It's better than using the newer format ... which is more convenient for other errors where seeing some code context is useful. By the way, that old format error is generated here:
I don't know if we could just change that and not break things. |
I have two questions for @nicolas-grekas:
Thanks! |
I would remove the ghost emoji. Until recently, my terminal emulator would crash when displaying emoji's. I'm not sure, but I guess there are more terminal emulators that can't properly deal with emoji's. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few notes, but mostly, this seems like a nice DX improvement (and Javier clearly focused on the details how how it looked) and a safe spot. If there are some tweaks that are needed, they should be easy to make.
], '', __FILE__); | ||
$projectPrefix = \dirname($vendorPrefix).DIRECTORY_SEPARATOR; | ||
|
||
return str_replace($projectPrefix, '', $absolutePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The component currently doesn't depend on symfony/filesystem
- and probably not worth trying to include it just for this.
use Symfony\Component\ErrorHandler\Exception\FlattenException; | ||
use Symfony\Component\VarDumper\Cloner\VarCloner; | ||
use Symfony\Component\VarDumper\Dumper\CliDumper; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This use
statement is not needed anymore, and neither is the class_exists
below.
$terminalSupportsLinks = 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR') && (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100); | ||
$href = sprintf('file://%s#L%d', $filePath, $lineNumber); | ||
|
||
return $terminalSupportsLinks ? "\033]8;;{$href}\033\\{$content}\033]8;;\033\\" : $content; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repeating that isn't ideal - add a CliDumper::handlesHrefGracefully()
public static function? It's not a blocker for me - it can be done later or not.
Folks, I realize this might be disappointing for some, but I'm closing this PR as "won't fix". As it current state, this PR is almost "useless". Why? Because the new design is almost never visible, only when you have some PHP syntax error. That's why I don't think all this complication is not worth it. For Symfony 7.1 we could try to update the styles of ALL errors in the CLI. Personally, I don't think we have to do this because current CLI error design is fine. But if someone wants to propose some tweaks and enhancements, we can consider those. Thanks! |
This is still WIP. Don't look or comment the code changes. Thanks!
Context
When there's an exception in your Symfony app and you're in the command console, you see something like this:
Although sometimes you see exceptions like this:
I see two problems here:
Problem
To me the main problem is designing this error message around "stack traces" (like in Java) instead of focusing on the error cause and location (like in modern languages/tools).
See "Rails + Pry" exception message:
See "Collision" exception message (used by Laravel and compatible with Symfony too):
See the amazing "Rust" error messages:
Proposal
I'd like to redesign Symfony exception messages to match that modern design.
The three main elements of the console output should be:
The code of this PR is a quick proof-of-concept of this idea:
The logical order would be "What -> Where -> Why" ... but "Why" is a long and ugly stack trace which hides the most important information ("What" and "Where"). That's why the order is "Why -> What -> Where".
What do you think?
Thanks!