1 | <?php |
||
2 | /** |
||
3 | * File containing the {@see AppUtils\BaseException} class. |
||
4 | * |
||
5 | * @package Application Utils |
||
6 | * @subpackage BaseException |
||
7 | * @see AppUtils\BaseException |
||
8 | */ |
||
9 | |||
10 | namespace AppUtils; |
||
11 | |||
12 | use Exception; |
||
13 | use Throwable; |
||
14 | |||
15 | /** |
||
16 | * Extended exception class with additional tools. Allows setting |
||
17 | * developer-only information that does not get shown along with |
||
18 | * the message, but can easily be retrieved and logged. |
||
19 | * |
||
20 | * @package Application Utils |
||
21 | * @subpackage BaseException |
||
22 | * @author Sebastian Mordziol <[email protected]> |
||
23 | */ |
||
24 | class BaseException extends Exception |
||
25 | { |
||
26 | protected ?string $details = null; |
||
27 | |||
28 | /** |
||
29 | * @param string $message |
||
30 | * @param string|NULL $details |
||
31 | * @param int|NULL $code |
||
32 | * @param Throwable|NULL $previous |
||
33 | */ |
||
34 | public function __construct(string $message, ?string $details=null, ?int $code=null, ?Throwable $previous=null) |
||
35 | { |
||
36 | if(defined('APP_UTILS_TESTSUITE') && APP_UTILS_TESTSUITE === 'true') |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
37 | { |
||
38 | $message .= PHP_EOL.$details; |
||
39 | } |
||
40 | |||
41 | if($code === null) |
||
42 | { |
||
43 | $code = 0; |
||
44 | } |
||
45 | |||
46 | parent::__construct($message, (int)$code, $previous); |
||
47 | |||
48 | $this->details = $details; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Retrieves the detailed error description, if any. |
||
53 | * @return string |
||
54 | */ |
||
55 | public function getDetails() : string |
||
56 | { |
||
57 | return $this->details ?? ''; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Displays pertinent information on the exception. |
||
62 | */ |
||
63 | public function display() : void |
||
64 | { |
||
65 | if(!headers_sent()) { |
||
66 | header('Content-type:text/plain; charset=utf-8'); |
||
67 | } |
||
68 | |||
69 | echo $this->getInfo(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Retrieves information on the exception that can be |
||
74 | * easily accessed. |
||
75 | * |
||
76 | * @return ConvertHelper_ThrowableInfo |
||
77 | */ |
||
78 | public function getInfo() : ConvertHelper_ThrowableInfo |
||
79 | { |
||
80 | return ConvertHelper::throwable2info($this); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Dumps a current PHP function trace, as a text only string. |
||
85 | */ |
||
86 | public static function dumpTraceAsString() : void |
||
87 | { |
||
88 | try |
||
89 | { |
||
90 | throw new BaseException(''); |
||
91 | } |
||
92 | catch(BaseException $e) |
||
93 | { |
||
94 | echo self::createInfo($e)->toString(); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Dumps a current PHP function trace, with HTML styling. |
||
100 | */ |
||
101 | public static function dumpTraceAsHTML() : void |
||
102 | { |
||
103 | try |
||
104 | { |
||
105 | throw new BaseException(''); |
||
106 | } |
||
107 | catch(BaseException $e) |
||
108 | { |
||
109 | echo '<pre style="background:#fff;font-family:monospace;font-size:14px;color:#444;padding:16px;border:solid 1px #999;border-radius:4px;">'; |
||
110 | echo self::createInfo($e)->toString(); |
||
111 | echo '</pre>'; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Creates an exception info instance from a throwable instance. |
||
117 | * |
||
118 | * @param Throwable $e |
||
119 | * @return ConvertHelper_ThrowableInfo |
||
120 | * @see ConvertHelper::throwable2info() |
||
121 | */ |
||
122 | public static function createInfo(Throwable $e) : ConvertHelper_ThrowableInfo |
||
123 | { |
||
124 | return ConvertHelper::throwable2info($e); |
||
125 | } |
||
126 | } |
||
127 |