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, 2010 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) {
45 // Make sure everything is assigned properly
46 parent::__construct($message, $code);
49 $this->saveBackTrace();
52 $message = (string) $message;
56 if (defined('EMERGENCY_EXIT_CALLED')) {
58 printf("[%s:] Message: %s, Backtrace: <pre>%s</pre>",
61 $this->getPrintableBackTrace()
68 // Should we log exceptions? (bad implementation)
69 if (defined('LOG_EXCEPTIONS')) {
71 error_log(sprintf("[%s:] %s (%s)",
80 * Save the current backtrace
84 private final function saveBackTrace () {
86 $this->backTrace = debug_backtrace();
89 $dummy = array_shift($this->backTrace);
92 ksort($this->backTrace);
98 * @return $backTrace The full backtrace in an array
100 public final function getBackTrace () {
101 return $this->backTrace;
105 * Getter for printable backtrace
107 * @return $backTrace Backtrace for web pages
109 public final function getPrintableBackTrace () {
111 $dbgTrace = $this->getBackTrace();
113 // Taken from de.php.net user comments
114 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
115 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
116 // No info by default
119 // Are there arguments?
120 if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
121 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], true))."</pre>";
123 foreach ($dbgInfo['args'] as $debug) {
124 // Add only non-array elements
125 if (!is_array($debug)) {
126 $info .= $debug . ', ';
130 // Remove last chars (commata, space)
131 $info = substr($info, 0, -2);
134 // Prepare argument infos
135 $info = "<em id=\"debug_args_".$dbgIndex."\">{$info}</em>";
138 $file = "Unknown file";
139 if (isset($dbgInfo['file'])) {
140 $file = basename($dbgInfo['file']);
144 $line = "Unknown line";
145 if (isset($dbgInfo['line'])) {
146 $line = "line {$dbgInfo['line']}";
150 $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";
154 $dbgMsg .= "Debug backtrace end<br />\n";
156 // Return full debug message
161 * Returns the name of the thrown exception
163 * @return $toString The name of the thrown exception
165 public function __toString() {
166 return get_class($this);
170 * Getter for hex-decimal code
172 * @param $code Integer code to encode in hex
173 * @return $hexCode The exception code in hex-decimal format
175 public final function getHexCode ($code = null) {
176 // Get the decimal code
177 if (is_null($code)) $code = $this->getCode();
179 // Format it to hex-decimal, 0x as prefix and 3 chars
180 $hexCode = sprintf("0x%03s", dechex($code));
187 * Setter for extra data
189 * @param $extraData Extra data to store
192 protected final function setExtraData ($extraData) {
193 $this->extraData = $extraData;
197 * Getter for extra data
199 * @return $extraData Extra data to store
201 public final function getExtraData () {
202 return $this->extraData;