Skip to content

Commit

Permalink
[ErrorHandler] Improve fileLinkFormat handling (#50619)
Browse files Browse the repository at this point in the history
- Avoid repeating file link format guessing (logic is already in FileLinkFormatter class)
- Always set a fileLinkFormat to a FileLinkFormatter object to handle path mappings properly
  • Loading branch information
nlemoine committed Jun 21, 2023
1 parent e0cb460 commit 81c102d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public function __construct(bool|callable $debug = false, string $charset = null
{
$this->debug = \is_bool($debug) ? $debug : $debug(...);
$this->charset = $charset ?: (\ini_get('default_charset') ?: 'UTF-8');
$fileLinkFormat ??= $_ENV['SYMFONY_IDE'] ?? $_SERVER['SYMFONY_IDE'] ?? null;
$this->fileLinkFormat = \is_string($fileLinkFormat)
? (ErrorRendererInterface::IDE_LINK_FORMATS[$fileLinkFormat] ?? $fileLinkFormat ?: false)
: ($fileLinkFormat ?: \ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: false);
$this->fileLinkFormat = \is_string($fileLinkFormat) ? new FileLinkFormatter($fileLinkFormat) : ($fileLinkFormat ?: false);
if (false === $this->fileLinkFormat) {
$this->fileLinkFormat = new FileLinkFormatter();
}
$this->projectDir = $projectDir;
$this->outputBuffer = \is_string($outputBuffer) ? $outputBuffer : $outputBuffer(...);
$this->logger = $logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,49 @@ public function testRender(\Throwable $exception, HtmlErrorRenderer $errorRender
$this->assertStringMatchesFormat($expected, $errorRenderer->render($exception)->getAsString());
}

/**
* @dataProvider provideFileLinkFormats
*/
public function testFileLinkFormat(\ErrorException $exception, string $fileLinkFormat, bool $withSymfonyIde, string $expected)
{
if ($withSymfonyIde) {
$_ENV['SYMFONY_IDE'] = $fileLinkFormat;
}
$errorRenderer = new HtmlErrorRenderer(true, null, $withSymfonyIde ? null : $fileLinkFormat);

$this->assertStringContainsString($expected, $errorRenderer->render($exception)->getAsString());
}

public function provideFileLinkFormats(): iterable
{
$exception = new \ErrorException('Notice', 0, \E_USER_NOTICE);

yield 'file link format set as known IDE with SYMFONY_IDE' => [
$exception,
'vscode',
true,
'href="vscode://file/'.__DIR__,
];
yield 'file link format set as a raw format with SYMFONY_IDE' => [
$exception,
'phpstorm://open?file=%f&line=%l',
true,
'href="phpstorm://open?file='.__DIR__,
];
yield 'file link format set as known IDE without SYMFONY_IDE' => [
$exception,
'vscode',
false,
'href="vscode://file/'.__DIR__,
];
yield 'file link format set as a raw format without SYMFONY_IDE' => [
$exception,
'phpstorm://open?file=%f&line=%l',
false,
'href="phpstorm://open?file='.__DIR__,
];
}

public static function getRenderData(): iterable
{
$expectedDebug = <<<HTML
Expand Down

0 comments on commit 81c102d

Please sign in to comment.