Skip to content

Commit a64a9fc

Browse files
committed
Improve error handling
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 0cbb6d7 commit a64a9fc

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

apps/dav/lib/UserMigration/CalendarMigrator.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ private function getPrincipalUri(IUser $user): string {
106106
* @throws CalendarMigratorException
107107
* @throws InvalidCalendarException
108108
*/
109-
private function getCalendarExportData(IUser $user, ICalendar $calendar): array {
109+
private function getCalendarExportData(IUser $user, ICalendar $calendar, OutputInterface $output): array {
110110
$userId = $user->getUID();
111111
$calendarId = $calendar->getKey();
112112
$calendarInfo = $this->calDavBackend->getCalendarById($calendarId);
113113

114114
if (empty($calendarInfo)) {
115-
throw new CalendarMigratorException("Invalid info for calendar ID: $calendarId");
115+
throw new CalendarMigratorException("Invalid info for calendar ID $calendarId");
116116
}
117117

118118
$uri = $calendarInfo['uri'];
@@ -155,6 +155,12 @@ private function getCalendarExportData(IUser $user, ICalendar $calendar): array
155155
$blobs,
156156
);
157157

158+
$problems = $mergedCalendar->validate();
159+
if (!empty($problems)) {
160+
$output->writeln('Skipping calendar "' . $properties['{DAV:}displayname'] . '" containing invalid calendar data');
161+
throw new InvalidCalendarException();
162+
}
163+
158164
return [
159165
'name' => $calendarNode->getName(),
160166
'vCalendar' => $mergedCalendar,
@@ -166,13 +172,13 @@ private function getCalendarExportData(IUser $user, ICalendar $calendar): array
166172
*
167173
* @throws CalendarMigratorException
168174
*/
169-
private function getCalendarExports(IUser $user): array {
175+
private function getCalendarExports(IUser $user, OutputInterface $output): array {
170176
$principalUri = $this->getPrincipalUri($user);
171177

172178
return array_values(array_filter(array_map(
173-
function (ICalendar $calendar) use ($user) {
179+
function (ICalendar $calendar) use ($user, $output) {
174180
try {
175-
return $this->getCalendarExportData($user, $calendar);
181+
return $this->getCalendarExportData($user, $calendar, $output);
176182
} catch (InvalidCalendarException $e) {
177183
// Allow this exception as invalid (e.g. deleted) calendars are not to be exported
178184
return null;
@@ -213,12 +219,10 @@ private function getUniqueCalendarUri(IUser $user, string $initialCalendarUri):
213219
public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
214220
$output->writeln('Exporting calendars into ' . CalendarMigrator::EXPORT_ROOT . '');
215221

216-
$userId = $user->getUID();
217-
218-
$calendarExports = $this->getCalendarExports($user);
222+
$calendarExports = $this->getCalendarExports($user, $output);
219223

220224
if (empty($calendarExports)) {
221-
$output->writeln("User <$userId> has no calendars to export");
225+
$output->writeln('No calendars to export');
222226
}
223227

224228
/**
@@ -414,7 +418,12 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
414418

415419
$output->writeln('Importing calendars from ' . CalendarMigrator::EXPORT_ROOT . '');
416420

417-
foreach ($importSource->getFolderListing(CalendarMigrator::EXPORT_ROOT) as $filename) {
421+
$calendarImports = $importSource->getFolderListing(CalendarMigrator::EXPORT_ROOT);
422+
if (empty($calendarImports)) {
423+
$output->writeln('No calendars to import…');
424+
}
425+
426+
foreach ($calendarImports as $filename) {
418427
$importPath = CalendarMigrator::EXPORT_ROOT . $filename;
419428
try {
420429
/** @var VCalendar $vCalendar */
@@ -423,17 +432,17 @@ public function import(IUser $user, IImportSource $importSource, OutputInterface
423432
VObjectReader::OPTION_FORGIVING,
424433
);
425434
} catch (Throwable $e) {
426-
throw new CalendarMigratorException("Failed to read file: \"$importPath\"", 0, $e);
435+
throw new CalendarMigratorException("Failed to read file \"$importPath\"", 0, $e);
427436
}
428437

429438
$problems = $vCalendar->validate();
430439
if (!empty($problems)) {
431-
throw new CalendarMigratorException("Invalid calendar data contained in: \"$importPath\"");
440+
throw new CalendarMigratorException("Invalid calendar data contained in \"$importPath\"");
432441
}
433442

434443
$splitFilename = explode('_', $filename, 2);
435444
if (count($splitFilename) !== 2) {
436-
throw new CalendarMigratorException("Invalid filename: \"$filename\", expected filename of the format: \"<calendar_name>_YYYY-MM-DD" . CalendarMigrator::FILENAME_EXT . '"');
445+
throw new CalendarMigratorException("Invalid filename \"$filename\", expected filename of the format \"<calendar_name>_YYYY-MM-DD" . CalendarMigrator::FILENAME_EXT . '"');
437446
}
438447
[$initialCalendarUri, $suffix] = $splitFilename;
439448

0 commit comments

Comments
 (0)