Skip to content

Commit f81be5f

Browse files
authored
Merge pull request #2914 from l0gicgate/AddMultipleErrorHandlers
Added ability to add handled exceptions as an array
2 parents d420a9e + bf4c579 commit f81be5f

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

Slim/Middleware/ErrorMiddleware.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function setDefaultErrorHandler($handler): self
188188
* The callable signature MUST match the ErrorHandlerInterface
189189
*
190190
* Pass true to $handleSubclasses to make the handler handle all subclasses of
191-
* the type as well.
191+
* the type as well. Pass an array of classes to make the same function handle multiple exceptions.
192192
*
193193
* @see \Slim\Interfaces\ErrorHandlerInterface
194194
*
@@ -201,19 +201,39 @@ public function setDefaultErrorHandler($handler): self
201201
* The callable MUST return an instance of
202202
* \Psr\Http\Message\ResponseInterface.
203203
*
204-
* @param string $type Exception/Throwable name. ie: RuntimeException::class
204+
* @param string|string[] $typeOrTypes Exception/Throwable name.
205+
* ie: RuntimeException::class or an array of classes
206+
* ie: [HttpNotFoundException::class, HttpMethodNotAllowedException::class]
205207
* @param callable|ErrorHandlerInterface $handler
206208
* @param bool $handleSubclasses
207209
* @return self
208210
*/
209-
public function setErrorHandler(string $type, $handler, bool $handleSubclasses = false): self
211+
public function setErrorHandler($typeOrTypes, $handler, bool $handleSubclasses = false): self
212+
{
213+
if (is_array($typeOrTypes)) {
214+
foreach ($typeOrTypes as $type) {
215+
$this->addErrorHandler($type, $handler, $handleSubclasses);
216+
}
217+
} else {
218+
$this->addErrorHandler($typeOrTypes, $handler, $handleSubclasses);
219+
}
220+
221+
return $this;
222+
}
223+
224+
/**
225+
* Used internally to avoid code repetition when passing multiple exceptions to setErrorHandler().
226+
* @param string $type
227+
* @param callable|ErrorHandlerInterface $handler
228+
* @param bool $handleSubclasses
229+
* @return void
230+
*/
231+
private function addErrorHandler(string $type, $handler, bool $handleSubclasses): void
210232
{
211233
if ($handleSubclasses) {
212234
$this->subClassHandlers[$type] = $handler;
213235
} else {
214236
$this->handlers[$type] = $handler;
215237
}
216-
217-
return $this;
218238
}
219239
}

tests/Middleware/ErrorMiddlewareTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,45 @@ public function testSuperclassExceptionHandlerDoesNotHandleSubclassException()
205205
$this->expectOutputString('Oops..');
206206
}
207207

208+
public function testHandleMultipleExceptionsAddedAsArray()
209+
{
210+
$responseFactory = $this->getResponseFactory();
211+
$app = new App($responseFactory);
212+
$callableResolver = $app->getCallableResolver();
213+
214+
$mw = new ErrorMiddleware($callableResolver, $this->getResponseFactory(), false, false, false);
215+
216+
$app->add(function ($request, $handler) {
217+
throw new InvalidArgumentException('This is an invalid argument exception...');
218+
});
219+
220+
$handler = (function (ServerRequestInterface $request, $exception) {
221+
$response = $this->createResponse();
222+
$response->getBody()->write($exception->getMessage());
223+
return $response;
224+
});
225+
226+
$mw->setErrorHandler([LogicException::class, InvalidArgumentException::class], $handler->bindTo($this));
227+
228+
$mw->setDefaultErrorHandler((function () {
229+
$response = $this->createResponse();
230+
$response->getBody()->write('Oops..');
231+
return $response;
232+
})->bindTo($this));
233+
234+
$app->add($mw);
235+
236+
$app->get('/foo', function (ServerRequestInterface $request, ResponseInterface $response) {
237+
$response->getBody()->write('...');
238+
return $response;
239+
});
240+
241+
$request = $this->createServerRequest('/foo');
242+
$app->run($request);
243+
244+
$this->expectOutputString('This is an invalid argument exception...');
245+
}
246+
208247
public function testErrorHandlerHandlesThrowables()
209248
{
210249
$responseFactory = $this->getResponseFactory();

0 commit comments

Comments
 (0)