Add ServerExceptionProblem insight

This commit is contained in:
2026-04-30 21:33:07 +00:00
parent 1d09358e7b
commit 423c6d3963
3 changed files with 110 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
<?php
namespace IndifferentKetchup\Codex\Analysis\ProjectZomboid;
use IndifferentKetchup\Codex\Analysis\InsightInterface;
use IndifferentKetchup\Codex\Analysis\PatternInsightInterface;
use IndifferentKetchup\Codex\Analysis\Problem;
use IndifferentKetchup\Codex\Pattern\ProjectZomboid\DebugServerPattern;
class ServerExceptionProblem extends Problem implements PatternInsightInterface
{
private string $exceptionType = '';
private string $body = '';
public static function getPatterns(): array
{
return [DebugServerPattern::EXCEPTION];
}
public function setMatches(array $matches, mixed $patternKey): void
{
$this->exceptionType = $matches['type'];
$this->body = trim($matches['body'] ?? '');
}
public function getExceptionType(): string
{
return $this->exceptionType;
}
public function getBody(): string
{
return $this->body;
}
public function getMessage(): string
{
return sprintf('Exception thrown: %s', $this->exceptionType);
}
public function isEqual(InsightInterface $insight): bool
{
return $insight instanceof self
&& $insight->getExceptionType() === $this->exceptionType;
}
}

View File

@@ -24,4 +24,6 @@ class DebugServerPattern
public const string MOD_MISSING = '/required mod "(?<mod>[^"]+)" not found/';
public const string EXCEPTION_HEADER = '/^\[\d{2}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}\]\s+ERROR:.*Exception thrown/';
public const string EXCEPTION = '/^\[\d{2}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}\][^\n]+Exception thrown\n\t(?<type>[A-Za-z0-9_.$]+(?:Exception|Error))[^\n]*(?<body>(?:\n\t.+)*)/';
}

View File

@@ -0,0 +1,62 @@
<?php
namespace IndifferentKetchup\Codex\Test\Tests\Games\ProjectZomboid\Analysis;
use IndifferentKetchup\Codex\Analysis\ProjectZomboid\ServerExceptionProblem;
use IndifferentKetchup\Codex\Pattern\ProjectZomboid\DebugServerPattern;
use PHPUnit\Framework\TestCase;
class ServerExceptionProblemTest extends TestCase
{
public function testGetPatternsReturnsTheExceptionRegex(): void
{
$this->assertSame([DebugServerPattern::EXCEPTION], ServerExceptionProblem::getPatterns());
}
public function testSetMatchesCapturesTypeAndBodyAcrossLines(): void
{
$entryText = "[16-04-26 00:01:19.080] ERROR: General f:0, t:1776297679080, st:48,648,194,258> DebugFileWatcher.registerDir> Exception thrown\n"
. "\tjava.nio.file.NoSuchFileException: /placeholder/config/mods at UnixException.translateToIOException(null:-1).\n"
. "\tStack trace:\n"
. "\t\tjava.base/sun.nio.fs.UnixException.translateToIOException(Unknown Source)";
$this->assertSame(1, preg_match(DebugServerPattern::EXCEPTION, $entryText, $matches));
$problem = new ServerExceptionProblem();
$problem->setMatches($matches, 0);
$this->assertSame('java.nio.file.NoSuchFileException', $problem->getExceptionType());
$this->assertStringContainsString('Stack trace', $problem->getBody());
$this->assertStringContainsString('java.base/sun.nio.fs.UnixException', $problem->getBody());
}
public function testIsEqualCoalescesSameTypeRegardlessOfBody(): void
{
$a = $this->problemFor('java.io.IOException', 'body one');
$b = $this->problemFor('java.io.IOException', 'body two completely different');
$c = $this->problemFor('java.lang.RuntimeException', 'body one');
$this->assertTrue($a->isEqual($b));
$this->assertFalse($a->isEqual($c));
}
public function testNestedExceptionTypeNamesAreSupported(): void
{
$entryText = "[16-04-26 00:01:45.937] ERROR: WorldGen f:0, t:1776297705937, st:48,648,221,115> IsoPropertyType.lookupOrDefaultStr> Exception thrown\n"
. "\tzombie.core.properties.IsoPropertyType\$IsoPropertyTypeNotFoundException: Property Name not found: ladderW";
$this->assertSame(1, preg_match(DebugServerPattern::EXCEPTION, $entryText, $matches));
$problem = new ServerExceptionProblem();
$problem->setMatches($matches, 0);
$this->assertSame('zombie.core.properties.IsoPropertyType$IsoPropertyTypeNotFoundException', $problem->getExceptionType());
}
private function problemFor(string $type, string $body): ServerExceptionProblem
{
$problem = new ServerExceptionProblem();
$problem->setMatches(['type' => $type, 'body' => $body], 0);
return $problem;
}
}