Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jan 30, 2024
1 parent 0d87227 commit 6e9c077
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 3.1.1 under development

- New #98: Add ability to perform `getBody()` on response when `ExceptionResponder` middleware is processing (@vjik)
- New #98: Add ability to execute `getBody()` on response when `ExceptionResponder` middleware is processing (@vjik)

## 3.1.0 January 07, 2024

Expand Down
7 changes: 4 additions & 3 deletions src/Middleware/ExceptionResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,22 @@ final class ExceptionResponder implements MiddlewareInterface
* ```
*
* @param callable[]|int[] $exceptionMap A callable that must return a `ResponseInterface` or response status code.
* @param bool $performGetBodyOnResponse Whether performing `getBody()` on response needs to be done.
* @param bool $checkResponseBody Whether executing `getBody()` on response needs to be done. It's useful for
* catching exceptions that can be thrown in the process of body generation.
*/
public function __construct(
private array $exceptionMap,
private ResponseFactoryInterface $responseFactory,
private Injector $injector,
private bool $performGetBodyOnResponse = false,
private bool $checkResponseBody = false,
) {
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$response = $handler->handle($request);
if ($this->performGetBodyOnResponse) {
if ($this->checkResponseBody) {
$response->getBody();
}
} catch (Throwable $t) {
Expand Down
10 changes: 5 additions & 5 deletions tests/Middleware/ExceptionResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public function testAnotherException(): void
$this->process($middleware);
}

public function testPerformGetBodyOnResponse(): void
public function testCheckResponseBody(): void
{
$middleware = $this->createMiddleware(performGetBodyOnResponse: true);
$middleware = $this->createMiddleware(checkResponseBody: true);
$request = (new ServerRequestFactory())->createServerRequest(Method::GET, 'http://example.com');
$handler = new class () implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
Expand All @@ -90,7 +90,7 @@ public function getBody(): StreamInterface

public function testSuccess(): void
{
$middleware = $this->createMiddleware(performGetBodyOnResponse: true);
$middleware = $this->createMiddleware(checkResponseBody: true);
$request = (new ServerRequestFactory())->createServerRequest(Method::GET, 'http://example.com');
$handler = new class () implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
Expand Down Expand Up @@ -119,7 +119,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface

private function createMiddleware(
array $exceptionMap = [],
bool $performGetBodyOnResponse = false,
bool $checkResponseBody = false,
): ExceptionResponder {
return new ExceptionResponder(
$exceptionMap,
Expand All @@ -129,7 +129,7 @@ private function createMiddleware(
ResponseFactoryInterface::class => new ResponseFactory(),
]),
),
$performGetBodyOnResponse,
$checkResponseBody,
);
}
}

0 comments on commit 6e9c077

Please sign in to comment.