Move exception printing utilities from MainLogger to Utils

where they can be useful to other stuff apart from just the logger
This commit is contained in:
Dylan K. Taylor 2021-10-14 15:54:20 +01:00
parent bdbfa70558
commit 06e7338ff9
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 57 additions and 46 deletions

View File

@ -24,15 +24,10 @@ declare(strict_types=1);
namespace pocketmine\utils;
use LogLevel;
use pocketmine\errorhandler\ErrorTypeToStringMap;
use pocketmine\thread\Thread;
use pocketmine\thread\Worker;
use function get_class;
use function implode;
use function is_int;
use function preg_replace;
use function sprintf;
use function trim;
use const PHP_EOL;
use const PTHREADS_INHERIT_NONE;
@ -137,46 +132,11 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{
* @return void
*/
public function logException(\Throwable $e, $trace = null){
if($trace === null){
$trace = $e->getTrace();
}
$lines = [self::printExceptionMessage($e)];
$lines[] = "--- Stack trace ---";
foreach(Utils::printableTrace($trace) as $line){
$lines[] = " " . $line;
}
for($prev = $e->getPrevious(); $prev !== null; $prev = $prev->getPrevious()){
$lines[] = "--- Previous ---";
$lines[] = self::printExceptionMessage($prev);
foreach(Utils::printableTrace($prev->getTrace()) as $line){
$lines[] = " " . $line;
}
}
$lines[] = "--- End of exception information ---";
$this->critical(implode("\n", $lines));
$this->critical(implode("\n", Utils::printableExceptionInfo($e, $trace)));
$this->syncFlushBuffer();
}
private static function printExceptionMessage(\Throwable $e) : string{
$errstr = preg_replace('/\s+/', ' ', trim($e->getMessage()));
$errno = $e->getCode();
if(is_int($errno)){
try{
$errno = ErrorTypeToStringMap::get($errno);
}catch(\InvalidArgumentException $ex){
//pass
}
}
$errfile = Filesystem::cleanPath($e->getFile());
$errline = $e->getLine();
return get_class($e) . ": \"$errstr\" ($errno) in \"$errfile\" at line $errline";
}
public function log($level, $message){
switch($level){
case LogLevel::EMERGENCY:

View File

@ -28,6 +28,7 @@ declare(strict_types=1);
namespace pocketmine\utils;
use DaveRandom\CallbackValidator\CallbackType;
use pocketmine\errorhandler\ErrorTypeToStringMap;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use function array_combine;
@ -46,6 +47,7 @@ use function file;
use function file_exists;
use function file_get_contents;
use function function_exists;
use function get_class;
use function get_current_user;
use function get_loaded_extensions;
use function getenv;
@ -53,6 +55,7 @@ use function gettype;
use function implode;
use function is_array;
use function is_bool;
use function is_int;
use function is_object;
use function is_string;
use function mb_check_encoding;
@ -384,6 +387,49 @@ final class Utils{
return -1;
}
private static function printableExceptionMessage(\Throwable $e) : string{
$errstr = preg_replace('/\s+/', ' ', trim($e->getMessage()));
$errno = $e->getCode();
if(is_int($errno)){
try{
$errno = ErrorTypeToStringMap::get($errno);
}catch(\InvalidArgumentException $ex){
//pass
}
}
$errfile = Filesystem::cleanPath($e->getFile());
$errline = $e->getLine();
return get_class($e) . ": \"$errstr\" ($errno) in \"$errfile\" at line $errline";
}
/**
* @param mixed[] $trace
* @return string[]
*/
public static function printableExceptionInfo(\Throwable $e, $trace = null) : array{
if($trace === null){
$trace = $e->getTrace();
}
$lines = [self::printableExceptionMessage($e)];
$lines[] = "--- Stack trace ---";
foreach(Utils::printableTrace($trace) as $line){
$lines[] = " " . $line;
}
for($prev = $e->getPrevious(); $prev !== null; $prev = $prev->getPrevious()){
$lines[] = "--- Previous ---";
$lines[] = self::printableExceptionMessage($prev);
foreach(Utils::printableTrace($prev->getTrace()) as $line){
$lines[] = " " . $line;
}
}
$lines[] = "--- End of exception information ---";
return $lines;
}
/**
* @param mixed[][] $trace
* @phpstan-param list<array<string, mixed>> $trace

View File

@ -180,21 +180,26 @@ parameters:
count: 1
path: ../../../src/utils/Internet.php
-
message: "#^Part \\$errno \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: ../../../src/utils/MainLogger.php
-
message: "#^Parameter \\#1 \\$path of static method pocketmine\\\\utils\\\\Filesystem\\:\\:cleanPath\\(\\) expects string, mixed given\\.$#"
count: 1
path: ../../../src/utils/Utils.php
-
message: "#^Parameter \\#1 \\$trace of static method pocketmine\\\\utils\\\\Utils\\:\\:printableTrace\\(\\) expects array\\<int, array\\<string, mixed\\>\\>, array given\\.$#"
count: 1
path: ../../../src/utils/Utils.php
-
message: "#^Parameter \\#2 \\$array of function array_map expects array, mixed given\\.$#"
count: 1
path: ../../../src/utils/Utils.php
-
message: "#^Part \\$errno \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: ../../../src/utils/Utils.php
-
message: "#^Parameter \\#1 \\$keys of function array_fill_keys expects array, mixed given\\.$#"
count: 1