3 * A general abstract exception. You should not throw this even when you
4 * remove the "abstract" key-word. Better you make your own exception and
5 * attach a dedicated message to it.
7 * @author Roland Haeder <webmaster@ship-simu.org>
9 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
10 * @license GNU GPL 3.0 or any newer version
11 * @link http://www.ship-simu.org
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 abstract class FrameworkException extends ReflectionException {
28 * Array for the backtrace
30 private $backTrace = array();
35 private $extraData = '';
38 * The super constructor for all exceptions
40 * @param $message The non-optional message for the exception
41 * @param $code An optional code for better debugging
44 public function __construct ($message, $code = 0) {
46 $this->saveBackTrace();
49 $message = (string) $message;
53 if (defined('EMERGENCY_EXIT_CALLED')) {
55 printf("[%s:] Message: %s, Backtrace: <pre>%s</pre>",
58 $this->getPrintableBackTrace()
64 // Make sure everything is assigned properly
65 parent::__construct($message, $code);
67 // Log it away if DEBUG_ALL is set
68 if (defined('DEBUG_ALL')) {
70 error_log(sprintf("[%s:] %s (%s)",
79 * Save the current backtrace
83 private final function saveBackTrace () {
85 $this->backTrace = debug_backtrace();
88 $dummy = array_shift($this->backTrace);
91 ksort($this->backTrace);
97 * @return $backTrace The full backtrace in an array
99 public final function getBackTrace () {
100 return $this->backTrace;
104 * Getter for printable backtrace
106 * @return $backTrace Backtrace for web pages
108 public final function getPrintableBackTrace () {
110 $dbgTrace = $this->getBackTrace();
112 // Taken from de.php.net user comments
113 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
114 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
115 // No info by default
118 // Are there arguments?
119 if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
120 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], true))."</pre>";
122 foreach ($dbgInfo['args'] as $debug) {
123 // Add only non-array elements
124 if (!is_array($debug)) {
125 $info .= $debug . ', ';
129 // Remove last chars (commata, space)
130 $info = substr($info, 0, -2);
133 // Prepare argument infos
134 $info = "<em id=\"debug_args_".$dbgIndex."\">{$info}</em>";
137 $file = "Unknown file";
138 if (isset($dbgInfo['file'])) {
139 $file = basename($dbgInfo['file']);
143 $line = "Unknown line";
144 if (isset($dbgInfo['line'])) {
145 $line = "line {$dbgInfo['line']}";
149 $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";
153 $dbgMsg .= "Debug backtrace end<br />\n";
155 // Return full debug message
160 * Returns the name of the thrown exception
162 * @return $toString The name of the thrown exception
164 public function __toString() {
165 return get_class($this);
169 * Getter for hex-decimal code
171 * @param $code Integer code to encode in hex
172 * @return $hexCode The exception code in hex-decimal format
174 public final function getHexCode ($code = null) {
175 // Get the decimal code
176 if (is_null($code)) $code = $this->getCode();
178 // Format it to hex-decimal, 0x as prefix and 3 chars
179 $hexCode = sprintf("0x%03s", dechex($code));
186 * Setter for extra data
188 * @param $extraData Extra data to store
191 protected final function setExtraData ($extraData) {
192 $this->extraData = $extraData;
196 * Getter for extra data
198 * @return $extraData Extra data to store
200 public final function getExtraData () {
201 return $this->extraData;