Skip to content

Commit 72467ba

Browse files
authored
Merge pull request #75 from yiisoft/add-error-dispatching
Add error event dispatching
2 parents 7ebbf4e + 17341e4 commit 72467ba

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Yii Error Handler Change Log
22

3-
## 3.0.1 under development
3+
## 3.1.0 under development
44

5+
- Chg #75: Dispatch `ApplicationError` in `ErrorCatcher` (@xepozz)
56
- Enh #82: Add `HeadersProvider` (@xepozz)
67

78
## 3.0.0 February 14, 2023

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,22 +198,26 @@ In the application middleware stack `Yiisoft\ErrorHandler\Middleware\ExceptionRe
198198
For use in the [Yii framework](https://www.yiiframework.com/),
199199
see [Yii guide to handling errors](https://github.com/yiisoft/docs/blob/master/guide/en/runtime/handling-errors.md).
200200

201+
## Events
202+
203+
- When `ErrorCatcher` catches an error it dispatches `\Yiisoft\ErrorHandler\Event\ApplicationError` event.
204+
201205
## Friendly Exceptions
202206

203207
`HtmlRenderer` supports [friendly exceptions](https://github.com/yiisoft/friendly-exception/).
204208

205209
Code blocks in solution markdown support language syntax highlight:
206210

207-
| Language | Aliases |
208-
| -------- | ------- |
209-
| Bash | bash, sh, zsh |
210-
| CSS | css |
211-
| HTML, XML | xml, html, xhtml, rss, atom, xjb, xsd, xsl, plist, svg |
212-
| JavaScript | javascript, js, jsx |
213-
| JSON | json |
214-
| PHP | php |
215-
| Plaintext | plaintext, txt, text |
216-
| SQL | sql |
211+
| Language | Aliases |
212+
|------------|--------------------------------------------------------|
213+
| Bash | bash, sh, zsh |
214+
| CSS | css |
215+
| HTML, XML | xml, html, xhtml, rss, atom, xjb, xsd, xsl, plist, svg |
216+
| JavaScript | javascript, js, jsx |
217+
| JSON | json |
218+
| PHP | php |
219+
| Plaintext | plaintext, txt, text |
220+
| SQL | sql |
217221

218222
For example:
219223

src/ErrorHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,7 @@ private function renderThrowableAndTerminate(Throwable $t): void
197197
http_response_code(Status::INTERNAL_SERVER_ERROR);
198198

199199
echo $this->handle($t);
200-
if ($this->eventDispatcher !== null) {
201-
$this->eventDispatcher->dispatch(new ApplicationError($t));
202-
}
200+
$this->eventDispatcher?->dispatch(new ApplicationError($t));
203201

204202
register_shutdown_function(static function (): void {
205203
exit(1);

src/Middleware/ErrorCatcher.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
use InvalidArgumentException;
88
use Psr\Container\ContainerInterface;
9+
use Psr\EventDispatcher\EventDispatcherInterface;
910
use Psr\Http\Message\ResponseFactoryInterface;
1011
use Psr\Http\Message\ResponseInterface;
1112
use Psr\Http\Message\ServerRequestInterface;
1213
use Psr\Http\Server\MiddlewareInterface;
1314
use Psr\Http\Server\RequestHandlerInterface;
1415
use Throwable;
16+
use Yiisoft\ErrorHandler\Event\ApplicationError;
1517
use Yiisoft\ErrorHandler\ErrorHandler;
1618
use Yiisoft\ErrorHandler\HeadersProvider;
1719
use Yiisoft\ErrorHandler\Renderer\HeaderRenderer;
@@ -57,6 +59,7 @@ public function __construct(
5759
private ResponseFactoryInterface $responseFactory,
5860
private ErrorHandler $errorHandler,
5961
private ContainerInterface $container,
62+
private ?EventDispatcherInterface $eventDispatcher = null,
6063
HeadersProvider $headersProvider = null,
6164
) {
6265
$this->headersProvider = $headersProvider ?? new HeadersProvider();
@@ -125,9 +128,13 @@ public function forceContentType(string $contentType): self
125128

126129
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
127130
{
131+
$t = null;
128132
try {
129133
return $handler->handle($request);
130134
} catch (Throwable $t) {
135+
$this->eventDispatcher?->dispatch(new ApplicationError($t));
136+
} finally {
137+
/** @psalm-suppress PossiblyNullArgument $t is set in catch() statement */
131138
return $this->generateErrorResponse($t, $request);
132139
}
133140
}

tests/Middleware/ErrorCatcherTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\ErrorHandler\Tests\Middleware;
66

7+
use Psr\EventDispatcher\EventDispatcherInterface;
78
use HttpSoft\Message\ResponseFactory;
89
use HttpSoft\Message\ServerRequest;
910
use InvalidArgumentException;
@@ -62,6 +63,31 @@ public function testProcessWithFailAcceptRequestHeader(): void
6263
$this->assertStringContainsString('<html', $content);
6364
}
6465

66+
public function testProcessWithFailedEventDispatcher(): void
67+
{
68+
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
69+
$eventDispatcher->method('dispatch')->willThrowException(new \RuntimeException('Event dispatcher error'));
70+
$container = new SimpleContainer([], fn (string $className): object => new $className());
71+
$errorCatcher = new ErrorCatcher(
72+
new ResponseFactory(),
73+
$this->createErrorHandler(),
74+
$container,
75+
$eventDispatcher,
76+
);
77+
$response = $errorCatcher->process(
78+
$this->createServerRequest('GET', ['Accept' => ['text/plain;q=2.0']]),
79+
$this->createRequestHandlerWithThrowable(),
80+
);
81+
$response
82+
->getBody()
83+
->rewind();
84+
$content = $response
85+
->getBody()
86+
->getContents();
87+
$this->assertNotSame(PlainTextRenderer::DEFAULT_ERROR_MESSAGE, $content);
88+
$this->assertStringContainsString('<html', $content);
89+
}
90+
6591
public function testAddedRenderer(): void
6692
{
6793
$mimeType = 'test/test';
@@ -240,6 +266,7 @@ private function createErrorCatcher(
240266
new ResponseFactory(),
241267
$this->createErrorHandler(),
242268
$container,
269+
null,
243270
$provider ?? new HeadersProvider()
244271
);
245272
}

0 commit comments

Comments
 (0)