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, this is free software
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()
65 // Make sure everything is assigned properly
66 parent::__construct($message, $code);
70 * Save the current backtrace
74 private final function saveBackTrace () {
76 $this->backTrace = debug_backtrace();
79 $dummy = array_shift($this->backTrace);
82 ksort($this->backTrace);
88 * @return $backTrace The full backtrace in an array
90 public final function getBackTrace () {
91 return $this->backTrace;
95 * Getter for printable backtrace
97 * @return $backTrace Backtrace for web pages
99 public final function getPrintableBackTrace () {
101 $dbgTrace = $this->getBackTrace();
103 // Taken from de.php.net user comments
104 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
105 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
106 // No info by default
109 // Are there arguments?
110 if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
111 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], true))."</pre>";
113 foreach ($dbgInfo['args'] as $debug) {
114 // Add only non-array elements
115 if (!is_array($debug)) {
116 $info .= $debug.", ";
120 $info = substr($info, 0, -2);
123 // Prepare argument infos
124 $info = "<em id=\"debug_args\">{$info}</em>";
127 $file = "Unknown file";
128 if (isset($dbgInfo['file'])) {
129 $file = basename($dbgInfo['file']);
133 $line = "Unknown line";
134 if (isset($dbgInfo['line'])) {
135 $line = "line {$dbgInfo['line']}";
139 $dbgMsg .= "\t at <em id=\"debug_id\">".$dbgIndex."</em> <em id=\"debug_file\">".$file."</em> (<em id=\"debug_line\">".$line."</em>) -> ".$dbgInfo['function']."(".$info.")<br />\n";
141 $dbgMsg .= "Debug backtrace end<br />\n";
147 * Returns the name of the thrown exception
149 * @return $toString The name of the thrown exception
151 public function __toString() {
152 return get_class($this);
156 * Getter for hex-decimal code
158 * @param $code Integer code to encode in hex
159 * @return $hexCode The exception code in hex-decimal format
161 public final function getHexCode ($code = null) {
162 // Get the decimal code
163 if (is_null($code)) $code = $this->getCode();
165 // Format it to hex-decimal, 0x as prefix and 3 chars
166 $hexCode = sprintf("0x%03s", dechex($code));
173 * Setter for extra data
175 * @param $extraData Extra data to store
178 protected final function setExtraData ($extraData) {
179 $this->extraData = $extraData;
183 * Getter for extra data
185 * @return $extraData Extra data to store
187 public final function getExtraData () {
188 return $this->extraData;