1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nofw\Error; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Psr\Log\LogLevel; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This handler can be used to log errors using a PSR-3 logger. |
12
|
|
|
* |
13
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class Psr3ErrorHandler implements ErrorHandler |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Defines which error levels should be mapped to certain error types by default. |
19
|
|
|
*/ |
20
|
|
|
private const DEFAULT_ERROR_LEVEL_MAP = [ |
21
|
|
|
\ParseError::class => LogLevel::CRITICAL, |
22
|
|
|
\Throwable::class => LogLevel::ERROR, |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The key under which the error should be passed to the log context. |
27
|
|
|
*/ |
28
|
|
|
public const ERROR_KEY = 'error'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var LoggerInterface |
32
|
|
|
*/ |
33
|
|
|
private $logger; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Defines which error levels should be mapped to certain error types. |
37
|
|
|
* |
38
|
|
|
* Note: The errors are checked in order, so if you want to define fallbacks for classes higher in the tree |
39
|
|
|
* make sure to add them to the end of the map. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $levelMap = []; |
44
|
|
|
|
45
|
|
|
public function __construct(LoggerInterface $logger, array $levelMap = []) |
46
|
|
|
{ |
47
|
|
|
$this->logger = $logger; |
48
|
|
|
|
49
|
|
|
// Keep user maintained order |
50
|
|
|
$this->levelMap = array_replace($levelMap, self::DEFAULT_ERROR_LEVEL_MAP, $levelMap); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function handle(\Throwable $t, array $context = []): void |
57
|
|
|
{ |
58
|
|
|
$context[self::ERROR_KEY] = $t; |
59
|
|
|
|
60
|
|
|
$this->logger->log( |
61
|
|
|
$this->getLevel($t), |
62
|
|
|
sprintf( |
63
|
|
|
'%s \'%s\' with message \'%s\' in %s(%s)', |
64
|
|
|
$this->getType($t), |
65
|
|
|
get_class($t), |
66
|
|
|
$t->getMessage(), |
67
|
|
|
$t->getFile(), |
68
|
|
|
$t->getLine() |
69
|
|
|
), |
70
|
|
|
$context |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Determine the level for the error. |
76
|
|
|
*/ |
77
|
|
|
private function getLevel(\Throwable $t): string |
78
|
|
|
{ |
79
|
|
|
$level = LogLevel::ERROR; |
80
|
|
|
foreach ($this->levelMap as $className => $candidate) { |
81
|
|
|
if ($t instanceof $className) { |
82
|
|
|
$level = $candidate; |
83
|
|
|
break; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $level; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Determine the error type. |
92
|
|
|
*/ |
93
|
|
|
private function getType(\Throwable $t): string |
94
|
|
|
{ |
95
|
|
|
if ($t instanceof \Exception) { |
96
|
|
|
return 'Exception'; |
97
|
|
|
} elseif ($t instanceof \Error) { |
|
|
|
|
98
|
|
|
return 'Error'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return 'Throwable'; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.