1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ErrorHandler; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
8
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Psr\Log\NullLogger; |
11
|
|
|
|
12
|
|
View Code Duplication |
final class AdvancedErrorHandler implements ErrorHandlerInterface |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ContentTypeResolverInterface |
16
|
|
|
*/ |
17
|
|
|
private $contentTypeResolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ErrorResponseProviderInterface |
21
|
|
|
*/ |
22
|
|
|
private $fallbackProvider; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ErrorResponseProviderInterface[] |
26
|
|
|
*/ |
27
|
|
|
private $providers = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var LoggerInterface |
31
|
|
|
*/ |
32
|
|
|
private $logger; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ContentTypeResolverInterface $contentTypeResolver |
36
|
|
|
* @param ErrorResponseProviderInterface $fallbackProvider |
37
|
|
|
* @param array $providers |
38
|
|
|
* @param LoggerInterface|null $logger |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
ContentTypeResolverInterface $contentTypeResolver, |
42
|
|
|
ErrorResponseProviderInterface $fallbackProvider, |
43
|
|
|
array $providers = [], |
44
|
|
|
LoggerInterface $logger = null |
45
|
|
|
) { |
46
|
|
|
$this->contentTypeResolver = $contentTypeResolver; |
47
|
|
|
$this->fallbackProvider = $fallbackProvider; |
48
|
|
|
$this->addProvider($fallbackProvider); |
49
|
|
|
foreach ($providers as $provider) { |
50
|
|
|
$this->addProvider($provider); |
51
|
|
|
} |
52
|
|
|
$this->logger = $logger ?? new NullLogger(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ErrorResponseProviderInterface $provider |
57
|
|
|
*/ |
58
|
|
|
private function addProvider(ErrorResponseProviderInterface $provider) |
59
|
|
|
{ |
60
|
|
|
$this->providers[$provider->getContentType()] = $provider; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Request $request |
65
|
|
|
* @param Response $response |
66
|
|
|
* @param \Exception $exception |
67
|
|
|
* |
68
|
|
|
* @return Response |
69
|
|
|
* |
70
|
|
|
* @throws \LogicException |
71
|
|
|
*/ |
72
|
|
|
public function __invoke(Request $request, Response $response, \Exception $exception): Response |
73
|
|
|
{ |
74
|
|
|
$contentType = $this->contentTypeResolver->getContentType($request, array_keys($this->providers)); |
75
|
|
|
|
76
|
|
|
$this->logException($exception); |
77
|
|
|
|
78
|
|
|
if (isset($this->providers[$contentType])) { |
79
|
|
|
return $this->providers[$contentType]->get($request, $response, $exception); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->fallbackProvider->get($request, $response, $exception); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \Exception $exception |
87
|
|
|
*/ |
88
|
|
|
private function logException(\Exception $exception) |
89
|
|
|
{ |
90
|
|
|
if ($exception instanceof HttpException) { |
91
|
|
|
$this->logger->warning( |
92
|
|
|
'error-handler: {code} {message}', |
93
|
|
|
['status' => $exception->getCode(), 'message' => $exception->getMessage()] |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->logger->error( |
100
|
|
|
'error-handler: {code} {message}', |
101
|
|
|
['status' => 500, 'message' => $exception->getMessage()] |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.