Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of object overview in exception message #309

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Entity/Aggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function __construct($entity, array $entityIdentifierValues)
$this->objectID = get_class($this->entity).'::'.reset($entityIdentifierValues);
}

public function getObjectID()
{
return $this->objectID;
}

/**
* Returns the entities class names that should be aggregated.
*
Expand Down
43 changes: 40 additions & 3 deletions src/IndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ public function index($entities, ObjectManager $objectManager)
$this->remove($entitiesToBeRemoved, $objectManager);
}

return $this->forEachChunk($objectManager, $entitiesToBeIndexed, function ($chunk) {
return $this->engine->update($chunk);
});
try {
return $this->forEachChunk($objectManager, $entitiesToBeIndexed, function ($chunk) {
return $this->engine->update($chunk);
});
} catch (\Exception $e) {
$message = $this->formatIndexingException($e, $entitiesToBeIndexed);

throw new \Exception($message);
}
}

public function remove($entities, ObjectManager $objectManager)
Expand Down Expand Up @@ -335,4 +341,35 @@ private function removePrefixFromIndexName($indexName)
{
return preg_replace('/^'.preg_quote($this->configuration['prefix'], '/').'/', '', $indexName);
}

private function formatIndexingException($exception, $entitiesToBeIndexed)
{
// Retrieve the objectID if returned by the API
preg_match('/objectID=(.*?)\sis\stoo\sbig/', $exception->getMessage(), $matches);

if (count($matches) === 0) {
return $exception->getMessage();
}

$objectId = $matches[1];
$entityId = null;
$faultyEntity = null;
$detailedInfos = '';
foreach ($entitiesToBeIndexed as $entityToBeIndexed) {
if (get_parent_class($entityToBeIndexed) === Aggregator::class) {
$entityId = $entityToBeIndexed->getObjectID();
} else {
$entityId = (string) $entityToBeIndexed->getId();
}

if ($entityId === $objectId) {
$faultyEntity = $this->normalizer->serialize($entityToBeIndexed, 'json');
$truncatedEntity = strlen($faultyEntity) > 300 ?
substr($faultyEntity, 0, 300) . '...' : $faultyEntity;
$detailedInfos = "\nObject overview: " . $truncatedEntity;
}
}

return $exception->getMessage() . $detailedInfos;
}
}
41 changes: 41 additions & 0 deletions tests/TestCase/IndexManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,45 @@ public function testIndexIfCondition()
// The content aggregator expects 3 ( not 4, because of the index_if condition ).
$this->assertEquals(3, $this->indexManager->count('', ContentAggregator::class));
}

public function testIndexOversizedEntityData()
{
$this->expectException('Exception');
$this->expectExceptionMessageRegExp('/objectID=10/');
$this->expectExceptionMessageRegExp('/Object overview/');
$om = $this->get('doctrine')->getManager();

$posts = [];
for ($i = 0; $i < 3; $i++) {
$posts[] = $this->createPost($i);
}

$post = $this->createPost(10);
$post->setTitle(str_repeat('Foo',10000));
$posts[] = $post;

$this->indexManager->index($posts, $om);
}

/**
* @expectedException \Exception
*/
public function testIndexOversizedAggregatorData()
{
$this->expectException('Exception');
$this->expectExceptionMessageRegExp('/objectID=Algolia\SearchBundle\TestApp\Entity\Image::1/');
$this->expectExceptionMessageRegExp('/Object overview/');
$om = $this->get('doctrine')->getManager();

$posts = [];
for ($i = 0; $i < 3; $i++) {
$posts[] = $this->createPost($i);
}

$comment = $this->createComment(1);
$image = $this->createImage(1);
$image->setUrl(str_repeat('Foo',10000));

$this->indexManager->index(array_merge($posts, [$comment, $image]), $om);
}
}