3 namespace CoreFramework\Generic;
6 * A general abstract exception. You should not throw this even when you
7 * remove the "abstract" key-word. Better you make your own exception and
8 * attach a dedicated message to it.
10 * @author Roland Haeder <webmaster@shipsimu.org>
12 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
13 * @license GNU GPL 3.0 or any newer version
14 * @link http://www.shipsimu.org
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 abstract class FrameworkException extends ReflectionException {
31 * Array for the backtrace
33 private $backTrace = array();
38 private $extraData = '';
41 * The super constructor for all exceptions
43 * @param $message The non-optional message for the exception
44 * @param $code An optional code for better debugging
47 public function __construct ($message, $code = 0) {
48 // Make sure everything is assigned properly
49 parent::__construct($message, $code);
52 $this->saveBackTrace();
55 $message = (string) $message;
59 if (defined('EMERGENCY_EXIT_CALLED')) {
61 printf("[%s:] Message: %s, Backtrace: <pre>%s</pre>",
64 $this->getPrintableBackTrace()
71 // Should we log exceptions? (bad implementation)
72 if (defined('LOG_EXCEPTIONS')) {
74 error_log(sprintf("[%s:] %s (%s)",
83 * Save the current backtrace
87 private final function saveBackTrace () {
89 $this->backTrace = debug_backtrace();
92 $dummy = array_shift($this->backTrace);
95 ksort($this->backTrace);
101 * @return $backTrace The full backtrace in an array
103 public final function getBackTrace () {
104 return $this->backTrace;
108 * Getter for printable backtrace
110 * @return $backTrace Backtrace for web pages
112 public final function getPrintableBackTrace () {
114 $dbgTrace = $this->getBackTrace();
116 // Taken from de.php.net user comments
117 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
118 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
119 // No info by default
122 // Are there arguments?
123 if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
124 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], TRUE))."</pre>";
126 foreach ($dbgInfo['args'] as $debug) {
127 // Add only non-array elements
128 if (!is_array($debug)) {
129 $info .= $debug . ', ';
133 // Remove last chars (commata, space)
134 $info = substr($info, 0, -2);
137 // Prepare argument infos
138 $info = '<em id="debug_args_' . $dbgIndex . '">' . $info . '</em>';
141 $file = 'Unknown file';
142 if (isset($dbgInfo['file'])) {
143 $file = basename($dbgInfo['file']);
147 $line = 'Unknown line';
148 if (isset($dbgInfo['line'])) {
149 $line = 'line ' . $dbgInfo['line'];
153 $dbgMsg .= "\t at <em id=\"debug_id_".$dbgIndex."\">".$dbgIndex."</em> <em id=\"debug_file_".$dbgIndex."\">".$file."</em> (<em id=\"debug_line_".$dbgIndex."\">".$line."</em>) -> ".$dbgInfo['function'].'('.$info.")<br />\n";
157 $dbgMsg .= "Debug backtrace end<br />\n";
159 // Return full debug message
164 * Returns the name of the thrown exception
166 * @return $toString The name of the thrown exception
168 public function __toString() {
169 return get_class($this);
173 * Getter for hex-decimal code
175 * @param $code Integer code to encode in hex
176 * @return $hexCode The exception code in hex-decimal format
178 public final function getHexCode ($code = NULL) {
179 // Get the decimal code
180 if (is_null($code)) $code = $this->getCode();
182 // Format it to hex-decimal, 0x as prefix and 3 chars
183 $hexCode = sprintf("0x%03s", dechex($code));
190 * Setter for extra data
192 * @param $extraData Extra data to store
195 protected final function setExtraData ($extraData) {
196 $this->extraData = $extraData;
200 * Getter for extra data
202 * @return $extraData Extra data to store
204 public final function getExtraData () {
205 return $this->extraData;