Skip to content

Instantly share code, notes, and snippets.

@codexp

codexp/Error.php

Created Oct 19, 2015
Embed
What would you like to do?
F3 Error Controller for set_error_handler()
<?php
namespace CodeXP\Controller;
use CodeXP\Controller\Base\Page;
use Psr\Log\LogLevel;
/**
* Error Controller
*
* @author codexp
* @version v0.0.1-2015.10.11
* @created 11.10.2015
*/
class Error extends Page
{
protected static $_ERROR = array(
'undefined' => 'UNDEFINED',
E_ERROR => 'E_ERROR',
E_WARNING => 'E_WARNING',
E_PARSE => 'E_PARSE',
E_NOTICE => 'E_NOTICE',
E_CORE_ERROR => 'E_CORE_ERROR',
E_CORE_WARNING => 'E_CORE_WARNING',
E_COMPILE_ERROR => 'E_COMPILE_ERROR',
E_COMPILE_WARNING => 'E_COMPILE_WARNING',
E_USER_ERROR => 'E_USER_ERROR',
E_USER_WARNING => 'E_USER_WARNING',
E_USER_NOTICE => 'E_USER_NOTICE',
E_STRICT => 'E_STRICT',
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
E_DEPRECATED => 'E_DEPRECATED',
E_USER_DEPRECATED => 'E_USER_DEPRECATED',
);
public function display(\Base $f3)
{
if (DEVELOPMENT) {
$trace = debug_backtrace(FALSE);
$error = $f3->get('ERROR');
unset($error['trace']);
r($error);
\ref::config('expLvl', 2);
r($trace);
} else {
// print default error
return false;
}
}
public static function php_error($type, $message, $file, $line, $vars)
{
if (isset(static::$_ERROR[$type])) {
$typeStr = static::$_ERROR[$type];
} else {
$typeStr = static::$_ERROR['undefined'];
}
switch($type) {
case E_PARSE: // 4
case E_COMPILE_ERROR: // 64
$level = LogLevel::EMERGENCY;
break;
default:
case E_ERROR: // 1
case E_CORE_ERROR: // 16
case E_USER_ERROR: // 256
case E_RECOVERABLE_ERROR: // 4096
$level = LogLevel::ERROR;
break;
case E_WARNING: // 2
case E_CORE_WARNING: // 32
case E_COMPILE_WARNING: // 128
case E_USER_WARNING: // 512
$level = LogLevel::WARNING;
break;
case E_NOTICE: // 8
case E_USER_NOTICE: // 1024
case E_STRICT: // 2048
case E_DEPRECATED: // 8192
case E_USER_DEPRECATED: // 16384
$level = LogLevel::NOTICE;
break;
}
$f3 = \Base::instance();
$f3['LOGGER']->log(
$level,
"[{date}] {type}: {message} ({file}:{line})",
array(
'date' => new \DateTime(),
'type' => $typeStr,
'message' => $message,
'file' => $file,
'line' => $line,
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.