]> git.mxchange.org Git - hub.git/blob - application/hub/exceptions.php
'public static final' is the right thing, some variables renamed to make clear what...
[hub.git] / application / hub / exceptions.php
1 <?php
2 /**
3  * An include file for setting up the exception handler of the hub
4  *
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0
8  * @copyright   Copyright (c) 2007 - 2008 Roland Haeder, 2009, 2010 Hub Developer Team
9  * @license             GNU GPL 3.0 or any newer version
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 // The hub's own exception handler
26 function hub_exception_handler ($exceptionInstance) {
27         // Is it an object and a valid instance?
28         if ((is_object($exceptionInstance)) && ($exceptionInstance instanceof FrameworkException)) {
29                 // Get the regular trace
30                 $trace = $exceptionInstance->getTrace();
31
32                 // Get 3 call levels
33                 $backTrace = '';
34                 for ($idx = 0; $idx < 3; $idx++) {
35                         // Copy array for argument analysis and init variable
36                         $traceArray = $trace[$idx];
37                         $argsString = '';
38
39                         // Convert arguments type into human-readable
40                         foreach ($traceArray['args'] as $arg) {
41                                 $argsString .= ', ' . gettype($arg);
42                         } // END - foreach
43                         $argsString = substr($argsString, 2);
44
45                         // Set missing file/line
46                         if (!isset($traceArray['file']))  $traceArray['file']  = 'unknown';
47                         if (!isset($traceArray['line']))  $traceArray['line']  = '0';
48                         if (!isset($traceArray['class'])) $traceArray['class'] = 'UnknownObject';
49                         if (!isset($traceArray['type']))  $traceArray['type']  = '->';
50
51                         $backTrace .= sprintf("---------- Pos %d: ----------
52 Method : %s%s%s(%s)
53 ----- Caller: -----
54 File   : %s
55 Line   : %d\n",
56                                 ($idx + 1),
57                                 $traceArray['class'],
58                                 $traceArray['type'],
59                                 $traceArray['function'],
60                                 $argsString,
61                                 basename($traceArray['file']),
62                                 $traceArray['line']
63                         );
64                 } // END - for
65
66                 // Construct the message
67                 $message = sprintf("--------------------------------------------------------------------------------
68 Uncaught Exception : %s
69 --------------------------------------------------------------------------------
70 Message            : %s
71 Code               : %s
72 File               : %s
73 Line               : %d
74 --------------------------------------------------------------------------------
75 Backtrace:
76 --------------------------------------------------------------------------------
77 %s
78 --------------------------------------------------------------------------------\n",
79                         trim(html_entity_decode(strip_tags($exceptionInstance->__toString()))),
80                         trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
81                         $exceptionInstance->getHexCode(),
82                         $exceptionInstance->getFile(),
83                         $exceptionInstance->getLine(),
84                         trim($backTrace)
85                 );
86
87                 // Output the message
88                 print($message);
89         } else {
90                 // Invalid exception instance detected! Do *only* throw exceptions that
91                 // extends our own exception 'FrameworkException' to get such nice
92                 // outputs like above.
93                 print("exceptionInstance is invalid! Please inform the core developer team.\n");
94         }
95 }
96
97 // Error handler
98 function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext) {
99         // Construct the message
100         $message = sprintf("File: %s, Line: %s, Code: %s, Message: %s",
101                 basename($errfile),
102                 $errline,
103                 $errno,
104                 $errstr
105         );
106
107         // Throw an exception here
108         throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
109 } // END - function
110
111 // Assertion handler
112 function __assertHandler ($file, $line, $code) {
113         // Empty code?
114         if ($code === '') $code = '<em>Unknown</em>';
115
116         // Create message
117         $message = sprintf("File: %s, Line: %s, Code: %s",
118                 basename($file),
119                 $line,
120                 $code
121         );
122
123         // Throw an exception here
124         throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
125 } // END - function
126
127 // Set error handler
128 //set_error_handler('__errorHandler');
129
130 // Set the new handler
131 set_exception_handler('hub_exception_handler');
132
133 // Init assert handling
134 assert_options(ASSERT_ACTIVE,     1);
135 assert_options(ASSERT_WARNING,    0);
136 assert_options(ASSERT_BAIL,       0);
137 assert_options(ASSERT_QUIET_EVAL, 0);
138 assert_options(ASSERT_CALLBACK,   '__assertHandler');
139
140 // [EOF]
141 ?>