Skip to content

Commit b89d1a5

Browse files
committed
#121: Make logger in ErrorHandler optional
#144: Mark ErrorException and UserException as final #146: Remove deprecated Yiisoft\ErrorHandler\Factory\ThrowableResponseFactory rename ThrowableResponseFactory to ThrowableResponseAction change parameters order in ThrowableResponseActionInterface
1 parent 73d73a7 commit b89d1a5

12 files changed

+59
-495
lines changed

config/di-web.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@
22

33
declare(strict_types=1);
44

5-
use Yiisoft\ErrorHandler\Factory\ThrowableResponseFactory;
5+
use Psr\Container\ContainerInterface;
6+
use Yiisoft\Definitions\DynamicReference;
7+
use Yiisoft\Definitions\Reference;
8+
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
69
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer;
10+
use Yiisoft\ErrorHandler\RendererProvider\CompositeRendererProvider;
11+
use Yiisoft\ErrorHandler\RendererProvider\ContentTypeRendererProvider;
12+
use Yiisoft\ErrorHandler\RendererProvider\HeadRendererProvider;
713
use Yiisoft\ErrorHandler\ThrowableRendererInterface;
8-
use Yiisoft\ErrorHandler\ThrowableResponseFactoryInterface;
14+
use Yiisoft\ErrorHandler\ThrowableResponseAction;
915

1016
/**
1117
* @var array $params
1218
*/
1319

1420
return [
1521
ThrowableRendererInterface::class => HtmlRenderer::class,
16-
ThrowableResponseFactoryInterface::class => ThrowableResponseFactory::class,
22+
ErrorCatcher::class => [
23+
'__construct()' => [
24+
'throwableResponseAction' => Reference::to(ThrowableResponseAction::class),
25+
],
26+
],
27+
ThrowableResponseAction::class => [
28+
'__construct()' => [
29+
'rendererProvider' => DynamicReference::to(
30+
static fn(ContainerInterface $container) => new CompositeRendererProvider(
31+
new HeadRendererProvider(),
32+
new ContentTypeRendererProvider($container),
33+
)
34+
),
35+
],
36+
],
1737
];

src/ErrorHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ final class ErrorHandler
4141
private bool $initialized = false;
4242

4343
/**
44-
* @param LoggerInterface $logger Logger to write errors to.
4544
* @param ThrowableRendererInterface $defaultRenderer Default throwable renderer.
45+
* @param LoggerInterface|null $logger Logger to write errors to.
4646
* @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for error events.
4747
* @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last.
4848
*/
4949
public function __construct(
50-
private readonly LoggerInterface $logger,
5150
private readonly ThrowableRendererInterface $defaultRenderer,
51+
private readonly ?LoggerInterface $logger = null,
5252
private readonly ?EventDispatcherInterface $eventDispatcher = null,
5353
private readonly int $exitShutdownHandlerDepth = 2
5454
) {
@@ -68,7 +68,7 @@ public function handle(
6868
$renderer ??= $this->defaultRenderer;
6969

7070
try {
71-
$this->logger->error($t->getMessage(), ['throwable' => $t]);
71+
$this->logger?->error($t->getMessage(), ['throwable' => $t]);
7272
return $this->debug ? $renderer->renderVerbose($t, $request) : $renderer->render($t, $request);
7373
} catch (Throwable $t) {
7474
return new ErrorData((string) $t);

src/Exception/ErrorException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @final
2020
*/
21-
class ErrorException extends \ErrorException implements FriendlyExceptionInterface
21+
final class ErrorException extends \ErrorException implements FriendlyExceptionInterface
2222
{
2323
/** @psalm-suppress MissingClassConstType Private constants never change. */
2424
private const ERROR_NAMES = [

src/Exception/UserException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @final
2424
*/
2525
#[Attribute(Attribute::TARGET_CLASS)]
26-
class UserException extends Exception
26+
final class UserException extends Exception
2727
{
2828
public static function isUserException(Throwable $throwable): bool
2929
{

src/Factory/ThrowableResponseFactory.php

Lines changed: 0 additions & 190 deletions
This file was deleted.

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\ThrowableResponseFactoryInterface;
15+
use Yiisoft\ErrorHandler\ThrowableResponseActionInterface;
1616

1717
/**
1818
* `ErrorCatcher` catches all throwables from the next middlewares
19-
* and renders it with a handler that implements the `ThrowableResponseFactoryInterface`.
19+
* and renders it with a handler that implements the `ThrowableResponseActionInterface`.
2020
*/
2121
final class ErrorCatcher implements MiddlewareInterface
2222
{
2323
public function __construct(
24-
private readonly ThrowableResponseFactoryInterface $throwableResponseFactory,
24+
private readonly ThrowableResponseActionInterface $throwableResponseAction,
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->throwableResponseFactory->create($t, $request);
40+
return $this->throwableResponseAction->handle($request, $t);
4141
}
4242
}
4343
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,19 @@
1212
use Yiisoft\Http\Status;
1313

1414
/**
15-
* `ThrowableResponseFactory` produces a response with rendered `Throwable` object.
15+
* `ThrowableResponseAction` produces a response with rendered `Throwable` object.
1616
*/
17-
final class ThrowableResponseFactory implements ThrowableResponseFactoryInterface
17+
final class ThrowableResponseAction implements ThrowableResponseActionInterface
1818
{
19-
private readonly HeadersProvider $headersProvider;
20-
2119
public function __construct(
2220
private readonly ResponseFactoryInterface $responseFactory,
2321
private readonly ErrorHandler $errorHandler,
2422
private readonly RendererProviderInterface $rendererProvider,
25-
?HeadersProvider $headersProvider = null,
23+
private readonly HeadersProvider $headersProvider = new HeadersProvider(),
2624
) {
27-
$this->headersProvider = $headersProvider ?? new HeadersProvider();
2825
}
2926

30-
public function create(Throwable $throwable, ServerRequestInterface $request): ResponseInterface
27+
public function handle(ServerRequestInterface $request, Throwable $throwable): ResponseInterface
3128
{
3229
$renderer = $this->rendererProvider->get($request);
3330

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
use Psr\Http\Message\ServerRequestInterface;
1010

1111
/**
12-
* `ThrowableResponseFactoryInterface` produces a response for `Throwable` object.
12+
* `ThrowableResponseActionInterface` produces a response for `Throwable` object.
1313
*/
14-
interface ThrowableResponseFactoryInterface
14+
interface ThrowableResponseActionInterface
1515
{
1616
/**
1717
* Handles a `Throwable` object and produces a response.
1818
*/
19-
public function create(Throwable $throwable, ServerRequestInterface $request): ResponseInterface;
19+
public function handle(ServerRequestInterface $request, Throwable $throwable): ResponseInterface;
2020
}

tests/ErrorHandlerTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
88
use PHPUnit\Framework\TestCase;
9-
use Psr\Log\LoggerInterface;
109
use RuntimeException;
1110
use Throwable;
1211
use Yiisoft\ErrorHandler\ErrorHandler;
@@ -16,14 +15,12 @@
1615
final class ErrorHandlerTest extends TestCase
1716
{
1817
private ErrorHandler $errorHandler;
19-
private LoggerInterface $loggerMock;
2018
private ThrowableRendererInterface $throwableRendererMock;
2119

2220
protected function setUp(): void
2321
{
24-
$this->loggerMock = $this->createMock(LoggerInterface::class);
2522
$this->throwableRendererMock = $this->createMock(ThrowableRendererInterface::class);
26-
$this->errorHandler = new ErrorHandler($this->loggerMock, $this->throwableRendererMock);
23+
$this->errorHandler = new ErrorHandler($this->throwableRendererMock);
2724
$this->errorHandler->memoryReserveSize(0);
2825
}
2926

0 commit comments

Comments
 (0)