Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64d02cb

Browse files
committed
[Debug] Make ExceptionHandler and ErrorHandler final
1 parent cf8cfeb commit 64d02cb

File tree

3 files changed

+66
-44
lines changed

3 files changed

+66
-44
lines changed

src/Symfony/Component/ErrorCatcher/ExceptionHandler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\ErrorCatcher;
1313

1414
use Symfony\Component\ErrorCatcher\ErrorRenderer\HtmlErrorRenderer;
15+
use Symfony\Component\ErrorCatcher\Exception\FatalThrowableError;
1516
use Symfony\Component\ErrorCatcher\Exception\FlattenException;
1617
use Symfony\Component\ErrorCatcher\Exception\OutOfMemoryException;
1718
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
@@ -101,8 +102,12 @@ public function setHandler(callable $handler = null)
101102
* The latter takes precedence and any output from the former is cancelled,
102103
* if and only if nothing bad happens in this handling path.
103104
*/
104-
public function handle(\Exception $exception)
105+
public function handle(\Throwable $exception)
105106
{
107+
if (!$exception instanceof \Exception) {
108+
$exception = new FatalThrowableError($exception);
109+
}
110+
106111
if (null === $this->handler || $exception instanceof OutOfMemoryException) {
107112
$this->sendPhpResponse($exception);
108113

src/Symfony/Component/ErrorCatcher/Tests/ExceptionHandlerTest.php

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testHeaders()
8585

8686
ob_start();
8787
$handler->sendPhpResponse(new MethodNotAllowedHttpException(['POST']));
88-
$response = ob_get_clean();
88+
ob_get_clean();
8989

9090
$expectedHeaders = [
9191
['HTTP/1.0 405', true, null],
@@ -106,37 +106,78 @@ public function testNestedExceptions()
106106
$this->assertStringMatchesFormat('%A<p class="break-long-words trace-message">Foo</p>%A<p class="break-long-words trace-message">Bar</p>%A', $response);
107107
}
108108

109-
public function testHandle()
109+
/**
110+
* @dataProvider handleProvider
111+
*/
112+
public function testHandle(\Throwable $exception, string $expectedClass, string $expectedTitle, string $expectedMessage)
110113
{
111-
$exception = new \Exception('foo');
112-
113-
$handler = $this->getMockBuilder('Symfony\Component\ErrorCatcher\ExceptionHandler')->setMethods(['sendPhpResponse'])->getMock();
114-
$handler
115-
->expects($this->exactly(2))
116-
->method('sendPhpResponse');
114+
$handler = new ExceptionHandler(true);
115+
ob_start();
117116

118117
$handler->handle($exception);
119118

120-
$handler->setHandler(function ($e) use ($exception) {
121-
$this->assertSame($exception, $e);
119+
$this->assertThatTheExceptionWasOutput(ob_get_clean(), $expectedClass, $expectedTitle, $expectedMessage);
120+
}
121+
122+
public function handleProvider()
123+
{
124+
return [
125+
[new \Exception('foo'), \Exception::class, 'Exception', 'foo'],
126+
[new \Error('bar'), \Error::class, 'Error', 'bar'],
127+
];
128+
}
129+
130+
public function testHandleWithACustomHandlerThatOutputsSomething()
131+
{
132+
$handler = new ExceptionHandler(true);
133+
ob_start();
134+
$handler->setHandler(function () {
135+
echo 'ccc';
122136
});
123137

124-
$handler->handle($exception);
138+
$handler->handle(new \Exception());
139+
ob_end_flush(); // Necessary because of this PHP bug : https://bugs.php.net/bug.php?id=76563
140+
$this->assertSame('ccc', ob_get_clean());
125141
}
126142

127-
public function testHandleOutOfMemoryException()
143+
public function testHandleWithACustomHandlerThatOutputsNothing()
144+
{
145+
$handler = new ExceptionHandler(true);
146+
$handler->setHandler(function () {});
147+
148+
$handler->handle(new \Exception('ccc'));
149+
150+
$this->assertThatTheExceptionWasOutput(ob_get_clean(), \Exception::class, 'Exception', 'ccc');
151+
}
152+
153+
public function testHandleWithACustomHandlerThatFails()
128154
{
129-
$exception = new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__);
155+
$handler = new ExceptionHandler(true);
156+
$handler->setHandler(function () {
157+
throw new \RuntimeException();
158+
});
159+
160+
$handler->handle(new \Exception('ccc'));
130161

131-
$handler = $this->getMockBuilder('Symfony\Component\ErrorCatcher\ExceptionHandler')->setMethods(['sendPhpResponse'])->getMock();
132-
$handler
133-
->expects($this->once())
134-
->method('sendPhpResponse');
162+
$this->assertThatTheExceptionWasOutput(ob_get_clean(), \Exception::class, 'Exception', 'ccc');
163+
}
135164

136-
$handler->setHandler(function ($e) {
165+
public function testHandleOutOfMemoryException()
166+
{
167+
$handler = new ExceptionHandler(true);
168+
ob_start();
169+
$handler->setHandler(function () {
137170
$this->fail('OutOfMemoryException should bypass the handler');
138171
});
139172

140-
$handler->handle($exception);
173+
$handler->handle(new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__));
174+
175+
$this->assertThatTheExceptionWasOutput(ob_get_clean(), OutOfMemoryException::class, 'OutOfMemoryException', 'foo');
176+
}
177+
178+
private function assertThatTheExceptionWasOutput(string $content, string $expectedClass, string $expectedTitle, string $expectedMessage)
179+
{
180+
$this->assertContains(sprintf('<span class="exception_title"><abbr title="%s">%s</abbr></span>', $expectedClass, $expectedTitle), $content);
181+
$this->assertContains(sprintf('<h1 class="break-long-words exception-message">%s</h1>', $expectedMessage), $content);
141182
}
142183
}

src/Symfony/Component/ErrorCatcher/Tests/MockExceptionHandler.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)