[Store] Skip missing toctree entries gracefully

This commit is contained in:
Johannes Wachter
2026-03-16 18:53:46 +01:00
committed by Christopher Hertel
parent b662357e31
commit b1f6e8a17e
2 changed files with 33 additions and 2 deletions

View File

@@ -11,6 +11,8 @@
namespace Symfony\AI\Store\Document\Loader;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\AI\Store\Document\EmbeddableDocumentInterface;
use Symfony\AI\Store\Document\LoaderInterface;
use Symfony\AI\Store\Exception\InvalidArgumentException;
@@ -25,6 +27,8 @@ final class RstToctreeLoader implements LoaderInterface
{
public function __construct(
private RstLoader $rstLoader = new RstLoader(),
private bool $throwOnMissingEntry = false,
private LoggerInterface $logger = new NullLogger(),
) {
}
@@ -135,7 +139,17 @@ final class RstToctreeLoader implements LoaderInterface
}
} else {
if (!file_exists($pattern)) {
throw new RuntimeException(\sprintf('Toctree entry "%s" resolved to "%s" which does not exist.', $entryPath, $pattern));
if ($this->throwOnMissingEntry) {
throw new RuntimeException(\sprintf('Toctree entry "%s" resolved to "%s" which does not exist.', $entryPath, $pattern));
}
$this->logger->warning('Skipping toctree entry "{entry}" — resolved to "{path}" which does not exist.', [
'entry' => $entryPath,
'path' => $pattern,
]);
++$i;
continue;
}
if (!\in_array($pattern, $entries, true)) {

View File

@@ -242,7 +242,7 @@ final class RstToctreeLoaderTest extends TestCase
file_put_contents($tempDir.'/index.rst', "Title\n=====\n\n.. toctree::\n\n missing_page\n");
try {
$loader = new RstToctreeLoader();
$loader = new RstToctreeLoader(throwOnMissingEntry: true);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('does not exist');
@@ -253,6 +253,23 @@ final class RstToctreeLoaderTest extends TestCase
}
}
public function testLoadToctreeSkipsMissingEntryByDefault()
{
$tempDir = sys_get_temp_dir().'/rst_missing_test_'.uniqid();
mkdir($tempDir, 0777, true);
file_put_contents($tempDir.'/index.rst', "Title\n=====\n\n.. toctree::\n\n missing_page\n");
try {
$loader = new RstToctreeLoader();
$documents = iterator_to_array($loader->load($tempDir.'/index.rst'), false);
$this->assertCount(1, $documents);
} finally {
unlink($tempDir.'/index.rst');
rmdir($tempDir);
}
}
public function testLoadSectionOverflowCreatesMultipleChunks()
{
// Create a temporary file with a very long section