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
|
|
|
/** |
16
|
|
|
* @deprecated use Chubbyphp\ErrorHandler\AdvancedErrorHandler |
17
|
|
|
*/ |
18
|
|
View Code Duplication |
final class AdvancedErrorHandler implements ErrorHandlerInterface |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ContentTypeResolverInterface |
22
|
|
|
*/ |
23
|
|
|
private $contentTypeResolver; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ErrorResponseProviderInterface |
27
|
|
|
*/ |
28
|
|
|
private $fallbackProvider; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ErrorResponseProviderInterface[] |
32
|
|
|
*/ |
33
|
|
|
private $providers = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var LoggerInterface |
37
|
|
|
*/ |
38
|
|
|
private $logger; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ContentTypeResolverInterface $contentTypeResolver |
42
|
|
|
* @param ErrorResponseProviderInterface $fallbackProvider |
43
|
|
|
* @param array $providers |
44
|
|
|
* @param LoggerInterface|null $logger |
45
|
|
|
*/ |
46
|
2 |
|
public function __construct( |
47
|
|
|
ContentTypeResolverInterface $contentTypeResolver, |
48
|
|
|
ErrorResponseProviderInterface $fallbackProvider, |
49
|
|
|
array $providers = [], |
50
|
|
|
LoggerInterface $logger = null |
51
|
|
|
) { |
52
|
2 |
|
$this->contentTypeResolver = $contentTypeResolver; |
53
|
2 |
|
$this->fallbackProvider = $fallbackProvider; |
54
|
2 |
|
$this->addProvider($fallbackProvider); |
55
|
2 |
|
foreach ($providers as $provider) { |
56
|
2 |
|
$this->addProvider($provider); |
57
|
|
|
} |
58
|
2 |
|
$this->logger = $logger ?? new NullLogger(); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param ErrorResponseProviderInterface $provider |
63
|
|
|
*/ |
64
|
2 |
|
private function addProvider(ErrorResponseProviderInterface $provider) |
65
|
|
|
{ |
66
|
2 |
|
$this->providers[$provider->getContentType()] = $provider; |
67
|
2 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Request $request |
71
|
|
|
* @param Response $response |
72
|
|
|
* @param \Exception $exception |
73
|
|
|
* |
74
|
|
|
* @return Response |
75
|
|
|
* |
76
|
|
|
* @throws \LogicException |
77
|
|
|
*/ |
78
|
2 |
|
public function __invoke(Request $request, Response $response, \Exception $exception): Response |
79
|
|
|
{ |
80
|
2 |
|
$contentType = $this->contentTypeResolver->getContentType($request, array_keys($this->providers)); |
81
|
|
|
|
82
|
2 |
|
$this->logException($exception); |
83
|
|
|
|
84
|
2 |
|
if (isset($this->providers[$contentType])) { |
85
|
1 |
|
return $this->providers[$contentType]->get($request, $response, $exception); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return $this->fallbackProvider->get($request, $response, $exception); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \Exception $exception |
93
|
|
|
*/ |
94
|
2 |
|
private function logException(\Exception $exception) |
95
|
|
|
{ |
96
|
2 |
|
if ($exception instanceof HttpException) { |
97
|
1 |
|
$this->logger->warning( |
98
|
1 |
|
'error-handler: {code} {message}', |
99
|
1 |
|
['status' => $exception->getCode(), 'message' => $exception->getMessage()] |
100
|
|
|
); |
101
|
|
|
|
102
|
1 |
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$this->logger->error( |
106
|
1 |
|
'error-handler: {code} {message}', |
107
|
1 |
|
['status' => 500, 'message' => $exception->getMessage()] |
108
|
|
|
); |
109
|
1 |
|
} |
110
|
|
|
} |
111
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.