Skip to content

Commit 714e586

Browse files
committed
rename ThrowableResponseFactory
use NullLogger as default logger
1 parent 6f7c6ca commit 714e586

File tree

10 files changed

+60
-41
lines changed

10 files changed

+60
-41
lines changed

CHANGELOG.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
## 5.0 under development
44

5-
- Chg #121: Make logger in ErrorHandler optional (@olegbaturin)
6-
- Chg #144: Mark ErrorException and UserException as final (@olegbaturin)
5+
- Chg #121: Use `NullLogger` as the default logger in the `ErrorHandler` (@olegbaturin)
6+
- Chg #144: Mark `ErrorException` and `UserException` as final (@olegbaturin)
77
- Chg #146: Remove deprecated `Yiisoft\ErrorHandler\Factory\ThrowableResponseFactory` (@olegbaturin)
8-
- Chg #157: Rename `Yiisoft\ErrorHandler\ThrowableResponseFactory` to `Yiisoft\ErrorHandler\ThrowableResponseAction` (@olegbaturin)
9-
- Chg #157: Change parameters order in `ThrowableResponseActionInterface` (@olegbaturin)
8+
- Chg #157: Change parameters order in the `ThrowableResponseActionInterface` (@olegbaturin)
109

1110
## 4.3.1 under development
1211

config/di-web.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,22 @@
44

55
use Psr\Container\ContainerInterface;
66
use Yiisoft\Definitions\DynamicReference;
7-
use Yiisoft\Definitions\Reference;
8-
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
97
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer;
108
use Yiisoft\ErrorHandler\RendererProvider\CompositeRendererProvider;
119
use Yiisoft\ErrorHandler\RendererProvider\ContentTypeRendererProvider;
1210
use Yiisoft\ErrorHandler\RendererProvider\HeadRendererProvider;
1311
use Yiisoft\ErrorHandler\ThrowableRendererInterface;
14-
use Yiisoft\ErrorHandler\ThrowableResponseAction;
12+
use Yiisoft\ErrorHandler\ThrowableResponseFactory;
13+
use Yiisoft\ErrorHandler\ThrowableResponseFactoryInterface;
1514

1615
/**
1716
* @var array $params
1817
*/
1918

2019
return [
2120
ThrowableRendererInterface::class => HtmlRenderer::class,
22-
ErrorCatcher::class => [
23-
'__construct()' => [
24-
'throwableResponseAction' => Reference::to(ThrowableResponseAction::class),
25-
],
26-
],
27-
ThrowableResponseAction::class => [
21+
ThrowableResponseFactoryInterface::class => ThrowableResponseFactory::class,
22+
ThrowableResponseFactory::class => [
2823
'__construct()' => [
2924
'rendererProvider' => DynamicReference::to(
3025
static fn(ContainerInterface $container) => new CompositeRendererProvider(

src/ErrorHandler.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\EventDispatcher\EventDispatcherInterface;
88
use Psr\Http\Message\ServerRequestInterface;
99
use Psr\Log\LoggerInterface;
10+
use Psr\Log\NullLogger;
1011
use Throwable;
1112
use Yiisoft\ErrorHandler\Event\ApplicationError;
1213
use Yiisoft\ErrorHandler\Exception\ErrorException;
@@ -42,13 +43,13 @@ final class ErrorHandler
4243

4344
/**
4445
* @param ThrowableRendererInterface $defaultRenderer Default throwable renderer.
45-
* @param LoggerInterface|null $logger Logger to write errors to.
46+
* @param LoggerInterface $logger Logger to write errors to.
4647
* @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for error events.
4748
* @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last.
4849
*/
4950
public function __construct(
5051
private readonly ThrowableRendererInterface $defaultRenderer,
51-
private readonly ?LoggerInterface $logger = null,
52+
private readonly LoggerInterface $logger = new NullLogger(),
5253
private readonly ?EventDispatcherInterface $eventDispatcher = null,
5354
private readonly int $exitShutdownHandlerDepth = 2
5455
) {
@@ -68,7 +69,7 @@ public function handle(
6869
$renderer ??= $this->defaultRenderer;
6970

7071
try {
71-
$this->logger?->error($t->getMessage(), ['throwable' => $t]);
72+
$this->logger->error($t->getMessage(), ['throwable' => $t]);
7273
return $this->debug ? $renderer->renderVerbose($t, $request) : $renderer->render($t, $request);
7374
} catch (Throwable $t) {
7475
return new ErrorData((string) $t);

src/Middleware/ErrorCatcher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
use Psr\Http\Server\RequestHandlerInterface;
1313
use Yiisoft\ErrorHandler\CompositeException;
1414
use Yiisoft\ErrorHandler\Event\ApplicationError;
15-
use Yiisoft\ErrorHandler\ThrowableResponseActionInterface;
15+
use Yiisoft\ErrorHandler\ThrowableResponseFactoryInterface;
1616

1717
/**
1818
* `ErrorCatcher` catches all throwables from the next middlewares
19-
* and renders it with a handler that implements the `ThrowableResponseActionInterface`.
19+
* and renders it with a handler that implements the `ThrowableResponseFactoryInterface`.
2020
*/
2121
final class ErrorCatcher implements MiddlewareInterface
2222
{
2323
public function __construct(
24-
private readonly ThrowableResponseActionInterface $throwableResponseAction,
24+
private readonly ThrowableResponseFactoryInterface $throwableResponseFactory,
2525
private readonly ?EventDispatcherInterface $eventDispatcher = null,
2626
) {
2727
}
@@ -37,7 +37,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
3737
$t = new CompositeException($e, $t);
3838
}
3939

40-
return $this->throwableResponseAction->handle($request, $t);
40+
return $this->throwableResponseFactory->handle($request, $t);
4141
}
4242
}
4343
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
use Yiisoft\Http\Status;
1313

1414
/**
15-
* `ThrowableResponseAction` produces a response with rendered `Throwable` object.
15+
* `ThrowableResponseFactory` produces a response with rendered `Throwable` object.
1616
*/
17-
final class ThrowableResponseAction implements ThrowableResponseActionInterface
17+
final class ThrowableResponseFactory implements ThrowableResponseFactoryInterface
1818
{
1919
public function __construct(
2020
private readonly ResponseFactoryInterface $responseFactory,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use Psr\Http\Message\ServerRequestInterface;
1010

1111
/**
12-
* `ThrowableResponseActionInterface` produces a response for `Throwable` object.
12+
* `ThrowableResponseFactoryInterface` produces a response for `Throwable` object.
1313
*/
14-
interface ThrowableResponseActionInterface
14+
interface ThrowableResponseFactoryInterface
1515
{
1616
/**
1717
* Handles a `Throwable` object and produces a response.

tests/ConfigTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
namespace Yiisoft\ErrorHandler\Tests;
66

7+
use HttpSoft\Message\ResponseFactory;
78
use PHPUnit\Framework\TestCase;
9+
use Psr\Http\Message\ResponseFactoryInterface;
810
use Yiisoft\Di\Container;
911
use Yiisoft\Di\ContainerConfig;
1012
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer;
1113
use Yiisoft\ErrorHandler\ThrowableRendererInterface;
14+
use Yiisoft\ErrorHandler\ThrowableResponseFactory;
15+
use Yiisoft\ErrorHandler\ThrowableResponseFactoryInterface;
1216

1317
final class ConfigTest extends TestCase
1418
{
@@ -17,16 +21,19 @@ public function testDiWeb(): void
1721
$container = $this->createContainer('web');
1822

1923
$throwableRenderer = $container->get(ThrowableRendererInterface::class);
20-
2124
$this->assertInstanceOf(HtmlRenderer::class, $throwableRenderer);
25+
26+
$throwableResponseFactory = $container->get(ThrowableResponseFactoryInterface::class);
27+
$this->assertInstanceOf(ThrowableResponseFactory::class, $throwableResponseFactory);
2228
}
2329

2430
private function createContainer(?string $postfix = null): Container
2531
{
2632
return new Container(
27-
ContainerConfig::create()->withDefinitions(
28-
$this->getDiConfig($postfix)
29-
)
33+
ContainerConfig::create()->withDefinitions([
34+
ResponseFactoryInterface::class => ResponseFactory::class,
35+
...$this->getDiConfig($postfix)
36+
])
3037
);
3138
}
3239

tests/ErrorHandlerTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
88
use PHPUnit\Framework\TestCase;
9+
use Psr\Log\LoggerInterface;
910
use RuntimeException;
1011
use Throwable;
1112
use Yiisoft\ErrorHandler\ErrorHandler;
@@ -15,17 +16,21 @@
1516
final class ErrorHandlerTest extends TestCase
1617
{
1718
private ErrorHandler $errorHandler;
19+
private LoggerInterface $loggerMock;
1820
private ThrowableRendererInterface $throwableRendererMock;
1921

2022
protected function setUp(): void
2123
{
24+
$this->loggerMock = $this->createMock(LoggerInterface::class);
2225
$this->throwableRendererMock = $this->createMock(ThrowableRendererInterface::class);
23-
$this->errorHandler = new ErrorHandler($this->throwableRendererMock);
26+
$this->errorHandler = new ErrorHandler($this->throwableRendererMock, $this->loggerMock);
2427
$this->errorHandler->memoryReserveSize(0);
2528
}
2629

27-
public function testHandleThrowableCallsDefaultRendererWhenNonePassed(): void
30+
public function testHandleThrowableCallsDefaultRendererWithoutLogger(): void
2831
{
32+
$errorHandler = new ErrorHandler($this->throwableRendererMock);
33+
$errorHandler->memoryReserveSize(0);
2934
$throwable = new RuntimeException();
3035

3136
$this
@@ -34,6 +39,18 @@ public function testHandleThrowableCallsDefaultRendererWhenNonePassed(): void
3439
->method('render')
3540
->with($throwable);
3641

42+
$errorHandler->handle($throwable);
43+
}
44+
45+
public function testHandleThrowableCallsDefaultRendererWhenNonePassed(): void
46+
{
47+
$throwable = new RuntimeException();
48+
49+
$this
50+
->throwableRendererMock
51+
->expects($this->once())
52+
->method('render')
53+
->with($throwable);
3754

3855
$this->errorHandler->handle($throwable);
3956
}

tests/Middleware/ErrorCatcherTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
use RuntimeException;
1515
use Throwable;
1616
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
17-
use Yiisoft\ErrorHandler\ThrowableResponseActionInterface;
17+
use Yiisoft\ErrorHandler\ThrowableResponseFactoryInterface;
1818
use Yiisoft\Http\Status;
1919

2020
final class ErrorCatcherTest extends TestCase
2121
{
2222
public function testSuccess(): void
2323
{
2424
$errorCatcher = new ErrorCatcher(
25-
$this->createThrowableResponseAction(),
25+
$this->createThrowableResponseFactory(),
2626
);
2727
$handler = new class () implements RequestHandlerInterface {
2828
public function handle(ServerRequestInterface $request): ResponseInterface
@@ -41,7 +41,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
4141
public function testError(): void
4242
{
4343
$errorCatcher = new ErrorCatcher(
44-
$this->createThrowableResponseAction(),
44+
$this->createThrowableResponseFactory(),
4545
);
4646
$response = $errorCatcher->process(
4747
new ServerRequest(),
@@ -56,7 +56,7 @@ public function testErrorWithEventDispatcher(): void
5656
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
5757
$eventDispatcher->method('dispatch')->willThrowException(new RuntimeException('Event dispatcher error'));
5858
$errorCatcher = new ErrorCatcher(
59-
$this->createThrowableResponseAction(),
59+
$this->createThrowableResponseFactory(),
6060
$eventDispatcher,
6161
);
6262
$response = $errorCatcher->process(
@@ -66,9 +66,9 @@ public function testErrorWithEventDispatcher(): void
6666
$this->assertSame(Status::INTERNAL_SERVER_ERROR, $response->getStatusCode());
6767
}
6868

69-
private function createThrowableResponseAction(): ThrowableResponseActionInterface
69+
private function createThrowableResponseFactory(): ThrowableResponseFactoryInterface
7070
{
71-
return new class () implements ThrowableResponseActionInterface {
71+
return new class () implements ThrowableResponseFactoryInterface {
7272
public function handle(ServerRequestInterface $request, Throwable $throwable): ResponseInterface
7373
{
7474
return new Response(Status::INTERNAL_SERVER_ERROR);
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44

55
namespace Yiisoft\ErrorHandler\Tests;
66

7-
use HttpSoft\Message\ResponseFactory;
87
use LogicException;
8+
use HttpSoft\Message\ResponseFactory;
99
use PHPUnit\Framework\TestCase;
1010
use Yiisoft\ErrorHandler\ErrorHandler;
1111
use Yiisoft\ErrorHandler\HeadersProvider;
1212
use Yiisoft\ErrorHandler\Renderer\PlainTextRenderer;
1313
use Yiisoft\ErrorHandler\RendererProvider\ContentTypeRendererProvider;
1414
use Yiisoft\ErrorHandler\Tests\Support\TestHelper;
1515
use Yiisoft\ErrorHandler\ThrowableRendererInterface;
16-
use Yiisoft\ErrorHandler\ThrowableResponseAction;
16+
use Yiisoft\ErrorHandler\ThrowableResponseFactory;
1717
use Yiisoft\Test\Support\Container\SimpleContainer;
1818

1919
use function PHPUnit\Framework\assertSame;
2020
use function PHPUnit\Framework\assertTrue;
2121

22-
final class ThrowableResponseActionTest extends TestCase
22+
final class ThrowableResponseFactoryTest extends TestCase
2323
{
2424
public function testBase(): void
2525
{
26-
$action = new ThrowableResponseAction(
26+
$action = new ThrowableResponseFactory(
2727
new ResponseFactory(),
2828
new ErrorHandler(
2929
new PlainTextRenderer(),
@@ -44,7 +44,7 @@ public function testBase(): void
4444

4545
public function testHeaders(): void
4646
{
47-
$action = new ThrowableResponseAction(
47+
$action = new ThrowableResponseFactory(
4848
new ResponseFactory(),
4949
new ErrorHandler(
5050
new PlainTextRenderer(),

0 commit comments

Comments
 (0)