From f6bc9da34eaaa9983cdc4d1596f17529a76a6135 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 11 Nov 2016 11:26:56 -0600 Subject: [PATCH] Prevent deprecation notice based on triggering error middleware Users cannot yet set the raiseThrowables flag, as there are not shipped facilities for handling errors using templates or whoops. As such, currently, if the queue is exhausted or an exception is thrown, error middleware is triggered. This patch adds an error handler to `Application::__invoke()` to swallow deprecation notices due to invocation of error middleware. --- src/Application.php | 12 +++++++++++- test/Container/ApplicationFactoryIntegrationTest.php | 12 ------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/Application.php b/src/Application.php index 6444aebd..1af00bc6 100644 --- a/src/Application.php +++ b/src/Application.php @@ -130,6 +130,8 @@ public function __construct( * If $out is not provided, uses the result of `getFinalHandler()`. * * @todo Remove logic for creating final handler for version 2.0. + * @todo Remove error handler for deprecation notice due to triggering + * error middleware for version 2.0.0. * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable|null $out @@ -137,6 +139,10 @@ public function __construct( */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null) { + set_error_handler(function ($errno, $errstr) { + return false !== strstr($errstr, 'error middleware is deprecated'); + }, E_USER_DEPRECATED); + if (! $out && (null === ($out = $this->getFinalHandler($response)))) { $response = $response instanceof StratigilityResponse ? $response @@ -144,7 +150,11 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res $out = new FinalHandler([], $response); } - return parent::__invoke($request, $response, $out); + $result = parent::__invoke($request, $response, $out); + + restore_error_handler(); + + return $result; } /** diff --git a/test/Container/ApplicationFactoryIntegrationTest.php b/test/Container/ApplicationFactoryIntegrationTest.php index 293031a7..fd05fc60 100644 --- a/test/Container/ApplicationFactoryIntegrationTest.php +++ b/test/Container/ApplicationFactoryIntegrationTest.php @@ -128,14 +128,8 @@ public function testConfiguredErrorMiddlewarePipeIsExecutedWhenMiddlewareCallsNe $request = new ServerRequest([], [], 'http://example.com/needs/authentication', 'GET'); $response = new Response(); - set_error_handler(function ($errno, $errstr) { - return false !== strstr($errstr, 'error middleware is deprecated'); - }, E_USER_DEPRECATED); - $response = $app($request, $response); - restore_error_handler(); - $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals(401, $response->getStatusCode(), 'Unexpected response'); $this->assertTrue($response->hasHeader('X-Always')); @@ -222,14 +216,8 @@ public function testConfiguredErrorMiddlewareIsExecutedWhenMiddlewareCallsNextWi $request = new ServerRequest([], [], 'http://example.com/needs/authentication', 'GET'); $response = new Response(); - set_error_handler(function ($errno, $errstr) { - return false !== strstr($errstr, 'error middleware is deprecated'); - }, E_USER_DEPRECATED); - $response = $app($request, $response); - restore_error_handler(); - $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals(401, $response->getStatusCode(), 'Unexpected response'); $this->assertTrue($response->hasHeader('X-Always'));