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