-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexceptionhandler.php
285 lines (249 loc) · 7.98 KB
/
exceptionhandler.php
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* @package midgardmvc_core
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
/**
* Midgard MVC exception handler
*
* @package midgardmvc_core
*/
class midgardmvc_core_exceptionhandler
{
public static function handle(Exception $exception)
{
$data = self::prepare_exception_data($exception);
if (isset($exception->midgardmvc))
{
$midgardmvc = $exception->midgardmvc;
}
try
{
if (!isset($midgardmvc))
{
$midgardmvc = midgardmvc_core::get_instance();
}
$midgardmvc->log($data['message_type'], $data['message'], 'warning');
}
catch (Exception $e)
{
// MVC not initialized, display original message and exit
return self::show_error_plaintext($data);
}
if (!$midgardmvc->dispatcher->headers_sent())
{
$midgardmvc->dispatcher->header("X-MidgardMVC-Error: {$data['message']}");
$midgardmvc->dispatcher->header($data['header']);
}
if ($data['http_code'] == 304)
{
return;
}
if (!isset($exception->request)) {
die($exception->__toString());
}
self::show_error_templated($data, $midgardmvc, $exception->request);
}
public static function handle_assert($file, $line, $expression)
{
$message = "Assertion '{$expression}' failed, {$file} line {$line}";
midgardmvc_core::get_instance()->log('midgardmvc_core', "Assertion {$expression} failed, {$file} {$line}", 'warning');
throw new RunTimeException($message);
}
private static function prepare_exception_data(Exception $exception)
{
$data = array();
$data['message_type'] = get_class($exception);
$data['http_code'] = self::code_by_exception($exception);
$data['message'] = strip_tags($exception->getMessage());
$data['header'] = self::header_by_code($data['http_code']);
$data['exception'] = $exception;
$data['trace'] = null;
return $data;
}
private static function show_error_plaintext(array $data, $dispatcher = null)
{
$message = "<h1>Unexpected Error</h1>\n\n<p>Headers were sent so we don't have correct HTTP code ({$data['http_code']}).</p>\n\n<p>{$data['message_type']}: {$data['message']}</p>\n";
if (is_null($dispatcher))
{
// We got an exception before MVC was fully initialized
if (!headers_sent())
{
header("X-MidgardMVC-Error: {$data['message']}");
header(self::header_by_code($data['http_code']));
}
die($message);
}
echo $message;
$dispatcher->end_request();
}
private static function show_error_templated(array $data, midgardmvc_core $midgardmvc, midgardmvc_core_request $request)
{
$midgardmvc->dispatcher->header('Content-Type: text/html; charset=utf-8');
if ($midgardmvc->configuration->enable_exception_trace)
{
$data['trace'] = $exception->getTrace();
}
try
{
if (! $request)
{
// Exception happened before request was set to context
$request = self::bootstrap_request();
}
$route = $request->get_route();
if ($route)
{
$errorRoute = clone $route;
$errorRoute->template_aliases['root'] = 'midgardmvc-show-error';
$request->set_route($errorRoute);
}
$request->set_data_item('midgardmvc_core_exceptionhandler', $data);
$request->set_data_item('cache_enabled', false);
$midgardmvc->templating->template($request);
$midgardmvc->templating->display($request);
}
catch (Exception $e)
{
// Templating isn't working
self::show_error_untemplated($data);
}
// Clean up and finish
$midgardmvc->context->delete();
}
private static function show_error_untemplated(array $data)
{
echo "<!DOCTYPE html>\n";
echo "<html>\n";
echo " <head>\n";
echo " <title>{$data['header']}</title>\n";
echo " </head>\n";
echo " <body class=\"{$data['message_type']}\">\n";
echo " <h1>{$data['header']}</h1>\n";
echo " <p>{$data['message']}</p>\n";
echo " </body>\n";
echo "</html>";
}
private static function bootstrap_request()
{
$request = new midgardmvc_core_request();
$core = midgardmvc_core::get_instance()->component->get('midgardmvc_core');
$request->add_component_to_chain($core);
$route = new midgardmvc_core_route('midgardmvc_show_error', '', '', '', array());
$request->set_route($route);
$request->set_component($core);
midgardmvc_core::get_instance()->context->create($request);
return $request;
}
public static function code_by_exception(Exception $exception)
{
// Different HTTP error codes for different Exceptions
switch (get_class($exception))
{
case 'midgardmvc_exception_notfound':
case 'midgardmvc_exception_unauthorized':
case 'midgardmvc_exception_httperror':
return $exception->getHttpCode();
}
return 500;
}
public static function header_by_code($code)
{
$headers = array
(
200 => 'HTTP/1.0 200 OK',
303 => 'HTTP/1.0 303 See Other',
304 => 'HTTP/1.0 304 Not Modified',
401 => 'HTTP/1.0 401 Unauthorized',
404 => 'HTTP/1.0 404 Not Found',
405 => 'HTTP/1.0 405 Method not allowed',
500 => 'HTTP/1.0 500 Server Error',
503 => 'HTTP/1.0 503 Service Unavailable',
);
if (!isset($headers[$code]))
{
$code = 500;
}
return $headers[$code];
}
}
/**
* Basic Midgard MVC exception
*
* @package midgardmvc_core
*/
class midgardmvc_exception extends Exception
{
var $midgardvc = null;
var $request = null;
public function __construct($message, $code = 500)
{
parent::__construct($message, $code);
$this->midgardmvc = midgardmvc_core::get_instance();
if ($this->midgardmvc->context)
{
$this->request = $this->midgardmvc->context->get_request();
}
}
public function getHttpCode()
{
return 500;
}
}
/**
* Midgard MVC "not found" exception
*
* @package midgardmvc_core
*/
class midgardmvc_exception_notfound extends midgardmvc_exception
{
// Redefine the exception so message isn't optional
public function __construct($message, $code = 404)
{
parent::__construct($message, $code);
}
public function getHttpCode()
{
return 404;
}
}
/**
* Midgard MVC "unauthorized" exception
*
* @package midgardmvc_core
*/
class midgardmvc_exception_unauthorized extends midgardmvc_exception
{
// Redefine the exception so message isn't optional
public function __construct($message, $code = 401)
{
parent::__construct($message, $code);
}
public function getHttpCode()
{
return 401;
}
}
/**
* Midgard MVC generic HTTP error exception
*
* @package midgardmvc_core
*/
class midgardmvc_exception_httperror extends midgardmvc_exception
{
protected $httpcode = 500;
// Redefine the exception so message isn't optional
public function __construct($message, $code = 500)
{
$this->httpcode = $code;
parent::__construct($message, $code);
}
public function getHttpCode()
{
return $this->httpcode;
}
}
set_exception_handler(array('midgardmvc_core_exceptionhandler', 'handle'));
?>