3 namespace CoreFramework\Generic;
6 use \ReflectionException;
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.
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
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.
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.
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/>.
32 abstract class FrameworkException extends ReflectionException {
34 * Array for the backtrace
36 private $backTrace = array();
41 private $extraData = '';
44 * The super constructor for all exceptions
46 * @param $message The non-optional message for the exception
47 * @param $code An optional code for better debugging
50 public function __construct ($message, $code = 0) {
51 // Make sure everything is assigned properly
52 parent::__construct($message, $code);
55 $this->saveBackTrace();
58 $message = (string) $message;
62 if (defined('EMERGENCY_EXIT_CALLED')) {
64 printf("[%s:] Message: %s, Backtrace: <pre>%s</pre>",
67 $this->getPrintableBackTrace()
74 // Should we log exceptions? (bad implementation)
75 if (defined('LOG_EXCEPTIONS')) {
77 error_log(sprintf("[%s:] %s (%s)",
86 * Save the current backtrace
90 private final function saveBackTrace () {
92 $this->backTrace = debug_backtrace();
95 $dummy = array_shift($this->backTrace);
98 ksort($this->backTrace);
102 * Get saved backtrace
104 * @return $backTrace The full backtrace in an array
106 public final function getBackTrace () {
107 return $this->backTrace;
111 * Getter for printable backtrace
113 * @return $backTrace Backtrace for web pages
115 public final function getPrintableBackTrace () {
117 $dbgTrace = $this->getBackTrace();
119 // Taken from de.php.net user comments
120 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
121 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
122 // No info by default
125 // Are there arguments?
126 if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
127 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], TRUE))."</pre>";
129 foreach ($dbgInfo['args'] as $debug) {
130 // Add only non-array elements
131 if (!is_array($debug)) {
132 $info .= $debug . ', ';
136 // Remove last chars (commata, space)
137 $info = substr($info, 0, -2);
140 // Prepare argument infos
141 $info = '<em id="debug_args_' . $dbgIndex . '">' . $info . '</em>';
144 $file = 'Unknown file';
145 if (isset($dbgInfo['file'])) {
146 $file = basename($dbgInfo['file']);
150 $line = 'Unknown line';
151 if (isset($dbgInfo['line'])) {
152 $line = 'line ' . $dbgInfo['line'];
156 $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";
160 $dbgMsg .= "Debug backtrace end<br />\n";
162 // Return full debug message
167 * Returns the name of the thrown exception
169 * @return $toString The name of the thrown exception
171 public function __toString() {
172 return get_class($this);
176 * Getter for hex-decimal code
178 * @param $code Integer code to encode in hex
179 * @return $hexCode The exception code in hex-decimal format
181 public final function getHexCode ($code = NULL) {
182 // Get the decimal code
183 if (is_null($code)) $code = $this->getCode();
185 // Format it to hex-decimal, 0x as prefix and 3 chars
186 $hexCode = sprintf("0x%03s", dechex($code));
193 * Setter for extra data
195 * @param $extraData Extra data to store
198 protected final function setExtraData ($extraData) {
199 $this->extraData = $extraData;
203 * Getter for extra data
205 * @return $extraData Extra data to store
207 public final function getExtraData () {
208 return $this->extraData;