Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 4.2.1 under development

- no changes in this release.
- New #156: Pass request to callable value in `ExceptionResponder` middleware (@vjik)

## 4.2.0 August 19, 2025

Expand Down
5 changes: 4 additions & 1 deletion src/Middleware/ExceptionResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

if (is_callable($responseHandler)) {
/** @var ResponseInterface */
return $this->injector->invoke($responseHandler, ['exception' => $t]);
return $this->injector->invoke($responseHandler, [
'exception' => $t,
'request' => $request,
]);
}
}
}
Expand Down
25 changes: 17 additions & 8 deletions tests/Middleware/ExceptionResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use HttpSoft\Message\Response;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\ResponseTrait;
use HttpSoft\Message\ServerRequest;
use HttpSoft\Message\ServerRequestFactory;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -41,18 +42,26 @@ public function testCode(): void

public function testCallable(): void
{
$request = new ServerRequest(headers: ['X-TEST' => ['HELLO']]);
$middleware = $this->createMiddleware([
DomainException::class => function (ResponseFactoryInterface $responseFactory) {
return $responseFactory->createResponse(Status::CREATED);
},
DomainException::class =>
static function (ResponseFactoryInterface $responseFactory, ServerRequestInterface $request) {
return $responseFactory->createResponse(Status::CREATED, $request->getHeaderLine('X-TEST'));
},
]);

$this->assertSame(
Status::CREATED,
$this
->process($middleware)
->getStatusCode(),
$response = $middleware->process(
$request,
new class () implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
throw new DomainException();
}
},
);

$this->assertSame(Status::CREATED, $response->getStatusCode());
$this->assertSame('HELLO', $response->getReasonPhrase());
}

public function testAnotherException(): void
Expand Down
Loading