1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ErrorHandler\Slim; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\ErrorHandler\ErrorResponseProviderInterface; |
8
|
|
|
use Chubbyphp\ErrorHandler\HttpException; |
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use Psr\Log\NullLogger; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @deprecated use Chubbyphp\ErrorHandler\SimpleErrorHandler |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
final class SimpleErrorHandler implements ErrorHandlerInterface |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ErrorResponseProviderInterface |
21
|
|
|
*/ |
22
|
|
|
private $provider; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var LoggerInterface |
26
|
|
|
*/ |
27
|
|
|
private $logger; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ErrorResponseProviderInterface $provider |
31
|
|
|
*/ |
32
|
2 |
|
public function __construct(ErrorResponseProviderInterface $provider, LoggerInterface $logger = null) |
33
|
|
|
{ |
34
|
2 |
|
$this->provider = $provider; |
35
|
2 |
|
$this->logger = $logger ?? new NullLogger(); |
36
|
2 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Request $request |
40
|
|
|
* @param Response $response |
41
|
|
|
* @param \Exception $exception |
42
|
|
|
* |
43
|
|
|
* @return Response |
44
|
|
|
*/ |
45
|
2 |
|
public function __invoke(Request $request, Response $response, \Exception $exception): Response |
46
|
|
|
{ |
47
|
2 |
|
$this->logException($exception); |
48
|
|
|
|
49
|
2 |
|
return $this->provider->get($request, $response, $exception); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param \Exception $exception |
54
|
|
|
*/ |
55
|
2 |
|
private function logException(\Exception $exception) |
56
|
|
|
{ |
57
|
2 |
|
if ($exception instanceof HttpException) { |
58
|
1 |
|
$this->logger->warning( |
59
|
1 |
|
'error-handler: {code} {message}', |
60
|
1 |
|
['status' => $exception->getCode(), 'message' => $exception->getMessage()] |
61
|
|
|
); |
62
|
|
|
|
63
|
1 |
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$this->logger->error( |
67
|
1 |
|
'error-handler: {code} {message}', |
68
|
1 |
|
['status' => 500, 'message' => $exception->getMessage()] |
69
|
|
|
); |
70
|
1 |
|
} |
71
|
|
|
} |
72
|
|
|
|
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.