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