8000 [ErrorCatcher] Make IDEs and static analysis tools happy by fabpot · Pull Request #32221 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[ErrorCatcher] Make IDEs and static analysis tools happy #32221

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 2 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(HttpKernelInterface $kernel, $controller)

public function previewErrorPageAction(Request $request, $code)
{
$exception = FlattenException::create(new \Exception('Something has intentionally gone wrong.'), $code);
$exception = FlattenException::createFromThrowable(new \Exception('Something has intentionally gone wrong.'), $code);

/*
* This Request mimics the parameters set by
Expand Down
10000
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testShowActionCanBeForcedToShowErrorPage()

$request = $this->createRequest('html');
$request->attributes->set('showException', false);
$exception = FlattenException::create(new \Exception(), 404);
$exception = FlattenException::createFromThrowable(new \Exception(), 404);
$controller = new ExceptionController($twig, /* "showException" defaults to --> */ true);

$response = $controller->showAction($request, $exception, null);
Expand All @@ -40,7 +40,7 @@ public function testFallbackToHtmlIfNoTemplateForRequestedFormat()
$twig = $this->createTwigEnv(['@Twig/Exception/error.html.twig' => '<html></html>']);

$request = $this->createRequest('txt');
$exception = FlattenException::create(new \Exception());
$exception = FlattenException::createFromThrowable(new \Exception());
$controller = new ExceptionController($twig, false);

$controller->showAction($request, $exception);
Expand All @@ -54,7 +54,7 @@ public function testFallbackToHtmlWithFullExceptionIfNoTemplateForRequestedForma

$request = $this->createRequest('txt');
$request->attributes->set('showException', true);
$exception = FlattenException::create(new \Exception());
$exception = FlattenException::createFromThrowable(new \Exception());
$controller = new ExceptionController($twig, false);

$controller->showAction($request, $exception);
Expand All @@ -67,7 +67,7 @@ public function testResponseHasRequestedMimeType()
$twig = $this->createTwigEnv(['@Twig/Exception/error.json.twig' => '{}']);

$request = $this->createRequest('json');
$exception = FlattenException::create(new \Exception());
$exception = FlattenException::createFromThrowable(new \Exception());
$controller = new ExceptionController($twig, false);

$response = $controller->showAction($request, $exception);
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Debug/Exception/FlattenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@
*/
class FlattenException extends BaseFlattenException
{
/**
* @deprecated since Symfony 4.4, use Symfony\Component\ErrorHandler\Exception::createFromThrowable() instead.
*/
public static function create(\Exception $exception, $statusCode = null, array $headers = []): self
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.4, use Symfony\Component\ErrorHandler\Exception::createFromThrowable() instead.', __METHOD__), E_USER_DEPRECATED);

return parent::createFromThrowable($exception, $statusCode, $headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function addRenderer(ErrorRendererInterface $renderer, string $format): s
/**
* Renders an Exception and returns the Response content.
*
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance
* @param \Throwable|FlattenException $exception A \Throwable or FlattenException instance
* @param string $format The request format (html, json, xml, etc.)
*
* @return string The Response content as a string
Expand All @@ -62,8 +62,8 @@ public function render($exception, string $format = 'html'): string
throw new ErrorRendererNotFoundException(sprintf('No error renderer found for format "%s".', $format));
}

if (!$exception instanceof FlattenException) {
$exception = FlattenException::create($exception);
if ($exception instanceof \Throwable) {
$exception = FlattenException::createFromThrowable($exception);
}

return $this->renderers[$format]->render($exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function getBody(FlattenException $exception)
} catch (\Exception $e) {
// something nasty happened and we cannot throw an exception anymore
if ($this->debug) {
$e = FlattenException::create($e);
$e = FlattenException::createFromThrowable($e);
$exceptionMessage = sprintf('Exception thrown when handling an exception (%s: %s)', $e->getClass(), $this->escapeHtml($e->getMessage()));
} else {
$exceptionMessage = 'Whoops, looks like something went wrong.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ class FlattenException
private $file;
private $line;

public static function create(\Exception $exception, $statusCode = null, array $headers = [])
{
return static::createFromThrowable($exception, $statusCode, $headers);
}

public static function createFromThrowable(\Throwable $exception, ?int $statusCode = null, array $headers = []): self
{
$e = new static();
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/ErrorCatcher/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ public function handle(\Exception $exception)
* This method uses plain PHP functions like header() and echo to output
* the response.
*
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance
* @param \Throwable|FlattenException $exception A \Throwable or FlattenException instance
*/
public function sendPhpResponse($exception)
{
if (!$exception instanceof FlattenException) {
$exception = FlattenException::create($exception);
if ($exception instanceof \Throwable) {
$exception = FlattenException::createFromThrowable($exception);
}

if (!headers_sent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testInvalidErrorRenderer()
$container = $this->getMockBuilder('Psr\Container\ContainerInterface')->getMock();
$container->expects($this->once())->method('has')->with('foo')->willReturn(false);

$exception = FlattenException::create(new \Exception('Foo'));
$exception = FlattenException::createFromThrowable(new \Exception('Foo'));
(new ErrorRenderer($container))->render($exception, 'foo');
}

Expand All @@ -48,7 +48,7 @@ public function testCustomErrorRenderer()

$errorRenderer = new ErrorRenderer($container);

$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$this->assertSame('Foo', $errorRenderer->render($exception, 'foo'));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ErrorRendererTest extends TestCase
*/
public function testErrorRendererNotFound()
{
$exception = FlattenException::create(new \Exception('foo'));
$exception = FlattenException::createFromThrowable(new \Exception('foo'));
(new ErrorRenderer([]))->render($exception, 'foo');
}

Expand All @@ -34,7 +34,7 @@ public function testErrorRendererNotFound()
*/
public function testInvalidErrorRenderer()
{
$exception = FlattenException::create(new \Exception('foo'));
$exception = FlattenException::createFromThrowable(new \Exception('foo'));
(new ErrorRenderer([new \stdClass()]))->render($exception, 'foo');
}

Expand All @@ -43,7 +43,7 @@ public function testCustomErrorRenderer()
$renderers = [new FooErrorRenderer()];
$errorRenderer = new ErrorRenderer($renderers);

$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$this->assertSame('Foo', $errorRenderer->render($exception, 'foo'));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class HtmlErrorRendererTest extends TestCase
{
public function testRender()
{
$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$expected = '<!DOCTYPE html>%A<html>%A<head>%A<title>Internal Server Error</title>%A<h1 class="break-long-words exception-message">Foo</h1>%A<abbr title="RuntimeException">RuntimeException</abbr>%A';

$this->assertStringMatchesFormat($expected, (new HtmlErrorRenderer())->render($exception));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JsonErrorRendererTest extends TestCase
{
public function testRender()
{
$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$expected = '{"title":"Internal Server Error","status":500,"detail":"Foo","exceptions":[{"message":"Foo","class":"RuntimeException","trace":';

$this->assertStringStartsWith($expected, (new JsonErrorRenderer())->render($exception));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TxtErrorRendererTest extends TestCase
{
public function testRender()
{
$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$expected = '[title] Internal Server Error%A[status] 500%A[detail] Foo%A[1] RuntimeException: Foo%A';

$this->assertStringMatchesFormat($expected, (new TxtErrorRenderer())->render($exception));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class XmlErrorRendererTest extends TestCase
{
public function testRender()
{
$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$expected = '<?xml version="1.0" encoding="UTF-8" ?>%A<problem xmlns="urn:ietf:rfc:7807">%A<title>Internal Server Error</title>%A<status>500</status>%A<detail>Foo</detail>%A';

$this->assertStringMatchesFormat($expected, (new XmlErrorRenderer())->render($exception));
Expand Down
Loading
0