Skip to content

Commit

Permalink
Merge pull request #2914 from l0gicgate/AddMultipleErrorHandlers
Browse files Browse the repository at this point in the history
Added ability to add handled exceptions as an array
  • Loading branch information
l0gicgate committed Jan 5, 2020
2 parents d420a9e + bf4c579 commit f81be5f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
30 changes: 25 additions & 5 deletions Slim/Middleware/ErrorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function setDefaultErrorHandler($handler): self
* The callable signature MUST match the ErrorHandlerInterface
*
* Pass true to $handleSubclasses to make the handler handle all subclasses of
* the type as well.
* the type as well. Pass an array of classes to make the same function handle multiple exceptions.
*
* @see \Slim\Interfaces\ErrorHandlerInterface
*
Expand All @@ -201,19 +201,39 @@ public function setDefaultErrorHandler($handler): self
* The callable MUST return an instance of
* \Psr\Http\Message\ResponseInterface.
*
* @param string $type Exception/Throwable name. ie: RuntimeException::class
* @param string|string[] $typeOrTypes Exception/Throwable name.
* ie: RuntimeException::class or an array of classes
* ie: [HttpNotFoundException::class, HttpMethodNotAllowedException::class]
* @param callable|ErrorHandlerInterface $handler
* @param bool $handleSubclasses
* @return self
*/
public function setErrorHandler(string $type, $handler, bool $handleSubclasses = false): self
public function setErrorHandler($typeOrTypes, $handler, bool $handleSubclasses = false): self
{
if (is_array($typeOrTypes)) {
foreach ($typeOrTypes as $type) {
$this->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;
}
}
39 changes: 39 additions & 0 deletions tests/Middleware/ErrorMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,45 @@ public function testSuperclassExceptionHandlerDoesNotHandleSubclassException()
$this->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();
Expand Down

0 comments on commit f81be5f

Please sign in to comment.