@@ -160,6 +160,9 @@ function (array $addressBookInfo) use ($user, $output) {
160160 )));
161161 }
162162
163+ /**
164+ * @throws InvalidAddressBookException
165+ */
163166 private function getUniqueAddressBookUri (IUser $ user , string $ initialAddressBookUri ): string {
164167 $ principalUri = $ this ->getPrincipalUri ($ user );
165168
@@ -168,7 +171,7 @@ private function getUniqueAddressBookUri(IUser $user, string $initialAddressBook
168171 : ContactsMigrator::MIGRATED_URI_PREFIX . $ initialAddressBookUri ;
169172
170173 if ($ initialAddressBookUri === '' ) {
171- throw new ContactsMigratorException ( ' Failed to get unique address book URI ' );
174+ throw new InvalidAddressBookException ( );
172175 }
173176
174177 $ existingAddressBookUris = array_map (
@@ -272,6 +275,8 @@ private function importContact(int $addressBookId, VCard $vCard, string $filenam
272275 /**
273276 * @param array{displayName: string, description?: string} $metadata
274277 * @param VCard[] $vCards
278+ *
279+ * @throws InvalidAddressBookException
275280 */
276281 private function importAddressBook (IUser $ user , string $ filename , string $ initialAddressBookUri , array $ metadata , array $ vCards , OutputInterface $ output ): void {
277282 $ principalUri = $ this ->getPrincipalUri ($ user );
@@ -366,24 +371,29 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
366371
367372 $ splitFilename = explode ('. ' , $ addressBookFilename , 2 );
368373 if (count ($ splitFilename ) !== 2 ) {
369- throw new ContactsMigratorException ("Invalid filename \"$ addressBookFilename \", expected filename of the format \"<address_book_name>. " . ContactsMigrator::FILENAME_EXT . '" ' );
374+ $ output ->writeln ("Invalid filename \"$ addressBookFilename \", expected filename of the format \"<address_book_name>. " . ContactsMigrator::FILENAME_EXT . '", skipping… ' );
375+ continue ;
370376 }
371377 [$ initialAddressBookUri , $ ext ] = $ splitFilename ;
372378
373379 /** @var array{displayName: string, description?: string} $metadata */
374380 $ metadata = json_decode ($ importSource ->getFileContents ($ metadataImportPath ), true , 512 , JSON_THROW_ON_ERROR );
375381
376- $ this ->importAddressBook (
377- $ user ,
378- $ addressBookFilename ,
379- $ initialAddressBookUri ,
380- $ metadata ,
381- $ vCards ,
382- $ output ,
383- );
384-
385- foreach ($ vCards as $ vCard ) {
386- $ vCard ->destroy ();
382+ try {
383+ $ this ->importAddressBook (
384+ $ user ,
385+ $ addressBookFilename ,
386+ $ initialAddressBookUri ,
387+ $ metadata ,
388+ $ vCards ,
389+ $ output ,
390+ );
391+ } catch (InvalidAddressBookException $ e ) {
392+ // Allow this exception to skip a failed import
393+ } finally {
394+ foreach ($ vCards as $ vCard ) {
395+ $ vCard ->destroy ();
396+ }
387397 }
388398 }
389399 }
0 commit comments