|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Exceptions; |
|
13
|
|
|
|
|
14
|
|
|
use Spiral\Exceptions\Style\PlainStyle; |
|
15
|
|
|
|
|
16
|
|
|
final class PlainHandler extends AbstractHandler |
|
17
|
|
|
{ |
|
18
|
|
|
// Lines to show around targeted line. |
|
19
|
|
|
private const SHOW_LINES = 2; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritdoc |
|
23
|
|
|
*/ |
|
24
|
|
|
public function renderException(\Throwable $e, int $verbosity = self::VERBOSITY_BASIC): string |
|
25
|
|
|
{ |
|
26
|
|
|
$result = ''; |
|
27
|
|
|
|
|
28
|
|
|
if ($e instanceof \Error) { |
|
29
|
|
|
$result .= '[' . get_class($e) . "]\n" . $e->getMessage(); |
|
30
|
|
|
} else { |
|
31
|
|
|
$result .= '[' . get_class($e) . "]\n" . $e->getMessage(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$result .= sprintf("in %s:%s\n", $e->getFile(), $e->getLine()); |
|
35
|
|
|
|
|
36
|
|
|
if ($verbosity >= self::VERBOSITY_DEBUG) { |
|
37
|
|
|
$result .= $this->renderTrace($e, new Highlighter(new PlainStyle())); |
|
38
|
|
|
} elseif ($verbosity >= self::VERBOSITY_VERBOSE) { |
|
39
|
|
|
$result .= $this->renderTrace($e); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $result; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Render exception call stack. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \Throwable $e |
|
49
|
|
|
* @param Highlighter|null $h |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
private function renderTrace(\Throwable $e, Highlighter $h = null): string |
|
53
|
|
|
{ |
|
54
|
|
|
$stacktrace = $this->getStacktrace($e); |
|
55
|
|
|
if (empty($stacktrace)) { |
|
56
|
|
|
return ''; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$result = "\nException Trace:\n"; |
|
60
|
|
|
|
|
61
|
|
|
foreach ($stacktrace as $trace) { |
|
62
|
|
|
if (isset($trace['type']) && isset($trace['class'])) { |
|
63
|
|
|
$line = sprintf( |
|
64
|
|
|
' %s%s%s()', |
|
65
|
|
|
$trace['class'], |
|
66
|
|
|
$trace['type'], |
|
67
|
|
|
$trace['function'] |
|
68
|
|
|
); |
|
69
|
|
|
} else { |
|
70
|
|
|
$line = $trace['function']; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (isset($trace['file'])) { |
|
74
|
|
|
$line .= sprintf(' at %s:%s', $trace['file'], $trace['line']); |
|
75
|
|
|
} else { |
|
76
|
|
|
$line .= sprintf(' at %s:%s', 'n/a', 'n/a'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$result .= $line . "\n"; |
|
80
|
|
|
|
|
81
|
|
|
if (!empty($h) && !empty($trace['file'])) { |
|
82
|
|
|
$result .= $h->highlightLines( |
|
83
|
|
|
file_get_contents($trace['file']), |
|
84
|
|
|
$trace['line'], |
|
85
|
|
|
static::SHOW_LINES |
|
86
|
|
|
) . "\n"; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $result; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|