From 6a45e0b0447fe9f745f2fc50918d9b8cd61fa7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20B=C3=83=C2=A9rub=C3=83=C2=A9?= Date: Sat, 4 Jan 2020 20:03:18 -0700 Subject: [PATCH 1/2] tidy up --- Slim/Middleware/ErrorMiddleware.php | 34 ++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/Slim/Middleware/ErrorMiddleware.php b/Slim/Middleware/ErrorMiddleware.php index 47232d347..170e68734 100644 --- a/Slim/Middleware/ErrorMiddleware.php +++ b/Slim/Middleware/ErrorMiddleware.php @@ -1,4 +1,5 @@ addErrorHandler($type, $handler, $handleSubclasses); + } + } else { + $this->addErrorHandler($typeOrTypes, $handler, $handleSubclasses); + } + + return $this; + } + + /** + * Used internally to avoid code repetition when passing multiple exceptions to setErrorHandler(). + * @param string $type + * @param callable|ErrorHandlerInterface $handler + * @param bool $handleSubclasses + * @return void + */ + private function addErrorHandler(string $type, $handler, bool $handleSubclasses): void { if ($handleSubclasses) { $this->subClassHandlers[$type] = $handler; } else { $this->handlers[$type] = $handler; } - - return $this; } } From 6537a86c5b893d488adc08a1e0914912ab1917dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20B=C3=83=C2=A9rub=C3=83=C2=A9?= Date: Sat, 4 Jan 2020 20:03:18 -0700 Subject: [PATCH 2/2] add tests for multiple error exception handlers --- tests/Middleware/ErrorMiddlewareTest.php | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/Middleware/ErrorMiddlewareTest.php b/tests/Middleware/ErrorMiddlewareTest.php index 3ebfc8d52..8326f0960 100644 --- a/tests/Middleware/ErrorMiddlewareTest.php +++ b/tests/Middleware/ErrorMiddlewareTest.php @@ -1,4 +1,5 @@ expectOutputString('Oops..'); } + public function testHandleMultipleExceptionsAddedAsArray() + { + $responseFactory = $this->getResponseFactory(); + $app = new App($responseFactory); + $callableResolver = $app->getCallableResolver(); + + $mw = new ErrorMiddleware($callableResolver, $this->getResponseFactory(), false, false, false); + + $app->add(function ($request, $handler) { + throw new InvalidArgumentException('This is an invalid argument exception...'); + }); + + $handler = (function (ServerRequestInterface $request, $exception) { + $response = $this->createResponse(); + $response->getBody()->write($exception->getMessage()); + return $response; + }); + + $mw->setErrorHandler([LogicException::class, InvalidArgumentException::class], $handler->bindTo($this)); + + $mw->setDefaultErrorHandler((function () { + $response = $this->createResponse(); + $response->getBody()->write('Oops..'); + return $response; + })->bindTo($this)); + + $app->add($mw); + + $app->get('/foo', function (ServerRequestInterface $request, ResponseInterface $response) { + $response->getBody()->write('...'); + return $response; + }); + + $request = $this->createServerRequest('/foo'); + $app->run($request); + + $this->expectOutputString('This is an invalid argument exception...'); + } + public function testErrorHandlerHandlesThrowables() { $responseFactory = $this->getResponseFactory();