From ddc8e96ca1d60c7aed963f3ccd2c10e553f13edd Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 5 Sep 2025 12:33:01 +0300 Subject: [PATCH 1/2] Pass request to callable value in `ExceptionResponder` middleware --- CHANGELOG.md | 2 +- src/Middleware/ExceptionResponder.php | 5 +++- tests/Middleware/ExceptionResponderTest.php | 26 ++++++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bac5bb..a711c56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Middleware/ExceptionResponder.php b/src/Middleware/ExceptionResponder.php index b912c44..7af60e9 100644 --- a/src/Middleware/ExceptionResponder.php +++ b/src/Middleware/ExceptionResponder.php @@ -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, + ]); } } } diff --git a/tests/Middleware/ExceptionResponderTest.php b/tests/Middleware/ExceptionResponderTest.php index 3d51e45..27645c8 100644 --- a/tests/Middleware/ExceptionResponderTest.php +++ b/tests/Middleware/ExceptionResponderTest.php @@ -5,9 +5,11 @@ namespace Yiisoft\ErrorHandler\Tests\Middleware; use DomainException; +use HttpSoft\Message\Request; use HttpSoft\Message\Response; use HttpSoft\Message\ResponseFactory; use HttpSoft\Message\ResponseTrait; +use HttpSoft\Message\ServerRequest; use HttpSoft\Message\ServerRequestFactory; use InvalidArgumentException; use LogicException; @@ -41,18 +43,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 From e4bc76e754f675b7bc25c89eabb1a188b66e71ce Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 5 Sep 2025 09:33:24 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- tests/Middleware/ExceptionResponderTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Middleware/ExceptionResponderTest.php b/tests/Middleware/ExceptionResponderTest.php index 27645c8..b669052 100644 --- a/tests/Middleware/ExceptionResponderTest.php +++ b/tests/Middleware/ExceptionResponderTest.php @@ -5,7 +5,6 @@ namespace Yiisoft\ErrorHandler\Tests\Middleware; use DomainException; -use HttpSoft\Message\Request; use HttpSoft\Message\Response; use HttpSoft\Message\ResponseFactory; use HttpSoft\Message\ResponseTrait;