Elgg  Version master
ErrorHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Application;
4 
9 
15 class ErrorHandler {
16 
17  use Loggable;
18 
40  public function __invoke($errno, $errmsg, $filename = '', $linenum = 0) {
41  $error = date('Y-m-d H:i:s (T)') . ": \"{$errmsg}\" in file {$filename} (line {$linenum})";
42 
43  // check if the error wasn't suppressed by the error control operator (@)
44  // error_reporting === 0 for PHP < 8.0
45  $reporting_disabled = (error_reporting() === 0) || !(error_reporting() & $errno);
46 
47  switch ($errno) {
48  case E_USER_ERROR:
49  $this->log(LogLevel::ERROR, "PHP ERROR: {$error}");
50 
52  Application::$_instance->internal_services->system_messages->addErrorMessage("ERROR: {$error}");
53  }
54 
55  // Since this is a fatal error, we want to stop any further execution but do so gracefully.
56  throw new ErrorException($error, 0, $errno, $filename, $linenum);
57 
58  case E_WARNING:
59  case E_USER_WARNING:
60  case E_RECOVERABLE_ERROR: // (e.g. type hint violation)
61  if (!$reporting_disabled) {
62  $this->log(LogLevel::WARNING, "PHP: {$error}");
63  }
64  break;
65 
66  default:
67  if (!$reporting_disabled) {
68  $this->log(LogLevel::NOTICE, "PHP NOTICE: {$error}");
69  }
70  break;
71  }
72 
73  return true;
74  }
75 }
static $_instance
Reference to the loaded Application.
Definition: Application.php:72
static isCoreLoaded()
Are Elgg&#39;s global functions loaded?
Error exception, when converting an error into an exception.
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
$error
Bad request error.
Definition: 400.php:6
__invoke($errno, $errmsg, $filename= '', $linenum=0)
Intercepts catchable PHP errors.
Handle system and PHP errors.
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:58