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 <<<<<<< HEAD:framework/main/exceptions/class_FrameworkException.php
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
19 >>>>>>> Some updates::inc/main/exceptions/class_FrameworkException.php
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 abstract class FrameworkException extends ReflectionException {
38 * Array for the backtrace
40 private $backTrace = array();
45 private $extraData = '';
48 * The super constructor for all exceptions
50 * @param $message The non-optional message for the exception
51 * @param $code An optional code for better debugging
54 public function __construct ($message, $code = 0) {
55 // Make sure everything is assigned properly
56 parent::__construct($message, $code);
59 $this->saveBackTrace();
62 $message = (string) $message;
66 if (defined('EMERGENCY_EXIT_CALLED')) {
69 '[%s:] Message: %s, Backtrace: <pre>%s</pre>',
72 $this->getPrintableBackTrace()
79 // Should we log exceptions? (bad implementation)
80 if (defined('LOG_EXCEPTIONS')) {
92 * Save the current backtrace
96 private final function saveBackTrace () {
98 $this->backTrace = debug_backtrace();
101 $dummy = array_shift($this->backTrace);
104 ksort($this->backTrace);
108 * Get saved backtrace
110 * @return $backTrace The full backtrace in an array
112 public final function getBackTrace () {
113 return $this->backTrace;
117 * Getter for printable backtrace
119 * @return $backTrace Backtrace for web pages
121 public final function getPrintableBackTrace () {
123 $dbgTrace = $this->getBackTrace();
125 // Taken from de.php.net user comments
126 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
127 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
128 // No info by default
131 // Are there arguments?
132 if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
133 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], true))."</pre>";
135 foreach ($dbgInfo['args'] as $debug) {
136 // Add only non-array elements
137 if (!is_array($debug)) {
138 $info .= $debug . ', ';
142 // Remove last chars (commata, space)
143 $info = substr($info, 0, -2);
146 // Prepare argument infos
147 $info = '<em id="debug_args_' . $dbgIndex . '">' . $info . '</em>';
150 $file = 'Unknown file';
151 if (isset($dbgInfo['file'])) {
152 $file = basename($dbgInfo['file']);
156 $line = 'Unknown line';
157 if (isset($dbgInfo['line'])) {
158 $line = 'line ' . $dbgInfo['line'];
162 $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";
166 $dbgMsg .= "Debug backtrace end<br />\n";
168 // Return full debug message
173 * Returns the name of the thrown exception
175 * @return $toString The name of the thrown exception
177 public function __toString() {
178 return get_class($this);
182 * Getter for hex-decimal code
184 * @param $code Integer code to encode in hex
185 * @return $hexCode The exception code in hex-decimal format
187 public final function getHexCode ($code = NULL) {
188 // Get the decimal code
189 if (is_null($code)) $code = $this->getCode();
191 // Format it to hex-decimal, 0x as prefix and 3 chars
192 $hexCode = sprintf("0x%03s", dechex($code));
199 * Setter for extra data
201 * @param $extraData Extra data to store
204 protected final function setExtraData ($extraData) {
205 $this->extraData = $extraData;
209 * Getter for extra data
211 * @return $extraData Extra data to store
213 public final function getExtraData () {
214 return $this->extraData;