1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PeekAndPoke\Component\Toolbox; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Karsten J. Gerber <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @codeCoverageIgnore |
11
|
|
|
*/ |
12
|
|
|
class ExceptionUtil |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param \Throwable $e |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public static function toString(\Throwable $e) |
20
|
|
|
{ |
21
|
|
|
// use internal exception printing |
22
|
|
|
ob_start(); |
23
|
|
|
echo (string) $e; |
24
|
|
|
|
25
|
|
|
return ob_get_clean(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \Throwable $e |
30
|
|
|
* |
31
|
|
|
* @return string[] |
32
|
|
|
*/ |
33
|
|
|
public static function toLines(\Throwable $e) |
34
|
|
|
{ |
35
|
|
|
return explode("\n", self::toString($e)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param LoggerInterface $logger |
40
|
|
|
* @param \Throwable $e |
41
|
|
|
* @param string $prefix |
42
|
|
|
* @param array $extra |
43
|
|
|
*/ |
44
|
|
|
public static function log(LoggerInterface $logger, \Throwable $e, $prefix, array $extra = []) |
45
|
|
|
{ |
46
|
|
|
$result = static::toString($e); |
47
|
|
|
|
48
|
|
|
$extra['exception'] = $result; |
49
|
|
|
|
50
|
|
|
if ($e instanceof \ErrorException) { |
51
|
|
|
$logger->alert($prefix . ' ' . static::formatMessage($e), $extra); |
52
|
|
|
} else { |
53
|
|
|
$logger->error($prefix . ' ' . static::formatMessage($e), $extra); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param LoggerInterface $logger |
59
|
|
|
* @param \Throwable $e |
60
|
|
|
* @param string $prefix |
61
|
|
|
* @param array $extra |
62
|
|
|
*/ |
63
|
|
|
public static function warn(LoggerInterface $logger, \Throwable $e, $prefix, array $extra = []) |
64
|
|
|
{ |
65
|
|
|
$result = static::toString($e); |
66
|
|
|
|
67
|
|
|
$extra['exception'] = $result; |
68
|
|
|
|
69
|
|
|
$logger->warning($prefix . ' ' . static::formatMessage($e), $extra); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param LoggerInterface $logger |
74
|
|
|
* @param \Throwable $e |
75
|
|
|
* @param string $prefix |
76
|
|
|
* @param array $extra |
77
|
|
|
*/ |
78
|
|
|
public static function info(LoggerInterface $logger, \Throwable $e, $prefix, array $extra = []) |
79
|
|
|
{ |
80
|
|
|
$result = static::toString($e); |
81
|
|
|
|
82
|
|
|
$extra['exception'] = $result; |
83
|
|
|
|
84
|
|
|
$logger->info($prefix . ' ' . static::formatMessage($e), $extra); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param \Throwable $e |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public static function toRaw(\Throwable $e) |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
'raw' => explode(PHP_EOL, static::toString($e)), |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param \Throwable $e |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public static function toReducedArray(\Throwable $e) |
105
|
|
|
{ |
106
|
|
|
$originalException = $e; |
107
|
|
|
|
108
|
|
|
$renderedTrace = []; |
109
|
|
|
|
110
|
|
|
while ($e) { |
111
|
|
|
|
112
|
|
|
$trace = $e->getTrace(); |
113
|
|
|
$currentRenderedTrace = []; |
114
|
|
|
|
115
|
|
|
$idx = 0; |
116
|
|
|
|
117
|
|
|
foreach ($trace as $traceItem) { |
118
|
|
|
|
119
|
|
|
$currentRenderedTrace['#' . $idx++] = |
120
|
|
|
($traceItem['file'] ?? '') . '(' . |
121
|
|
|
($traceItem['line'] ?? '') . ') ' . |
122
|
|
|
($traceItem['class'] ?? '') . |
123
|
|
|
($traceItem['type'] ?? '') . |
124
|
|
|
($traceItem['function'] ?? ''); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$renderedTrace[] = [ |
128
|
|
|
'ex' => \get_class($e), |
129
|
|
|
'message' => static::formatMessage($e), |
130
|
|
|
'trace' => $currentRenderedTrace, |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$e = $e->getPrevious(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$json = [ |
137
|
|
|
'ex' => \get_class($originalException), |
138
|
|
|
'message' => static::formatMessage($originalException), |
139
|
|
|
'trace' => $renderedTrace, |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
return $json; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param \Throwable $e |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public static function formatMessage(\Throwable $e) |
151
|
|
|
{ |
152
|
|
|
return $e->getMessage() . ' (Code ' . $e->getCode() . ') at ' . $e->getFile() . ':' . $e->getLine(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|