Move some error handling stuff to SPL

This commit is contained in:
Dylan K. Taylor 2019-01-17 19:16:03 +00:00
parent e341f3dce2
commit bbc8bc4df5
6 changed files with 20 additions and 93 deletions

16
composer.lock generated
View File

@ -413,12 +413,12 @@
"source": {
"type": "git",
"url": "https://github.com/pmmp/RakLib.git",
"reference": "2f5dfdaa28ff69d72cd1682faa521e18b17a15ef"
"reference": "caffc9bcce765c78d4464b4f6852e3981b81b752"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pmmp/RakLib/zipball/2f5dfdaa28ff69d72cd1682faa521e18b17a15ef",
"reference": "2f5dfdaa28ff69d72cd1682faa521e18b17a15ef",
"url": "https://api.github.com/repos/pmmp/RakLib/zipball/caffc9bcce765c78d4464b4f6852e3981b81b752",
"reference": "caffc9bcce765c78d4464b4f6852e3981b81b752",
"shasum": ""
},
"require": {
@ -446,7 +446,7 @@
"source": "https://github.com/pmmp/RakLib/tree/master",
"issues": "https://github.com/pmmp/RakLib/issues"
},
"time": "2019-01-09T18:42:21+00:00"
"time": "2019-01-17T19:13:53+00:00"
},
{
"name": "pocketmine/snooze",
@ -488,12 +488,12 @@
"source": {
"type": "git",
"url": "https://github.com/pmmp/SPL.git",
"reference": "985e212ac528412ec00ddd3b88c5fc11549a2fe0"
"reference": "d7bd7b8cb10c264ca6e6b5c3ef9b52e48a6f29d8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pmmp/SPL/zipball/985e212ac528412ec00ddd3b88c5fc11549a2fe0",
"reference": "985e212ac528412ec00ddd3b88c5fc11549a2fe0",
"url": "https://api.github.com/repos/pmmp/SPL/zipball/d7bd7b8cb10c264ca6e6b5c3ef9b52e48a6f29d8",
"reference": "d7bd7b8cb10c264ca6e6b5c3ef9b52e48a6f29d8",
"shasum": ""
},
"type": "library",
@ -512,7 +512,7 @@
"support": {
"source": "https://github.com/pmmp/SPL/tree/master"
},
"time": "2018-10-21T17:32:00+00:00"
"time": "2019-01-17T18:54:01+00:00"
}
],
"packages-dev": [],

View File

@ -60,21 +60,6 @@ use function substr;
use function time;
use function zend_version;
use function zlib_encode;
use const E_COMPILE_ERROR;
use const E_COMPILE_WARNING;
use const E_CORE_ERROR;
use const E_CORE_WARNING;
use const E_DEPRECATED;
use const E_ERROR;
use const E_NOTICE;
use const E_PARSE;
use const E_RECOVERABLE_ERROR;
use const E_STRICT;
use const E_USER_DEPRECATED;
use const E_USER_ERROR;
use const E_USER_NOTICE;
use const E_USER_WARNING;
use const E_WARNING;
use const FILE_IGNORE_NEW_LINES;
use const JSON_UNESCAPED_SLASHES;
use const PHP_EOL;
@ -217,26 +202,13 @@ class CrashDump{
}else{
$error = (array) error_get_last();
$error["trace"] = Utils::currentTrace(3); //Skipping CrashDump->baseCrash, CrashDump->construct, Server->crashDump
$errorConversion = [
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"
];
$error["fullFile"] = $error["file"];
$error["file"] = Utils::cleanPath($error["file"]);
$error["type"] = $errorConversion[$error["type"]] ?? $error["type"];
try{
$error["type"] = \ErrorUtils::errorTypeToString($error["type"]);
}catch(\InvalidArgumentException $e){
//pass
}
if(($pos = strpos($error["message"], "\n")) !== false){
$error["message"] = substr($error["message"], 0, $pos);
}

View File

@ -163,7 +163,7 @@ namespace pocketmine {
exit(1);
}
set_error_handler([Utils::class, 'errorExceptionHandler']);
\ErrorUtils::setErrorExceptionHandler();
/*
* We now use the Composer autoloader, but this autoloader is still for loading plugins.

View File

@ -24,12 +24,10 @@ declare(strict_types=1);
namespace pocketmine\scheduler;
use pocketmine\utils\MainLogger;
use pocketmine\utils\Utils;
use pocketmine\Worker;
use function error_reporting;
use function gc_enable;
use function ini_set;
use function set_error_handler;
class AsyncWorker extends Worker{
/** @var mixed[] */
@ -53,7 +51,7 @@ class AsyncWorker extends Worker{
$this->registerClassLoader();
//set this after the autoloader is registered
set_error_handler([Utils::class, 'errorExceptionHandler']);
\ErrorUtils::setErrorExceptionHandler();
\GlobalLogger::set($this->logger);
if($this->logger instanceof MainLogger){

View File

@ -36,19 +36,8 @@ use function sprintf;
use function time;
use function touch;
use function trim;
use const E_COMPILE_ERROR;
use const E_COMPILE_WARNING;
use const E_CORE_ERROR;
use const E_CORE_WARNING;
use const E_DEPRECATED;
use const E_ERROR;
use const E_NOTICE;
use const E_PARSE;
use const E_RECOVERABLE_ERROR;
use const E_STRICT;
use const E_USER_DEPRECATED;
use const E_USER_ERROR;
use const E_USER_NOTICE;
use const E_USER_WARNING;
use const E_WARNING;
use const PHP_EOL;
@ -209,30 +198,16 @@ class MainLogger extends \AttachableThreadedLogger{
$errno = $e->getCode();
$errline = $e->getLine();
$errorConversion = [
0 => "EXCEPTION",
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"
];
if($errno === 0){
$type = LogLevel::CRITICAL;
}else{
$type = ($errno === E_ERROR or $errno === E_USER_ERROR) ? LogLevel::ERROR : (($errno === E_USER_WARNING or $errno === E_WARNING) ? LogLevel::WARNING : LogLevel::NOTICE);
}
$errno = $errorConversion[$errno] ?? $errno;
try{
$errno = \ErrorUtils::errorTypeToString($errno);
}catch(\InvalidArgumentException $e){
//pass
}
$errstr = preg_replace('/\s+/', ' ', trim($errstr));
$errfile = Utils::cleanPath($errfile);

View File

@ -39,7 +39,6 @@ use function chunk_split;
use function count;
use function debug_zval_dump;
use function dechex;
use function error_reporting;
use function exec;
use function explode;
use function fclose;
@ -628,23 +627,6 @@ class Utils{
return array_combine($matches[1], $matches[2]);
}
/**
* @param int $severity
* @param string $message
* @param string $file
* @param int $line
*
* @return bool
* @throws \ErrorException
*/
public static function errorExceptionHandler(int $severity, string $message, string $file, int $line) : bool{
if(error_reporting() & $severity){
throw new \ErrorException($message, 0, $severity, $file, $line);
}
return true; //stfu operator
}
public static function testValidInstance(string $className, string $baseName) : void{
try{
$base = new \ReflectionClass($baseName);