Skip to content

Commit 3882b7b

Browse files
authored
Add ability to perform getBody() on response when ExceptionResponder middleware is processing (#98)
1 parent fd84094 commit 3882b7b

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 3.1.1 under development
44

5-
- no changes in this release.
5+
- New #98: Add ability to execute `getBody()` on response when `ExceptionResponder` middleware is processing (@vjik)
66

77
## 3.1.0 January 07, 2024
88

src/Middleware/ExceptionResponder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,24 @@ final class ExceptionResponder implements MiddlewareInterface
5151
* ```
5252
*
5353
* @param callable[]|int[] $exceptionMap A callable that must return a `ResponseInterface` or response status code.
54+
* @param bool $checkResponseBody Whether executing `getBody()` on response needs to be done. It's useful for
55+
* catching exceptions that can be thrown in the process of body generation.
5456
*/
5557
public function __construct(
5658
private array $exceptionMap,
5759
private ResponseFactoryInterface $responseFactory,
5860
private Injector $injector,
61+
private bool $checkResponseBody = false,
5962
) {
6063
}
6164

6265
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
6366
{
6467
try {
65-
return $handler->handle($request);
68+
$response = $handler->handle($request);
69+
if ($this->checkResponseBody) {
70+
$response->getBody();
71+
}
6672
} catch (Throwable $t) {
6773
foreach ($this->exceptionMap as $exceptionType => $responseHandler) {
6874
if ($t instanceof $exceptionType) {
@@ -78,5 +84,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
7884
}
7985
throw $t;
8086
}
87+
88+
return $response;
8189
}
8290
}

tests/Middleware/ExceptionResponderTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
namespace Yiisoft\ErrorHandler\Tests\Middleware;
66

77
use DomainException;
8+
use HttpSoft\Message\Response;
89
use HttpSoft\Message\ResponseFactory;
10+
use HttpSoft\Message\ResponseTrait;
911
use HttpSoft\Message\ServerRequestFactory;
1012
use InvalidArgumentException;
13+
use LogicException;
1114
use PHPUnit\Framework\TestCase;
1215
use Psr\Http\Message\ResponseFactoryInterface;
1316
use Psr\Http\Message\ResponseInterface;
1417
use Psr\Http\Message\ServerRequestInterface;
18+
use Psr\Http\Message\StreamInterface;
1519
use Psr\Http\Server\RequestHandlerInterface;
1620
use Yiisoft\ErrorHandler\Middleware\ExceptionResponder;
1721
use Yiisoft\Http\Method;
@@ -61,6 +65,45 @@ public function testAnotherException(): void
6165
$this->process($middleware);
6266
}
6367

68+
public function testCheckResponseBody(): void
69+
{
70+
$middleware = $this->createMiddleware(checkResponseBody: true);
71+
$request = (new ServerRequestFactory())->createServerRequest(Method::GET, 'http://example.com');
72+
$handler = new class () implements RequestHandlerInterface {
73+
public function handle(ServerRequestInterface $request): ResponseInterface
74+
{
75+
return new class () implements ResponseInterface {
76+
use ResponseTrait;
77+
78+
public function getBody(): StreamInterface
79+
{
80+
throw new LogicException('test');
81+
}
82+
};
83+
}
84+
};
85+
86+
$this->expectException(LogicException::class);
87+
$this->expectExceptionMessage('test');
88+
$middleware->process($request, $handler);
89+
}
90+
91+
public function testSuccess(): void
92+
{
93+
$middleware = $this->createMiddleware(checkResponseBody: true);
94+
$request = (new ServerRequestFactory())->createServerRequest(Method::GET, 'http://example.com');
95+
$handler = new class () implements RequestHandlerInterface {
96+
public function handle(ServerRequestInterface $request): ResponseInterface
97+
{
98+
return new Response();
99+
}
100+
};
101+
102+
$response = $middleware->process($request, $handler);
103+
104+
$this->assertSame(200, $response->getStatusCode());
105+
}
106+
64107
private function process(ExceptionResponder $middleware): ResponseInterface
65108
{
66109
return $middleware->process(
@@ -74,8 +117,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
74117
);
75118
}
76119

77-
private function createMiddleware(array $exceptionMap): ExceptionResponder
78-
{
120+
private function createMiddleware(
121+
array $exceptionMap = [],
122+
bool $checkResponseBody = false,
123+
): ExceptionResponder {
79124
return new ExceptionResponder(
80125
$exceptionMap,
81126
new ResponseFactory(),
@@ -84,6 +129,7 @@ private function createMiddleware(array $exceptionMap): ExceptionResponder
84129
ResponseFactoryInterface::class => new ResponseFactory(),
85130
]),
86131
),
132+
$checkResponseBody,
87133
);
88134
}
89135
}

0 commit comments

Comments
 (0)