3 namespace Org\Mxchange\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')) {
65 '[%s:] Message: %s, Backtrace: <pre>%s</pre>',
68 $this->getPrintableBackTrace()
75 // Should we log exceptions? (bad implementation)
76 if (defined('LOG_EXCEPTIONS')) {
88 * Save the current backtrace
92 private final function saveBackTrace () {
94 $this->backTrace = debug_backtrace();
97 $dummy = array_shift($this->backTrace);
100 ksort($this->backTrace);
104 * Get saved backtrace
106 * @return $backTrace The full backtrace in an array
108 public final function getBackTrace () {
109 return $this->backTrace;
113 * Getter for printable backtrace
115 * @return $backTrace Backtrace for web pages
117 public final function getPrintableBackTrace () {
119 $dbgTrace = $this->getBackTrace();
121 // Taken from de.php.net user comments
122 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
123 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
124 // No info by default
127 // Are there arguments?
128 if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
129 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], true))."</pre>";
131 foreach ($dbgInfo['args'] as $debug) {
132 // Add only non-array elements
133 if (!is_array($debug)) {
134 $info .= $debug . ', ';
138 // Remove last chars (commata, space)
139 $info = substr($info, 0, -2);
142 // Prepare argument infos
143 $info = '<em id="debug_args_' . $dbgIndex . '">' . $info . '</em>';
146 $file = 'Unknown file';
147 if (isset($dbgInfo['file'])) {
148 $file = basename($dbgInfo['file']);
152 $line = 'Unknown line';
153 if (isset($dbgInfo['line'])) {
154 $line = 'line ' . $dbgInfo['line'];
158 $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";
162 $dbgMsg .= "Debug backtrace end<br />\n";
164 // Return full debug message
169 * Returns the name of the thrown exception
171 * @return $toString The name of the thrown exception
173 public function __toString() {
174 return get_class($this);
178 * Getter for hex-decimal code
180 * @param $code Integer code to encode in hex
181 * @return $hexCode The exception code in hex-decimal format
183 public final function getHexCode ($code = NULL) {
184 // Get the decimal code
185 if (is_null($code)) $code = $this->getCode();
187 // Format it to hex-decimal, 0x as prefix and 3 chars
188 $hexCode = sprintf("0x%03s", dechex($code));
195 * Setter for extra data
197 * @param $extraData Extra data to store
200 protected final function setExtraData ($extraData) {
201 $this->extraData = $extraData;
205 * Getter for extra data
207 * @return $extraData Extra data to store
209 public final function getExtraData () {
210 return $this->extraData;