1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
<?php
/**
* Handles error reporting and debugging.
* @package Core
*/
class Debug
{
/**
* An array of logged items
* @var array
* @access public
* @static
*/
public static $logged = array();
/**
* Displays the error page. If you have 'silent_errors' enabled in
* core.php config file, a small message will be shown instead.
*
* @return void
* @access public
*/
public static function render_error($exception)
{
ob_end_clean();
if ($exception->getCode() == 404)
{
$status = '404 Not Found';
}
else
{
$status = '503 Service Temporarily Unavailable';
}
header($_SERVER["SERVER_PROTOCOL"].' '.$status);
header("Status: {$status}");
if (Config::get('core.errors.silent', false))
{
echo $status;
return;
}
$view = View::get('debug');
$view->exception = $exception;
$view->log = Debug::$logged;
echo $view->render();
}
/**
* Catches errors and exceptions and sends them
* to the configured handler if one is present,
* otherwise render_error() will be called.
*
* @param Exception $exception Caught exception
* @return void
* @access public
* @static
*/
public static function onError($exception)
{
set_exception_handler(array('Debug', 'internalException'));
set_error_handler(array('Debug', 'internalError'), E_ALL);
$handler = Config::get('core.errors.handler', 'Debug::render_error');
call_user_func($handler, $exception);
}
/**
* Converts PHP Errors to Exceptions
*
* @param string $errno Error number
* @param string $errstr Error message
* @param string $errfile File in which the error occurred
* @param string $errline Line at which the error occurred
* @return void
* @access public
* @throws ErrorException Throws converted exception to be immediately caught
* @static
*/
public static function errorHandler($errno, $errstr, $errfile, $errline)
{
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
/**
* Handles exceptions that occurred while inside the error handler. Prevents recursion.
*
* @param Exception $exception Caught exception
* @return void
* @access public
* @static
*/
public static function internalException($exception)
{
echo $exception->getMessage().' in '.$exception->getFile().' on line '.$exception->getLine();
}
/**
* Handles errors that occurred while inside the error handler. Prevents recursion.
*
* @param string $errno Error number
* @param string $errstr Error message
* @param string $errfile File in which the error occurred
* @param string $errline Line at which the error occurred
* @return void
* @access public
* @static
*/
public static function internalError($errno, $errstr, $errfile, $errline)
{
echo $errstr.' in '.$errfile.' on line '.$errline;
}
/**
* Initializes the error handler
*
* @return void
* @access public
* @static
*/
public static function init()
{
set_exception_handler(array('Debug', 'onError'));
set_error_handler(array('Debug', 'errorHandler'), E_ALL);
}
/**
* Adds an item to the log.
*
* @param mixed $val Item to be logged
* @return void
* @access public
* @static
*/
public static function log($val)
{
array_unshift(Debug::$logged, $val);
}
}
|