page: move custom error handling to include
[minimedit.git] / error.inc.php
1 <?php
2 # custom error handling for MinimEdit
3
4 function abort($body, $status = NULL)
5 {
6         if ($status) header("HTTP/1.1 $status");
7         print "$body\n";
8         exit;
9 }
10
11 function fail($error)
12 {
13         global $User, $Page;
14         http_response_code(500);
15         if (!isset($Page)) {
16                 require_once('article.inc.php');
17                 $Page = new ArchiveArticle(NULL);
18                 $Page->title = 'Fout';
19         }
20         include_once 'page.inc.php';
21
22         ob_start();
23         require '500.inc.html';
24         $Page->place['debug'] = htmlspecialchars($error);
25         $Page->raw = ob_get_clean();
26         print $Page->render();
27 }
28
29 set_exception_handler('fail');
30
31 define('E_FATAL', E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
32
33 set_error_handler(function ($level, $error, $file, $line) {
34         if ($level & E_FATAL) {
35                 fail($error);
36                 return;
37         }
38         return FALSE;
39 });
40
41 register_shutdown_function(function () {
42         # display failure page for fatal exceptions
43         $error = error_get_last();
44         if (!($error['type'] & E_FATAL)) return;
45         fail("Fatal: $error[message] in $error[file]:$error[line]");
46 });
47
48 error_reporting(error_reporting() & ~E_FATAL);
49