]> git.mxchange.org Git - hub.git/blob - application/hub/exceptions.php
Started with index page for welcoming the user and ask him to start searching the...
[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@shipsimu.org>
6  * @version             0.0
7  * @copyright   Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2015 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                 // Init variable
29                 $backTrace = '';
30
31                 // Get all call levels from backtrace
32                 foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
33                         // Init argument string
34                         $argsString = '';
35
36                         // Any arguments?
37                         if ((isset($traceArray['args'])) && (is_array($traceArray['args'])) && (count($traceArray['args']) > 0)) {
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                         } // END - if
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 - foreach
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                 /*
91                  * Invalid exception instance detected! Do *only* throw exceptions that
92                  * extends our own exception 'FrameworkException' to get such nice
93                  * outputs like above.
94                  */
95                 print('exceptionInstance[]=' . gettype($exceptionInstance) . ' is invalid! Please inform the core developer team.');
96         }
97 }
98
99 // Error handler
100 function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext) {
101         // Construct the message
102         $message = sprintf('File: %s, Line: %s, Code: %s, Message: %s',
103                 basename($errfile),
104                 $errline,
105                 $errno,
106                 $errstr
107         );
108
109         // Throw an exception here
110         throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
111 } // END - function
112
113 // Assertion handler
114 function __assertHandler ($file, $line, $code) {
115         // Empty code?
116         if ($code === '') {
117                 $code = '<em>Unknown</em>';
118         } // END - if
119
120         // Create message
121         $message = sprintf('File: %s, Line: %s, Code: %s',
122                 basename($file),
123                 $line,
124                 $code
125         );
126
127         // Log assert
128         die($message . PHP_EOL);
129         syslog(LOG_WARNING, $message);
130
131         // Throw an exception here
132         throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
133 } // END - function
134
135 // Set error handler
136 //set_error_handler('__errorHandler');
137
138 // Set the exception handler
139 set_exception_handler('hub_exception_handler');
140
141 // Init assert handling
142 assert_options(ASSERT_ACTIVE    , TRUE);
143 assert_options(ASSERT_WARNING   , FALSE);
144 assert_options(ASSERT_BAIL      , TRUE);
145 assert_options(ASSERT_QUIET_EVAL, FALSE);
146
147 // Set assertion handler
148 assert_options(ASSERT_CALLBACK  , '__assertHandler');
149
150 // [EOF]
151 ?>