Hoo man has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/257418

Change subject: Fix dump generation exception handling/ make more robust
......................................................................

Fix dump generation exception handling/ make more robust

This failed in production today because
Wikibase\DataModel\Services\Lookup\EntityLookup::getEntity
throws EntityLookupExceptions rather than StorageExceptions
(unlike Wikibase\Lib\Store\EntityRevisionLookup::getEntityRevision).

In order to make this more robust, let DumpGenerator::dumpEntities
catch both.

Also make the exceptions thrown inside the Rdf-/ JsonDumpGenerators
consistent.

Bug: T120716
Change-Id: Iec181923614a943e130b3e11d1b9f1d385d584d7
(cherry picked from commit 79357e9ee926b7bc844d2eb67a8efe526c0ca407)
---
M repo/includes/Dumpers/DumpGenerator.php
M repo/includes/Dumpers/JsonDumpGenerator.php
M repo/includes/Dumpers/RdfDumpGenerator.php
M repo/tests/phpunit/includes/Dumpers/JsonDumpGeneratorTest.php
4 files changed, 38 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/18/257418/1

diff --git a/repo/includes/Dumpers/DumpGenerator.php 
b/repo/includes/Dumpers/DumpGenerator.php
index 62fcd7e..00ded6a 100644
--- a/repo/includes/Dumpers/DumpGenerator.php
+++ b/repo/includes/Dumpers/DumpGenerator.php
@@ -4,6 +4,7 @@
 
 use InvalidArgumentException;
 use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Services\Lookup\EntityLookupException;
 use Wikibase\DataModel\Services\Entity\EntityPrefetcher;
 use Wikibase\Lib\Reporting\ExceptionHandler;
 use Wikibase\Lib\Reporting\MessageReporter;
@@ -312,6 +313,8 @@
                                if ( $this->limit && $dumpCount >= $this->limit 
) {
                                        break;
                                }
+                       } catch ( EntityLookupException $ex ) {
+                               $this->exceptionHandler->handleException( $ex, 
'failed-to-dump', 'Failed to dump ' . $entityId );
                        } catch ( StorageException $ex ) {
                                $this->exceptionHandler->handleException( $ex, 
'failed-to-dump', 'Failed to dump ' . $entityId );
                        }
@@ -323,6 +326,7 @@
         *
         * @param EntityId $entityId
         *
+        * @throws EntityLookupException
         * @throws StorageException
         * @return string|null
         */
diff --git a/repo/includes/Dumpers/JsonDumpGenerator.php 
b/repo/includes/Dumpers/JsonDumpGenerator.php
index 9eeaf7e..f1b1268 100644
--- a/repo/includes/Dumpers/JsonDumpGenerator.php
+++ b/repo/includes/Dumpers/JsonDumpGenerator.php
@@ -13,9 +13,10 @@
 use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
 use Wikibase\Lib\Serialization\CallbackFactory;
 use Wikibase\Lib\Serialization\SerializationModifier;
+use Wikibase\DataModel\Services\Lookup\EntityLookupException;
 use Wikibase\DataModel\Services\Lookup\RedirectResolvingEntityLookup;
-use Wikibase\Lib\Store\StorageException;
 use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException;
+use Wikibase\Lib\Store\StorageException;
 
 /**
  * JsonDumpGenerator generates an JSON dump of a given set of entities, 
excluding
@@ -124,6 +125,7 @@
        /**
         * @param EntityId $entityId
         *
+        * @throws EntityLookupException
         * @throws StorageException
         *
         * @return string|null
@@ -133,11 +135,12 @@
                        $entity = $this->entityLookup->getEntity( $entityId );
 
                        if ( !$entity ) {
-                               throw new StorageException( 'Entity not found: 
' . $entityId->getSerialization() );
+                               throw new EntityLookupException( $entityId, 
'Entity not found: ' . $entityId->getSerialization() );
                        }
                } catch ( MWContentSerializationException $ex ) {
                        throw new StorageException( 'Deserialization error for 
' . $entityId->getSerialization() );
                } catch ( RevisionedUnresolvedRedirectException $e ) {
+                       // Redirects aren't supposed to be in the JSON dumps
                        return null;
                }
 
diff --git a/repo/includes/Dumpers/RdfDumpGenerator.php 
b/repo/includes/Dumpers/RdfDumpGenerator.php
index 3de9340..fecf9b6 100644
--- a/repo/includes/Dumpers/RdfDumpGenerator.php
+++ b/repo/includes/Dumpers/RdfDumpGenerator.php
@@ -7,11 +7,12 @@
 use MWException;
 use SiteList;
 use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Services\Lookup\EntityLookupException;
 use Wikibase\DataModel\Services\Entity\EntityPrefetcher;
 use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
 use Wikibase\Lib\Store\EntityRevisionLookup;
-use Wikibase\DataModel\Services\Lookup\RedirectResolvingEntityLookup;
 use Wikibase\Lib\Store\StorageException;
+use Wikibase\DataModel\Services\Lookup\RedirectResolvingEntityLookup;
 use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException;
 use Wikibase\Rdf\ValueSnakRdfBuilderFactory;
 use Wikibase\Rdf\HashDedupeBag;
@@ -91,6 +92,7 @@
         *
         * @param EntityId $entityId
         *
+        * @throws EntityLookupException
         * @throws StorageException
         * @return string|null RDF
         */
@@ -99,7 +101,7 @@
                        $entityRevision = 
$this->entityRevisionLookup->getEntityRevision( $entityId );
 
                        if ( !$entityRevision ) {
-                               throw new StorageException( 'Entity not found: 
' . $entityId->getSerialization() );
+                               throw new EntityLookupException( $entityId, 
'Entity not found: ' . $entityId->getSerialization() );
                        }
 
                        $this->rdfBuilder->addEntityRevisionInfo(
diff --git a/repo/tests/phpunit/includes/Dumpers/JsonDumpGeneratorTest.php 
b/repo/tests/phpunit/includes/Dumpers/JsonDumpGeneratorTest.php
index e777f9d..0580d69 100644
--- a/repo/tests/phpunit/includes/Dumpers/JsonDumpGeneratorTest.php
+++ b/repo/tests/phpunit/includes/Dumpers/JsonDumpGeneratorTest.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Test\Dumpers;
 
+use Exception;
 use DataValues\Serializers\DataValueSerializer;
 use InvalidArgumentException;
 use MWContentSerializationException;
@@ -16,6 +17,7 @@
 use Wikibase\DataModel\Services\Entity\NullEntityPrefetcher;
 use Wikibase\DataModel\Entity\BasicEntityIdParser;
 use Wikibase\DataModel\Services\Lookup\EntityLookup;
+use Wikibase\DataModel\Services\Lookup\EntityLookupException;
 use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
 use Wikibase\Dumpers\JsonDumpGenerator;
 use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException;
@@ -196,7 +198,8 @@
         * @dataProvider idProvider
         */
        public function 
testGenerateDump_HandlesMWContentSerializationException( array $ids ) {
-               $jsonDumper = $this->getJsonDumperWithExceptionHandler( $ids );
+               $ex = new MWContentSerializationException( 'cannot 
deserialize!' );
+               $jsonDumper = $this->getJsonDumperWithExceptionHandler( $ids, 
$ex );
                $pager = $this->makeIdPager( $ids );
 
                ob_start();
@@ -207,8 +210,24 @@
                $this->assertEquals( array(), $data );
        }
 
-       private function getJsonDumperWithExceptionHandler( array $ids ) {
-               $entityLookup = 
$this->getEntityLookupThrowsMWContentSerializationException();
+       /**
+        * @dataProvider idProvider
+        */
+       public function testGenerateDump_HandlesEntityLookupException( array 
$ids ) {
+               $ex = new EntityLookupException( new ItemId( 'Q2' ), 'Whatever' 
);
+               $jsonDumper = $this->getJsonDumperWithExceptionHandler( $ids, 
$ex );
+               $pager = $this->makeIdPager( $ids );
+
+               ob_start();
+               $jsonDumper->generateDump( $pager );
+               $json = ob_get_clean();
+
+               $data = json_decode( $json, true );
+               $this->assertEquals( array(), $data );
+       }
+
+       private function getJsonDumperWithExceptionHandler( array $ids, 
Exception $ex ) {
+               $entityLookup = $this->getEntityLookupThrows( $ex );
                $out = fopen( 'php://output', 'w' );
                $serializer = $this->serializerFactory->newEntitySerializer();
 
@@ -247,12 +266,12 @@
        /**
         * @return EntityLookup
         */
-       private function getEntityLookupThrowsMWContentSerializationException() 
{
+       private function getEntityLookupThrows( Exception $ex ) {
                $entityLookup = $this->getMock( 
'Wikibase\DataModel\Services\Lookup\EntityLookup' );
                $entityLookup->expects( $this->any() )
                        ->method( 'getEntity' )
-                       ->will( $this->returnCallback( function( EntityId $id ) 
{
-                               throw new MWContentSerializationException( 
'cannot deserialize!' );
+                       ->will( $this->returnCallback( function( EntityId $id ) 
use ( $ex ) {
+                               throw $ex;
                        } ) );
 
                return $entityLookup;

-- 
To view, visit https://gerrit.wikimedia.org/r/257418
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iec181923614a943e130b3e11d1b9f1d385d584d7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: wmf/1.27.0-wmf.7
Gerrit-Owner: Hoo man <h...@online.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to