forked from midgardproject/midgardmvc_core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexceptionhandler.php
203 lines (177 loc) · 5.82 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
<?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)
{
// Different HTTP error codes for different Exceptions
$message_type = get_class($exception);
switch ($message_type)
{
case 'midgardmvc_exception_notfound':
case 'midgardmvc_exception_unauthorized':
case 'midgardmvc_exception_httperror':
$http_code = $exception->getCode();
break;
default:
$http_code = 500;
break;
}
$message = strip_tags($exception->getMessage());
$message = str_replace("\n", ' ', $message);
$midgardmvc = midgardmvc_core::get_instance();
$midgardmvc->log($message_type, $message, 'warn');
if ($midgardmvc->firephp)
{
$midgardmvc->firephp->error($exception);
}
$header = self::header_by_code($http_code);
if (!isset($midgardmvc->dispatcher))
{
if (headers_sent())
{
die("<h1>Unexpected Error</h1>\n\n<p>Headers were sent so we don't have correct HTTP code ({$http_code}).</p>\n\n<p>{$message_type}: {$message}</p>\n");
}
header("X-MidgardMVC-Error: {$message}");
header($header);
}
else
{
if ($midgardmvc->dispatcher->headers_sent())
{
echo "<h1>Unexpected Error</h1>\n\n<p>Headers were sent so we don't have correct HTTP code ({$http_code}).</p>\n\n<p>{$message_type}: {$message}</p>\n";
$midgardmvc->dispatcher->end_request();
}
$midgardmvc->dispatcher->header("X-MidgardMVC-Error: {$message}");
$midgardmvc->dispatcher->header($header);
}
if ($http_code != 304)
{
if (isset($midgardmvc->dispatcher))
{
$midgardmvc->dispatcher->header('Content-Type: text/html; charset=utf-8');
}
$data['header'] = $header;
$data['message_type'] = $message_type;
$data['message'] = $message;
$data['exception'] = $exception;
$data['trace'] = false;
if (!$midgardmvc)
{
return;
}
if ($midgardmvc->configuration && $midgardmvc->configuration->enable_exception_trace)
{
$data['trace'] = $exception->getTrace();
}
try
{
if (!$midgardmvc->context)
{
throw new Exception('no context found');
}
$midgardmvc->context->set_item('midgardmvc_core_exceptionhandler', $data);
$midgardmvc->context->set_item('template_entry_point', 'midcom-show-error');
$midgardmvc->context->set_item('cache_enabled', false);
if (!$midgardmvc->templating)
{
throw new Exception('no templating found');
}
$midgardmvc->templating->template();
$midgardmvc->templating->display();
}
catch (Exception $e)
{
// Templating isn't working
echo "<!DOCTYPE html>\n";
echo "<html>\n";
echo " <head>\n";
echo " <title>{$header}</title>\n";
echo " </head>\n";
echo " <body class=\"{$message_type}\">\n";
echo " <h1>{$header}</h1>\n";
echo " <p>{$message}</p>\n";
echo " </body>\n";
echo "</html>";
}
// Clean up the context
$midgardmvc->context->delete();
}
}
private 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 {}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* Midgard MVC generic HTTP error exception
*
* @package midgardmvc_core
*/
class midgardmvc_exception_httperror extends midgardmvc_exception
{
// Redefine the exception so message isn't optional
public function __construct($message, $code = 500)
{
parent::__construct($message, $code);
}
}
set_exception_handler(array('midgardmvc_core_exceptionhandler', 'handle'));
?>