@@ 5-150 (lines=146) @@ | ||
2 | ||
3 | namespace Dazzle\Throwable; |
|
4 | ||
5 | class Error extends \Error |
|
6 | { |
|
7 | /** |
|
8 | * @var mixed[] |
|
9 | */ |
|
10 | protected $context = []; |
|
11 | ||
12 | /** |
|
13 | * @param string $message |
|
14 | * @param int $code |
|
15 | * @param \Error|\Exception|null $previous |
|
16 | */ |
|
17 | public function __construct($message = 'Unknown error', $code = 0, $previous = null) |
|
18 | { |
|
19 | parent::__construct($message, $code, $previous); |
|
20 | } |
|
21 | ||
22 | /** |
|
23 | * @param mixed[] $context |
|
24 | */ |
|
25 | public function setContext(array $context = []) |
|
26 | { |
|
27 | $this->context = $context; |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * @return mixed[] |
|
32 | */ |
|
33 | public function getContext() |
|
34 | { |
|
35 | return $this->context; |
|
36 | } |
|
37 | ||
38 | /** |
|
39 | * @return string |
|
40 | */ |
|
41 | public function __toString() |
|
42 | { |
|
43 | return static::toString($this); |
|
44 | } |
|
45 | ||
46 | /** |
|
47 | * Return Error full trace in string format. |
|
48 | * |
|
49 | * @param \Error|\Exception $ex |
|
50 | * @return string |
|
51 | */ |
|
52 | public static function toString($ex) |
|
53 | { |
|
54 | return implode("\n", [ |
|
55 | "\t" . 'Throwable trace:', |
|
56 | static::toThrowableString($ex), |
|
57 | "\t" . 'Stack trace:', |
|
58 | static::toStackString($ex) |
|
59 | ]); |
|
60 | } |
|
61 | ||
62 | /** |
|
63 | * Return Error full trace in array format. |
|
64 | * |
|
65 | * @param \Error|\Exception $ex |
|
66 | * @return mixed |
|
67 | */ |
|
68 | public static function toTrace($ex) |
|
69 | { |
|
70 | return Throwable::getThrowableStack($ex); |
|
71 | } |
|
72 | ||
73 | /** |
|
74 | * Return Error stack trace in array format. |
|
75 | * |
|
76 | * @param \Error|\Exception $ex |
|
77 | * @return string[] |
|
78 | */ |
|
79 | public static function toStackTrace($ex) |
|
80 | { |
|
81 | $list = []; |
|
82 | for ($stack = Throwable::getThrowableStack($ex); $stack !== null; $stack = $stack['prev']) |
|
83 | { |
|
84 | $list = array_merge($stack['trace'], $list); |
|
85 | } |
|
86 | ||
87 | return $list; |
|
88 | } |
|
89 | ||
90 | /** |
|
91 | * Return Error throwable trace in array format. |
|
92 | * |
|
93 | * @param \Error|\Exception $ex |
|
94 | * @return string[] |
|
95 | */ |
|
96 | public static function toThrowableTrace($ex) |
|
97 | { |
|
98 | $list = []; |
|
99 | for ($stack = Throwable::getThrowableStack($ex); $stack !== null; $stack = $stack['prev']) |
|
100 | { |
|
101 | $list[] = Throwable::parseThrowableMessage($stack); |
|
102 | } |
|
103 | ||
104 | return array_reverse($list); |
|
105 | } |
|
106 | ||
107 | /** |
|
108 | * Return Error stack trace in string format. |
|
109 | * |
|
110 | * @param \Error|\Exception $ex |
|
111 | * @return string |
|
112 | */ |
|
113 | public static function toStackString($ex) |
|
114 | { |
|
115 | $stack = []; |
|
116 | $i = 0; |
|
117 | $trace = static::toStackTrace($ex); |
|
118 | $pad = strlen(count($trace)) > 2 ?: 2; |
|
119 | ||
120 | foreach ($trace as $element) |
|
121 | { |
|
122 | $stack[] = "\t" . str_pad('' . $i, $pad, ' ', STR_PAD_LEFT) . '. ' . $element; |
|
123 | ++$i; |
|
124 | } |
|
125 | ||
126 | return implode("\n", $stack); |
|
127 | } |
|
128 | ||
129 | /** |
|
130 | * Return Error throwable trace in string format. |
|
131 | * |
|
132 | * @param \Error|\Exception $ex |
|
133 | * @return string |
|
134 | */ |
|
135 | public static function toThrowableString($ex) |
|
136 | { |
|
137 | $stack = []; |
|
138 | $i = 0; |
|
139 | $trace = static::toThrowableTrace($ex); |
|
140 | $pad = strlen(count($trace)) > 2 ?: 2; |
|
141 | ||
142 | foreach ($trace as $element) |
|
143 | { |
|
144 | $stack[] = "\t" . str_pad('' . $i, $pad, ' ', STR_PAD_LEFT) . '. ' . $element; |
|
145 | ++$i; |
|
146 | } |
|
147 | ||
148 | return implode("\n", $stack); |
|
149 | } |
|
150 | } |
|
151 |
@@ 5-150 (lines=146) @@ | ||
2 | ||
3 | namespace Dazzle\Throwable; |
|
4 | ||
5 | class Exception extends \Exception |
|
6 | { |
|
7 | /** |
|
8 | * @var mixed[] |
|
9 | */ |
|
10 | protected $context = []; |
|
11 | ||
12 | /** |
|
13 | * @param string $message |
|
14 | * @param int $code |
|
15 | * @param \Error|\Exception|null $previous |
|
16 | */ |
|
17 | public function __construct($message = 'Unknown exception', $code = 0, $previous = null) |
|
18 | { |
|
19 | parent::__construct($message, $code, $previous); |
|
20 | } |
|
21 | ||
22 | /** |
|
23 | * @param mixed[] $context |
|
24 | */ |
|
25 | public function setContext(array $context = []) |
|
26 | { |
|
27 | $this->context = $context; |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * @return mixed[] |
|
32 | */ |
|
33 | public function getContext() |
|
34 | { |
|
35 | return $this->context; |
|
36 | } |
|
37 | ||
38 | /** |
|
39 | * @return string |
|
40 | */ |
|
41 | public function __toString() |
|
42 | { |
|
43 | return static::toString($this); |
|
44 | } |
|
45 | ||
46 | /** |
|
47 | * Return Exception full trace in string format. |
|
48 | * |
|
49 | * @param \Error|\Exception $ex |
|
50 | * @return string |
|
51 | */ |
|
52 | public static function toString($ex) |
|
53 | { |
|
54 | return implode("\n", [ |
|
55 | "\t" . 'Throwable trace:', |
|
56 | static::toThrowableString($ex), |
|
57 | "\t" . 'Stack trace:', |
|
58 | static::toStackString($ex) |
|
59 | ]); |
|
60 | } |
|
61 | ||
62 | /** |
|
63 | * Return Exception full trace in array format. |
|
64 | * |
|
65 | * @param \Error|\Exception $ex |
|
66 | * @return mixed |
|
67 | */ |
|
68 | public static function toTrace($ex) |
|
69 | { |
|
70 | return Throwable::getThrowableStack($ex); |
|
71 | } |
|
72 | ||
73 | /** |
|
74 | * Return Exception stack trace in array format. |
|
75 | * |
|
76 | * @param \Error|\Exception $ex |
|
77 | * @return string[] |
|
78 | */ |
|
79 | public static function toStackTrace($ex) |
|
80 | { |
|
81 | $list = []; |
|
82 | for ($stack = Throwable::getThrowableStack($ex); $stack !== null; $stack = $stack['prev']) |
|
83 | { |
|
84 | $list = array_merge($stack['trace'], $list); |
|
85 | } |
|
86 | ||
87 | return $list; |
|
88 | } |
|
89 | ||
90 | /** |
|
91 | * Return Exception throwable trace in array format. |
|
92 | * |
|
93 | * @param \Error|\Exception $ex |
|
94 | * @return string[] |
|
95 | */ |
|
96 | public static function toThrowableTrace($ex) |
|
97 | { |
|
98 | $list = []; |
|
99 | for ($stack = Throwable::getThrowableStack($ex); $stack !== null; $stack = $stack['prev']) |
|
100 | { |
|
101 | $list[] = Throwable::parseThrowableMessage($stack); |
|
102 | } |
|
103 | ||
104 | return array_reverse($list); |
|
105 | } |
|
106 | ||
107 | /** |
|
108 | * Return Exception stack trace in string format. |
|
109 | * |
|
110 | * @param \Error|\Exception $ex |
|
111 | * @return string |
|
112 | */ |
|
113 | public static function toStackString($ex) |
|
114 | { |
|
115 | $stack = []; |
|
116 | $i = 0; |
|
117 | $trace = static::toStackTrace($ex); |
|
118 | $pad = strlen(count($trace)) > 2 ?: 2; |
|
119 | ||
120 | foreach ($trace as $element) |
|
121 | { |
|
122 | $stack[] = "\t" . str_pad('' . $i, $pad, ' ', STR_PAD_LEFT) . '. ' . $element; |
|
123 | ++$i; |
|
124 | } |
|
125 | ||
126 | return implode("\n", $stack); |
|
127 | } |
|
128 | ||
129 | /** |
|
130 | * Return Exception throwable trace in string format. |
|
131 | * |
|
132 | * @param \Error|\Exception $ex |
|
133 | * @return string |
|
134 | */ |
|
135 | public static function toThrowableString($ex) |
|
136 | { |
|
137 | $stack = []; |
|
138 | $i = 0; |
|
139 | $trace = static::toThrowableTrace($ex); |
|
140 | $pad = strlen(count($trace)) > 2 ?: 2; |
|
141 | ||
142 | foreach ($trace as $element) |
|
143 | { |
|
144 | $stack[] = "\t" . str_pad('' . $i, $pad, ' ', STR_PAD_LEFT) . '. ' . $element; |
|
145 | ++$i; |
|
146 | } |
|
147 | ||
148 | return implode("\n", $stack); |
|
149 | } |
|
150 | } |
|
151 |