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