Skip to content

Commit 8c5a921

Browse files
author
Marco Bunge
committed
Fix #24
1 parent 1b04d58 commit 8c5a921

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

src/Application.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -537,16 +537,18 @@ public function handleError(
537537
// get last occured exception
538538
$exception = $this->getLastException();
539539

540+
$message = $errorHandler->getErrorMessage($exception);
541+
542+
$errorResponse = $this->determineErrorResponse($exception, $message, $response, $request);
543+
540544
if (
541545
false === $catch
542546
&& true === $showError
543547
) {
544548
$this->throwException($exception);
545549
}
546550

547-
$message = $errorHandler->getErrorMessage($exception);
548-
549-
return $this->determineErrorResponse($exception, $message, $response, $request);
551+
return $errorResponse;
550552
}
551553

552554
/**
@@ -655,7 +657,7 @@ public function shutdown($response = null)
655657
public function terminateOutputBuffering($level = 0, $response = null)
656658
{
657659

658-
// close response stream berfore terminating output buffer
660+
// close response stream before terminating output buffer
659661
// and only if response is an instance of
660662
// \Psr\Http\ResponseInterface
661663
if ($response instanceof ResponseInterface) {
@@ -801,18 +803,18 @@ protected function initContentType()
801803
protected function initSystem(){
802804
// error handler
803805
set_error_handler(function($level, $message, $file = null, $line = null){
804-
$this->emit(self::EVENT_HANDLE_ERROR, [$level, $message, $file, $line]);
806+
$event = $this->getApplicationEvent();
807+
$this->emit($event->setName(self::EVENT_HANDLE_ERROR), $level, $message, $file, $line);
805808
});
806809

807810
// exception handler
808811
set_exception_handler(function($exception){
809-
$this->emit(self::EVENT_SYSTEM_EXCEPTION, [$exception]);
812+
$event = $this->getApplicationEvent();
813+
$this->emit($event->setName(self::EVENT_SYSTEM_EXCEPTION), $exception);
810814
});
811815

812816
// shutdown function
813-
register_shutdown_function(function (){
814-
$this->emit(self::EVENT_SYSTEM_SHUTDOWN);
815-
});
817+
register_shutdown_function([$this, 'shutdown']);
816818
}
817819

818820
}

src/Application/Services/Whoops/ApplicationSystemFacade.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ public function setErrorHandler(callable $handler, $types = 'use-php-defaults')
5454
$this->errorHandler = $handler;
5555
return $this->application->addListener(
5656
ApplicationInterface::EVENT_HANDLE_ERROR,
57-
function ($event, $data) use ($handler) {
58-
call_user_func_array($handler, $data);
57+
function ($event) use ($handler) {
58+
$args = func_get_args();
59+
array_shift($args);
60+
call_user_func_array($handler, $args);
5961
}
6062
);
6163
}
@@ -68,8 +70,10 @@ public function setExceptionHandler(callable $handler)
6870
{
6971
return $this->application->addListener(
7072
ApplicationInterface::EVENT_SYSTEM_EXCEPTION,
71-
function ($event, $data) use ($handler) {
72-
call_user_func_array($handler, $data);
73+
function ($event) use ($handler) {
74+
$args = func_get_args();
75+
array_shift($args);
76+
call_user_func_array($handler, $args);
7377
}
7478
);
7579
}
@@ -100,7 +104,7 @@ public function registerShutdownFunction(callable $function)
100104
{
101105
return $this->application->addListener(
102106
ApplicationInterface::EVENT_SYSTEM_SHUTDOWN,
103-
function () use ($function) {
107+
function ($event) use ($function) {
104108
call_user_func_array($function, []);
105109
}
106110
);

tests/ApplicationTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public function testTerminate()
112112
{
113113
$app = new Application();
114114

115+
$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response, array $args = []) {
116+
return $response;
117+
});
118+
115119
$app->addListener('response.sent', function (HttpApplicationEvent $event) {
116120
$this->assertInstanceOf('League\Event\Event', $event);
117121
$this->assertInstanceOf(ServerRequestInterface::class, $event->getRequest());
@@ -205,6 +209,7 @@ public function testHandleAutoWiringControllerAction()
205209
*/
206210
public function testHandleWithOtherException()
207211
{
212+
208213
$app = new Application();
209214

210215
$request = ServerRequestFactory::fromGlobals();
@@ -225,12 +230,11 @@ public function testHandleWithOtherException()
225230
}
226231

227232
/**
228-
*
233+
* This should not throw errors
229234
*/
230-
public function testExceptionHandling()
235+
public function testHttpErrorHandling()
231236
{
232237
$app = new Application();
233-
$app->setConfig('error', false);
234238

235239
$request = ServerRequestFactory::fromGlobals();
236240

@@ -404,7 +408,7 @@ public function testApplicationMiddlewareOnError()
404408
{
405409
$app = new Application();
406410
$handledOnError = false;
407-
$app->addMiddleware(function(ServerRequestInterface $request, ResponseInterface $response, callable $next) use (&$handledOnError){
411+
$app->addMiddleware(function (ServerRequestInterface $request, ResponseInterface $response, callable $next) use (&$handledOnError) {
408412
$handledOnError = true;
409413
return $response;
410414
});
@@ -420,7 +424,7 @@ public function testApplicationMiddleware()
420424
$app = new Application();
421425
$handled = false;
422426
$app->get('/', [TestController::class, 'getIndex']);
423-
$app->addMiddleware(function(ServerRequestInterface $request, ResponseInterface $response, callable $next) use (&$handled){
427+
$app->addMiddleware(function (ServerRequestInterface $request, ResponseInterface $response, callable $next) use (&$handled) {
424428
$handled = true;
425429
$response->getBody()->write('<');
426430
/** @var ResponseInterface $response */
@@ -439,13 +443,12 @@ public function testApplicationMiddleware()
439443
public function testErrorHandler()
440444
{
441445
$app = new Application();
442-
$response = $app->handle(ServerRequestFactory::fromGlobals());
443446

444-
$text = $response->getBody()->__toString();
447+
$response = $app->handle(ServerRequestFactory::fromGlobals());
448+
$app->shutdown($response);
445449

446450
$this->assertTrue($app->isError());
447-
$this->assertEquals('League\Route\Http\Exception\NotFoundException', substr($text, 0, strpos($text, ':')));
448-
$this->assertInstanceOf('League\Route\Http\Exception\NotFoundException', $app->getLastException());
451+
$this->assertInstanceOf('\League\Route\Http\Exception\NotFoundException', $app->getLastException());
449452
}
450453

451454

0 commit comments

Comments
 (0)