All pocketmine\thread\Thread now log uncaught exceptions and fatal errors by default

This commit is contained in:
Dylan K. Taylor
2023-08-08 14:56:54 +01:00
parent 2e58387a43
commit 2559d1719f
3 changed files with 15 additions and 12 deletions

View File

@ -115,6 +115,7 @@ trait CommonThreadPartsTrait{
protected function onUncaughtException(\Throwable $e) : void{
$this->synchronized(function() use ($e) : void{
$this->crashInfo = ThreadCrashInfo::fromThrowable($e, $this->getThreadName());
\GlobalLogger::get()->logException($e);
});
}
@ -128,11 +129,22 @@ trait CommonThreadPartsTrait{
$last = error_get_last();
if($last !== null){
//fatal error
$this->crashInfo = ThreadCrashInfo::fromLastErrorInfo($last, $this->getThreadName());
$crashInfo = ThreadCrashInfo::fromLastErrorInfo($last, $this->getThreadName());
}else{
//probably misused exit()
$this->crashInfo = ThreadCrashInfo::fromThrowable(new \RuntimeException("Thread crashed without an error - perhaps exit() was called?"), $this->getThreadName());
$crashInfo = ThreadCrashInfo::fromThrowable(new \RuntimeException("Thread crashed without an error - perhaps exit() was called?"), $this->getThreadName());
}
$this->crashInfo = $crashInfo;
$lines = [];
//mimic exception printed format
$lines[] = "Fatal error: " . $crashInfo->makePrettyMessage();
$lines[] = "--- Stack trace ---";
foreach($crashInfo->getTrace() as $frame){
$lines[] = " " . $frame->getPrintableFrame();
}
$lines[] = "--- End of fatal error information ---";
\GlobalLogger::get()->critical(implode("\n", $lines));
}
});
}