d7bbe1c49d8282c5caa0972369c7e4257fcf18f5
[core.git] / framework / main / exceptions / class_FrameworkException.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Generic;
4
5 // Import SPL stuff
6 use \ReflectionException;
7
8 /**
9  * A general abstract exception. You should not throw this even when you
10  * remove the "abstract" key-word. Better you make your own exception and
11  * attach a dedicated message to it.
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 abstract class FrameworkException extends ReflectionException {
33         /**
34          * Array for the backtrace
35          */
36         private $backTrace = [];
37
38         /**
39          * Extra data
40          */
41         private $extraData = '';
42
43         /**
44          * The super constructor for all exceptions
45          *
46          * @param       $message        The non-optional message for the exception
47          * @param       $code           An optional code for better debugging
48          * @return      void
49          */
50         public function __construct (string $message, int $code = 0) {
51                 // Make sure everything is assigned properly
52                 parent::__construct($message, $code);
53
54                 // Extract backtrace
55                 $this->saveBackTrace();
56
57                 // In emergency exit?
58                 if (defined('EMERGENCY_EXIT_CALLED')) {
59                         // Output message
60                         printf(
61                                 '[%s:] Message: %s, Backtrace: <pre>%s</pre>',
62                                 $this->__toString(),
63                                 $message,
64                                 $this->getPrintableBackTrace()
65                         );
66
67                         // End here
68                         exit();
69                 }
70
71                 // Should we log exceptions? (bad implementation)
72                 if (defined('LOG_EXCEPTIONS')) {
73                         // Log the error
74                         error_log(sprintf(
75                                 '[%s:] %s (%s)',
76                                 $this->__toString(),
77                                 $message,
78                                 $this->getHexCode()
79                         ));
80                 }
81         }
82
83         /**
84          * Save the current backtrace
85          *
86          * @return      void
87          */
88         private final function saveBackTrace () {
89                 // Get full backtrace
90                 $this->backTrace = debug_backtrace();
91
92                 // Remove this call
93                 $dummy = array_shift($this->backTrace);
94
95                 // resort the array
96                 ksort($this->backTrace);
97         }
98
99         /**
100          * Get saved backtrace
101          *
102          * @return      $backTrace      The full backtrace in an array
103          */
104         public final function getBackTrace () {
105                 return $this->backTrace;
106         }
107
108         /**
109          * Getter for printable backtrace
110          *
111          * @return      $backTrace      Backtrace for web pages
112          */
113         public final function getPrintableBackTrace () {
114                 // Get the backtrace
115                 $dbgTrace = $this->getBackTrace();
116
117                 // Taken from de.php.net user comments
118                 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
119                 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
120                         // No info by default
121                         $info = 'NULL';
122
123                         // Are there arguments?
124                         if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
125                                 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], true))."</pre>";
126                                 $info = '';
127                                 foreach ($dbgInfo['args'] as $debug) {
128                                         // Add only non-array elements
129                                         if (!is_array($debug)) {
130                                                 $info .= $debug . ', ';
131                                         }
132                                 } // END - foreach
133
134                                 // Remove last chars (commata, space)
135                                 $info = substr($info, 0, -2);
136                         }
137
138                         // Prepare argument infos
139                         $info = '<em id="debug_args_' . $dbgIndex . '">' . $info . '</em>';
140
141                         // File detection
142                         $file = 'Unknown file';
143                         if (isset($dbgInfo['file'])) {
144                                 $file = basename($dbgInfo['file']);
145                         }
146
147                         // Line detection
148                         $line = 'Unknown line';
149                         if (isset($dbgInfo['line'])) {
150                                 $line = 'line ' . $dbgInfo['line'];
151                         }
152
153                         // The message
154                         $dbgMsg .= "\t at <em id=\"debug_id_".$dbgIndex."\">".$dbgIndex."</em> <em id=\"debug_file_".$dbgIndex."\">".$file."</em> (<em id=\"debug_line_".$dbgIndex."\">".$line."</em>) -&gt; ".$dbgInfo['function'].'('.$info.")<br />\n";
155                 }
156
157                 // Add end-message
158                 $dbgMsg .= "Debug backtrace end<br />\n";
159
160                 // Return full debug message
161                 return $dbgMsg;
162         }
163
164         /**
165          * Returns the name of the thrown exception
166          *
167          * @return      $toString               The name of the thrown exception
168          */
169         public function __toString() {
170                 return get_class($this);
171         }
172
173         /**
174          * Getter for hex-decimal code
175          *
176          * @param       $code           Integer code to encode in hex
177          * @return      $hexCode        The exception code in hex-decimal format
178          */
179         public final function getHexCode (int $code = NULL) {
180                 // Is NULL?
181                 if (is_null($code)) {
182                         // Get default error code
183                         $code = $this->getCode();
184                 }
185
186                 // Format it to hex-decimal, 0x as prefix and 3 chars
187                 $hexCode = sprintf("0x%03s", dechex($code));
188
189                 // Return it
190                 return $hexCode;
191         }
192
193         /**
194          * Setter for extra data
195          *
196          * @param       $extraData      Extra data to store
197          * @return      void
198          */
199         protected final function setExtraData (string $extraData) {
200                 $this->extraData = $extraData;
201         }
202
203         /**
204          * Getter for extra data
205          *
206          * @return      $extraData      Extra data to store
207          */
208         public final function getExtraData () {
209                 return $this->extraData;
210         }
211
212 }