Skip to content

Commit c858f33

Browse files
authored
Replace ExceptionMiddleware to ExceptionResponder (#241)
1 parent 2c442c9 commit c858f33

4 files changed

Lines changed: 54 additions & 42 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"yiisoft/error-handler": "^4.3",
4949
"yiisoft/http": "^1.2",
5050
"yiisoft/hydrator": "^1.6.1",
51+
"yiisoft/injector": "^1.2",
5152
"yiisoft/input-http": "^1.0.1",
5253
"yiisoft/log": "^2.1.1",
5354
"yiisoft/log-target-file": "^3.0",

config/web/di/application.php

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

33
declare(strict_types=1);
44

5-
use App\Http\ExceptionMiddleware;
5+
use App\Http\ExceptionResponderFactory;
66
use App\Http\NotFoundMiddleware;
77
use Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter;
88
use Yiisoft\DataResponse\Formatter\XmlDataResponseFormatter;
@@ -35,7 +35,7 @@
3535
'application/json' => new JsonDataResponseFormatter(),
3636
]),
3737
ErrorCatcher::class,
38-
ExceptionMiddleware::class,
38+
static fn(ExceptionResponderFactory $factory) => $factory->create(),
3939
RequestBodyParser::class,
4040
Router::class,
4141
NotFoundMiddleware::class,

src/Http/ExceptionMiddleware.php

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http;
6+
7+
use Psr\Http\Message\ResponseFactoryInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
use Throwable;
10+
use Yiisoft\ErrorHandler\Exception\UserException;
11+
use Yiisoft\ErrorHandler\Middleware\ExceptionResponder;
12+
use Yiisoft\Injector\Injector;
13+
use Yiisoft\Input\Http\InputValidationException;
14+
15+
final readonly class ExceptionResponderFactory
16+
{
17+
public function __construct(
18+
private ResponseFactoryInterface $responseFactory,
19+
private ApiResponseFactory $apiResponseFactory,
20+
private Injector $injector,
21+
) {}
22+
23+
public function create(): ExceptionResponder
24+
{
25+
return new ExceptionResponder(
26+
[
27+
InputValidationException::class => $this->inputValidationException(...),
28+
Throwable::class => $this->throwable(...),
29+
],
30+
$this->responseFactory,
31+
$this->injector,
32+
);
33+
}
34+
35+
private function inputValidationException(InputValidationException $exception): ResponseInterface
36+
{
37+
return $this->apiResponseFactory->failValidation($exception->getResult());
38+
}
39+
40+
private function throwable(Throwable $exception): ResponseInterface
41+
{
42+
if (UserException::isUserException($exception)) {
43+
$code = $exception->getCode();
44+
return $this->apiResponseFactory->fail(
45+
$exception->getMessage(),
46+
code: is_int($code) ? $code : null,
47+
);
48+
}
49+
throw $exception;
50+
}
51+
}

0 commit comments

Comments
 (0)