-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathError.php
123 lines (109 loc) · 3.01 KB
/
Error.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* This file is part of the ZBateson\MailMimeParser project.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*/
namespace ZBateson\MailMimeParser;
use InvalidArgumentException;
use Psr\Log\LogLevel;
use Throwable;
/**
* Holds information about an error or notice that happened on a specific
* object.
*
* @author Zaahid Bateson
*/
class Error
{
/**
* @var string The error message.
*/
protected string $message;
/**
* @var string The PSR log level for this error.
*/
protected string $psrLevel;
/**
* @var ErrorBag The object the error/notice occurred on.
*/
protected ErrorBag $object;
/**
* @var ?Throwable An Exception object if one happened, or null if not
*/
protected ?Throwable $exception;
/**
* @var array<string, int>
*/
private array $levelMap = [
LogLevel::EMERGENCY => 0,
LogLevel::ALERT => 1,
LogLevel::CRITICAL => 2,
LogLevel::ERROR => 3,
LogLevel::WARNING => 4,
LogLevel::NOTICE => 5,
LogLevel::INFO => 6,
LogLevel::DEBUG => 7,
];
/**
*
* @throws InvalidArgumentException if the passed $psrLogLevelAsErrorLevel
* is not a known PSR log level (see \Psr\Log\LogLevel)
*/
public function __construct(string $message, string $psrLogLevelAsErrorLevel, ErrorBag $object, ?Throwable $exception = null)
{
if (!isset($this->levelMap[$psrLogLevelAsErrorLevel])) {
throw new InvalidArgumentException($psrLogLevelAsErrorLevel . ' is not a known PSR Log Level');
}
$this->message = $message;
$this->psrLevel = $psrLogLevelAsErrorLevel;
$this->object = $object;
$this->exception = $exception;
}
/**
* Returns the error message.
*/
public function getMessage() : string
{
return $this->message;
}
/**
* Returns the PSR string log level for this error message.
*/
public function getPsrLevel() : string
{
return $this->psrLevel;
}
/**
* Returns the class type the error occurred on.
*/
public function getClass() : string
{
return \get_class($this->object);
}
/**
* Returns the object the error occurred on.
*/
public function getObject() : ErrorBag
{
return $this->object;
}
/**
* Returns the exception that occurred, if any, or null.
*/
public function getException() : ?Throwable
{
return $this->exception;
}
/**
* Returns true if the PSR log level for this error is equal to or greater
* than the one passed, e.g. passing LogLevel::ERROR would return true for
* LogLevel::ERROR and LogLevel::CRITICAL, ALERT and EMERGENCY.
*/
public function isPsrLevelGreaterOrEqualTo(string $minLevel) : bool
{
$minIntLevel = $this->levelMap[$minLevel] ?? 1000;
$thisLevel = $this->levelMap[$this->psrLevel];
return ($minIntLevel >= $thisLevel);
}
}