|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pimf |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Pimf; |
|
10
|
|
|
|
|
11
|
|
|
use Pimf\Util\Header; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Defines the default exception handler if an exception is not caught within a try/catch block. |
|
15
|
|
|
* Execution will stop after the exception_handler is called. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Pimf |
|
18
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class Error |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Handle an exception and display the exception report. |
|
24
|
|
|
* |
|
25
|
|
|
* @param \Exception $exception |
|
26
|
|
|
* @param Logger $logger |
|
27
|
|
|
* @param boolean $exit |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function exception(\Exception $exception, Logger $logger, $exit = true) |
|
30
|
|
|
{ |
|
31
|
|
|
static::log($exception, $logger); |
|
32
|
|
|
|
|
33
|
|
|
ob_get_length() > 0 && ob_get_level() && ob_end_clean(); |
|
34
|
|
|
|
|
35
|
|
|
if (Config::get('error.debug_info') === true) { |
|
36
|
|
|
echo static::format($exception, Sapi::isCli()); |
|
37
|
|
|
if ($exit) { |
|
38
|
|
|
exit; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ($exception instanceof \Pimf\Controller\Exception |
|
43
|
|
|
|| $exception instanceof \Pimf\Resolver\Exception |
|
44
|
|
|
) { |
|
45
|
|
|
Event::first('404', array($exception)); |
|
46
|
|
|
Header::sendNotFound(null, $exit); |
|
47
|
|
|
} else { |
|
48
|
|
|
Event::first('500', array($exception)); |
|
49
|
|
|
Header::sendInternalServerError(null, $exit); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* If detailed errors are enabled, just format the exception into |
|
55
|
|
|
* a simple error message and display it. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \Exception $exception |
|
58
|
|
|
* @param boolean $isCli |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function format(\Exception $exception, $isCli = false) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($isCli === true) { |
|
65
|
|
|
return |
|
66
|
|
|
"+++ Untreated Exception +++" . PHP_EOL . "Message: " . $exception->getMessage() . PHP_EOL . "Location: " . $exception->getFile() |
|
67
|
|
|
. " on line " . $exception->getLine() . PHP_EOL . "Stack Trace: " . PHP_EOL . $exception->getTraceAsString() . PHP_EOL; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return "<html> |
|
71
|
|
|
<head> |
|
72
|
|
|
<style> |
|
73
|
|
|
pre { display: block; |
|
74
|
|
|
padding: 8.5px; |
|
75
|
|
|
margin: 0 0 9px; |
|
76
|
|
|
line-height: 18px; |
|
77
|
|
|
word-break: break-all; |
|
78
|
|
|
word-wrap: break-word; |
|
79
|
|
|
white-space: pre; |
|
80
|
|
|
white-space: pre-wrap; |
|
81
|
|
|
border: 1px solid #ccc; |
|
82
|
|
|
border: 1px solid rgba(0, 0, 0, 0.15); |
|
83
|
|
|
-webkit-border-radius: 4px; |
|
84
|
|
|
-moz-border-radius: 4px; |
|
85
|
|
|
border-radius: 6px; |
|
86
|
|
|
color: chartreuse; |
|
87
|
|
|
background-color: black; |
|
88
|
|
|
} |
|
89
|
|
|
</style> |
|
90
|
|
|
</head> |
|
91
|
|
|
<h2>Untreated Exception</h2> |
|
92
|
|
|
<h3>Message:</h3> |
|
93
|
|
|
<pre>" . $exception->getMessage() . "</pre> |
|
94
|
|
|
<h3>Location:</h3> |
|
95
|
|
|
<pre>" . $exception->getFile() . " on line " . $exception->getLine() . "</pre> |
|
96
|
|
|
<h3>Stack Trace:</h3> |
|
97
|
|
|
<pre>" . $exception->getTraceAsString() . "</pre></html>"; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Handle a native PHP error as an ErrorException. |
|
102
|
|
|
* |
|
103
|
|
|
* @param int $code |
|
104
|
|
|
* @param string $error |
|
105
|
|
|
* @param string $file |
|
106
|
|
|
* @param int $line |
|
107
|
|
|
* @param Logger $logger |
|
108
|
|
|
* @param array|int $reporting which PHP errors are reported |
|
109
|
|
|
* @param boolean $exit |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function native($code, $error, $file, $line, Logger $logger, $reporting, $exit = true) |
|
112
|
|
|
{ |
|
113
|
|
|
if ($reporting === 0) { |
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// create an ErrorException for the PHP error |
|
118
|
|
|
$exception = new \ErrorException($error, $code, 0, $file, $line); |
|
119
|
|
|
|
|
120
|
|
|
if (in_array($code, (array)Config::get('error.ignore_levels'))) { |
|
121
|
|
|
return static::log($exception, $logger); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// display the ErrorException |
|
125
|
|
|
static::exception($exception, $logger, $exit); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Handle the PHP shutdown event. |
|
130
|
|
|
* |
|
131
|
|
|
* @param Logger $logger |
|
132
|
|
|
* @param array|null $error |
|
133
|
|
|
* @param bool $exit |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function shutdown(Logger $logger, $error, $exit = true) |
|
136
|
|
|
{ |
|
137
|
|
|
// if a fatal error occurred |
|
138
|
|
|
if (!is_null($error)) { |
|
139
|
|
|
static::exception(new \ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line']), |
|
140
|
|
|
$logger, $exit); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param \Exception $exception |
|
146
|
|
|
* @param Logger $logger |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function log(\Exception $exception, Logger $logger) |
|
149
|
|
|
{ |
|
150
|
|
|
if (Config::get('error.log') === true) { |
|
151
|
|
|
$logger->error($exception->getMessage() . ' ' . $exception->getTraceAsString()); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|