|
9 | 9 |
|
10 | 10 | namespace Toolkit\Sys\Util;
|
11 | 11 |
|
12 |
| -use Throwable; |
13 |
| -use function get_class; |
14 |
| -use function json_encode; |
15 |
| -use function strip_tags; |
16 |
| - |
17 | 12 | /**
|
18 | 13 | * Class PhpException
|
19 | 14 | *
|
20 | 15 | * @package Toolkit\Sys\Util
|
| 16 | + * @deprecated |
21 | 17 | */
|
22 |
| -class PhpException |
| 18 | +class PhpException extends \Toolkit\Stdlib\Util\PhpException |
23 | 19 | {
|
24 |
| - /** |
25 |
| - * @param Throwable $e |
26 |
| - * @param bool $getTrace |
27 |
| - * @param null $catcher |
28 |
| - * |
29 |
| - * @return string |
30 |
| - * @see PhpException::toHtml() |
31 |
| - */ |
32 |
| - public static function toText(Throwable $e, bool $getTrace = true, $catcher = null): string |
33 |
| - { |
34 |
| - return self::toHtml($e, $getTrace, $catcher, true); |
35 |
| - } |
36 |
| - |
37 |
| - /** |
38 |
| - * @param Throwable $e |
39 |
| - * @param bool $getTrace |
40 |
| - * @param null $catcher |
41 |
| - * |
42 |
| - * @return string |
43 |
| - * @see PhpException::toHtml() |
44 |
| - */ |
45 |
| - public static function toString(Throwable $e, bool $getTrace = true, $catcher = null): string |
46 |
| - { |
47 |
| - return self::toHtml($e, $getTrace, $catcher, true); |
48 |
| - } |
49 |
| - |
50 |
| - /** |
51 |
| - * Converts an exception into a simple string. |
52 |
| - * |
53 |
| - * @param Throwable $e the exception being converted |
54 |
| - * @param bool $clearHtml |
55 |
| - * @param bool $getTrace |
56 |
| - * @param null|string $catcher |
57 |
| - * |
58 |
| - * @return string the string representation of the exception. |
59 |
| - */ |
60 |
| - public static function toHtml(Throwable $e, bool $getTrace = true, string $catcher = null, bool $clearHtml = false): string |
61 |
| - { |
62 |
| - if (!$getTrace) { |
63 |
| - $message = "Error: {$e->getMessage()}"; |
64 |
| - } else { |
65 |
| - $message = sprintf( |
66 |
| - "<h3>%s(%d): %s</h3>\n<pre><strong>File: %s(Line %d)</strong>%s \n\n%s</pre>", |
67 |
| - get_class($e), |
68 |
| - $e->getCode(), |
69 |
| - $e->getMessage(), |
70 |
| - $e->getFile(), |
71 |
| - $e->getLine(), |
72 |
| - $catcher ? "\nCatch By: $catcher" : '', |
73 |
| - $e->getTraceAsString() |
74 |
| - ); |
75 |
| - } |
76 |
| - |
77 |
| - return $clearHtml ? strip_tags($message) : "<div class=\"exception-box\">{$message}</div>"; |
78 |
| - } |
79 |
| - |
80 |
| - /** |
81 |
| - * Converts an exception into a simple array. |
82 |
| - * |
83 |
| - * @param Throwable $e the exception being converted |
84 |
| - * @param bool $getTrace |
85 |
| - * @param null|string $catcher |
86 |
| - * |
87 |
| - * @return array |
88 |
| - */ |
89 |
| - public static function toArray(Throwable $e, bool $getTrace = true, string $catcher = null): array |
90 |
| - { |
91 |
| - $data = [ |
92 |
| - 'class' => get_class($e), |
93 |
| - 'message' => $e->getMessage(), |
94 |
| - 'code' => $e->getCode(), |
95 |
| - 'file' => $e->getFile() . ':' . $e->getLine(), |
96 |
| - ]; |
97 |
| - |
98 |
| - if ($catcher) { |
99 |
| - $data['catcher'] = $catcher; |
100 |
| - } |
101 |
| - |
102 |
| - if ($getTrace) { |
103 |
| - $data['trace'] = $e->getTrace(); |
104 |
| - } |
105 |
| - |
106 |
| - return $data; |
107 |
| - } |
108 |
| - |
109 |
| - /** |
110 |
| - * Converts an exception into a json string. |
111 |
| - * |
112 |
| - * @param Throwable $e the exception being converted |
113 |
| - * @param bool $getTrace |
114 |
| - * @param null|string $catcher |
115 |
| - * |
116 |
| - * @return string the string representation of the exception. |
117 |
| - */ |
118 |
| - public static function toJson(Throwable $e, bool $getTrace = true, string $catcher = null): string |
119 |
| - { |
120 |
| - if (!$getTrace) { |
121 |
| - return json_encode(['msg' => "Error: {$e->getMessage()}"]); |
122 |
| - } |
123 |
| - |
124 |
| - $map = [ |
125 |
| - 'code' => $e->getCode() ?: 500, |
126 |
| - 'msg' => sprintf( |
127 |
| - '%s(%d): %s, File: %s(Line %d)', |
128 |
| - get_class($e), |
129 |
| - $e->getCode(), |
130 |
| - $e->getMessage(), |
131 |
| - $e->getFile(), |
132 |
| - $e->getLine() |
133 |
| - ), |
134 |
| - 'data' => $e->getTrace() |
135 |
| - ]; |
136 |
| - |
137 |
| - if ($catcher) { |
138 |
| - $map['catcher'] = $catcher; |
139 |
| - } |
140 |
| - |
141 |
| - if ($getTrace) { |
142 |
| - $map['trace'] = $e->getTrace(); |
143 |
| - } |
144 |
| - |
145 |
| - return json_encode($map); |
146 |
| - } |
147 | 20 | }
|
0 commit comments