Stop user-data errors leaking to Sentry from migrations

Convert per-resource user-error throws into setStatus + addError + return false
in the create*() methods (createDatabase, createEntity, createField,
createIndex, createRecord). Failures are recorded for the migration report
where they're detected, and the import loop continues to the next resource —
matching the existing skip-from-cache pattern at line 1015 and the
documents-DB skip at line 535.

The per-resource wrap at import() simplifies to a pure safety net: record
for the user, then rethrow. By construction, anything reaching it is a
library bug or infra failure, which the worker boundary routes to Sentry.

Row-flush DuplicateException / StructureException are caught at the flush
site and recorded inline — same shape as the rest of the create*() methods.

Behaviour preserved: user errors continue to appear in the migration report,
the loop continues across resources, the migration ends with status=failed
when any resource failed. Only routing to Sentry changed.
This commit is contained in:
Prem Palanisamy committed 2026-05-04 08:20:31 +01:00
1 parent 39a249231c
commit 276e7c2507
1 file changed
+93 -54
+93 -54
View File
@@ -333,6 +333,10 @@ class Appwrite extends Destination
default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION),
};
} catch (\Throwable $e) {
// Safety net: expected user-data failures are recorded inline by the
// create*() methods via setStatus + addError + return false. Anything
// reaching here is a library bug or infra failure — record it for the
// user, then rethrow so the worker boundary can route it to Sentry.
$resource->setStatus(Resource::STATUS_ERROR, $e->getMessage());
$this->addError(new Exception(
@@ -344,14 +348,7 @@ class Appwrite extends Destination
previous: $e
));
// User-facing failures (Migration\Exception) are recorded for the migration
// report and the loop continues. Anything else is a library bug or infra
// failure — rethrow so the worker boundary can route it to Sentry.
if (!$e instanceof Exception) {
throw $e;
}
$responseResource = $resource;
throw $e;
} finally {
$this->dbForProject->setPreserveDates(false);
}
@@ -421,12 +418,14 @@ class Appwrite extends Destination
$validator = new UID();
if (!$validator->isValid($resource->getId())) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, $validator->getDescription());
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: $validator->getDescription(),
);
));
return false;
}
$createdAt = $this->normalizeDateTime($resource->getCreatedAt());
@@ -481,12 +480,14 @@ class Appwrite extends Destination
$validator = new UID();
if (!$validator->isValid($resource->getId())) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, $validator->getDescription());
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: $validator->getDescription(),
);
));
return false;
}
$database = $this->dbForProject->getDocument(
@@ -495,12 +496,14 @@ class Appwrite extends Destination
);
if ($database->isEmpty()) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Database not found');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Database not found',
);
));
return false;
}
$createdAt = $this->normalizeDateTime($resource->getCreatedAt());
@@ -582,12 +585,14 @@ class Appwrite extends Destination
);
if ($database->isEmpty()) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Database not found');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Database not found',
);
));
return false;
}
$table = $this->dbForProject->getDocument(
@@ -596,41 +601,49 @@ class Appwrite extends Destination
);
if ($table->isEmpty()) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Table not found');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Table not found',
);
));
return false;
}
if (!empty($resource->getFormat())) {
if (!Structure::hasFormat($resource->getFormat(), $type)) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, "Format {$resource->getFormat()} not available for column type {$type}");
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: "Format {$resource->getFormat()} not available for column type {$type}",
);
));
return false;
}
}
if ($resource->isRequired() && $resource->getDefault() !== null) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Cannot set default value for required column');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Cannot set default value for required column',
);
));
return false;
}
if ($resource->isArray() && $resource->getDefault() !== null) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Cannot set default value for array column');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Cannot set default value for array column',
);
));
return false;
}
if ($type === UtopiaDatabase::VAR_RELATIONSHIP) {
@@ -640,12 +653,14 @@ class Appwrite extends Destination
$resource->getOptions()['relatedCollection']
);
if ($relatedTable->isEmpty()) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Related table not found');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Related table not found',
);
));
return false;
}
}
@@ -678,20 +693,26 @@ class Appwrite extends Destination
$this->dbForProject->checkAttribute($table, $column);
$column = $this->dbForProject->createDocument('attributes', $column);
} catch (DuplicateException) {
throw new Exception(
} catch (DuplicateException $e) {
$resource->setStatus(Resource::STATUS_ERROR, 'Attribute already exists');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Attribute already exists',
);
} catch (LimitException) {
throw new Exception(
previous: $e,
));
return false;
} catch (LimitException $e) {
$resource->setStatus(Resource::STATUS_ERROR, 'Attribute limit exceeded');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Attribute limit exceeded',
);
previous: $e,
));
return false;
} catch (\Throwable $e) {
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $table->getId());
$dbForDatabases->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $table->getSequence());
@@ -734,24 +755,30 @@ class Appwrite extends Destination
]);
$this->dbForProject->createDocument('attributes', $twoWayAttribute);
} catch (DuplicateException) {
} catch (DuplicateException $e) {
$this->dbForProject->deleteDocument('attributes', $column->getId());
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Attribute already exists');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Attribute already exists',
);
} catch (LimitException) {
previous: $e,
));
return false;
} catch (LimitException $e) {
$this->dbForProject->deleteDocument('attributes', $column->getId());
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Column limit exceeded');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Column limit exceeded',
);
previous: $e,
));
return false;
} catch (\Throwable $e) {
$this->dbForProject->purgeCachedDocument('database_' . $database->getSequence(), $relatedTable->getId());
$dbForDatabases->purgeCachedCollection('database_' . $database->getSequence() . '_collection_' . $relatedTable->getSequence());
@@ -832,12 +859,14 @@ class Appwrite extends Destination
$resource->getTable()->getDatabase()->getId(),
);
if ($database->isEmpty()) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Database not found');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Database not found',
);
));
return false;
}
$table = $this->dbForProject->getDocument(
@@ -845,12 +874,14 @@ class Appwrite extends Destination
$resource->getTable()->getId(),
);
if ($table->isEmpty()) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Table not found');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Table not found',
);
));
return false;
}
$dbForDatabases = ($this->getDatabasesDB)($database);
@@ -860,12 +891,14 @@ class Appwrite extends Destination
], $dbForDatabases->getLimitForIndexes());
if ($count >= $dbForDatabases->getLimitForIndexes()) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Index limit reached for table');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Index limit reached for table',
);
));
return false;
}
// Lengths hidden by default
@@ -922,12 +955,14 @@ class Appwrite extends Destination
if (!$validator->isValid($index)) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Invalid index: ' . $validator->getDescription());
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Invalid index: ' . $validator->getDescription(),
);
));
return false;
}
$index = $this->dbForProject->createDocument('indexes', $index);
@@ -984,12 +1019,14 @@ class Appwrite extends Destination
$validator = new UID();
if (!$validator->isValid($resource->getId())) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, $validator->getDescription());
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: $validator->getDescription(),
);
));
return false;
}
// Check if document has already been created
@@ -1096,23 +1133,25 @@ class Appwrite extends Destination
),
};
} catch (DuplicateException $e) {
// Convert to Migration\Exception so the per-resource wrap recognises it
// as a user-facing failure and records it without escalating to Sentry.
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, 'Document already exists');
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Document already exists',
previous: $e,
);
));
return false;
} catch (StructureException $e) {
throw new Exception(
$resource->setStatus(Resource::STATUS_ERROR, $e->getMessage());
$this->addError(new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: $e->getMessage(),
previous: $e,
);
));
return false;
}
} finally {
$this->rowBuffer = [];