source: trunk/Packages/Common/Error.php

Last change on this file was 95, checked in by chronos, 3 years ago
  • Modified: Updated Common package.
  • Added: Explicit types for better type checking.
  • Fixed: Support for php 8.0.
File size: 3.2 KB
Line 
1<?php
2
3class ErrorHandler
4{
5 public string $Encoding;
6 public bool $ShowError;
7 public int $UserErrors;
8 public $OnError;
9
10 function __construct()
11 {
12 $this->Encoding = 'utf-8';
13 $this->ShowError = false;
14 $this->UserErrors = E_ALL; //E_ERROR | E_WARNING | E_PARSE;
15 $this->OnError = array(array($this, 'ShowDefaultError'));
16 }
17
18 function Start()
19 {
20 set_error_handler(array($this, 'ErrorHandler'));
21 set_exception_handler(array($this, 'ExceptionHandler'));
22 }
23
24 function Stop()
25 {
26 restore_error_handler();
27 restore_exception_handler();
28 }
29
30 function ErrorHandler($Number, $Message, $FileName, $LineNumber, $Variables)
31 {
32 $ErrorType = array
33 (
34 1 => 'Error',
35 2 => 'Warning',
36 4 => 'Parsing Error',
37 8 => 'Notice',
38 16 => 'Core Error',
39 32 => 'Core Warning',
40 64 => 'Compile Error',
41 128 => 'Compile Warning',
42 256 => 'User Error',
43 512 => 'User Warning',
44 1024 => 'User Notice'
45 );
46
47 if (($this->UserErrors & $Number))
48 {
49 // Error was suppressed with the @-operator
50 if (0 === error_reporting())
51 {
52 return false;
53 }
54 $Backtrace = debug_backtrace();
55 $Backtrace[0]['function'] = $Message;
56 $Backtrace[0]['args'] = '';
57 $Backtrace[0]['file'] = $FileName;
58 $Backtrace[0]['line'] = $LineNumber;
59 $this->Report($Backtrace);
60 //if ((E_ERROR | E_PARSE) & $Number)
61 die();
62 }
63 }
64
65 function ExceptionHandler(Throwable $Exception)
66 {
67 $Backtrace = $Exception->getTrace();
68 array_unshift($Backtrace, array(
69 'function' => $Exception->getMessage(),
70 'file' => $Exception->getFile(),
71 'line' => $Exception->getLine(),
72 ));
73 $this->Report($Backtrace);
74 die();
75 }
76
77 function ShowDefaultError(string $Message): void
78 {
79 $Output = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>'."\n".
80 '<meta http-equiv="Content-Language" content="en">'."\n".
81 '<meta http-equiv="Content-Type" content="text/html; charset='.$this->Encoding.'"></head><body>'."\n".
82 'An internal error occurred!<br/>'.
83 'Administrator will be notified and the problem will be investigated and fixed soon.'.'<br/><br/>';
84 if ($this->ShowError == true)
85 $Output .= '<pre>'.$Message.'</pre><br/>';
86 $Output .= '</body></html>';
87 echo($Output);
88 }
89
90 function Report($Backtrace)
91 {
92 $Date = date('Y-m-d H:i:s');
93 $Error = '# '.$Date."\n";
94 foreach ($Backtrace as $Item)
95 {
96 if (!array_key_exists('line', $Item)) $Item['line'] = '';
97 if (!array_key_exists('file', $Item)) $Item['file'] = '';
98
99 $Error .= ' '.$Item['file'].'('.$Item['line'].")\t".$Item['function'];
100 $Arguments = '';
101 if (array_key_exists('args', $Item) and is_array($Item['args']))
102 foreach ($Item['args'] as $Item)
103 {
104 if (is_object($Item)) ;
105 else if (is_array($Item)) $Arguments .= "'".serialize($Item)."',";
106 else $Arguments .= "'".$Item."',";
107 }
108 if (strlen($Arguments) > 0) $Error .= '('.substr($Arguments, 0, -1).')';
109 $Error .= "\n";
110 }
111 $Error .= "\n";
112
113 foreach ($this->OnError as $OnError)
114 call_user_func($OnError, $Error);
115 }
116}
Note: See TracBrowser for help on using the repository browser.