1
0
Fork 0
mirror of synced 2024-07-09 00:16:13 +12:00

Single catch with type check

This commit is contained in:
Jake Barnby 2023-06-15 13:25:25 +12:00
parent 10f06226d3
commit 44e7a822b1
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C

View file

@ -102,7 +102,7 @@ class DatabaseV1 extends Worker
case Database::VAR_RELATIONSHIP: case Database::VAR_RELATIONSHIP:
$relatedCollection = $dbForProject->getDocument('database_' . $database->getInternalId(), $options['relatedCollection']); $relatedCollection = $dbForProject->getDocument('database_' . $database->getInternalId(), $options['relatedCollection']);
if ($relatedCollection->isEmpty()) { if ($relatedCollection->isEmpty()) {
throw new Exception('Collection not found'); throw new DatabaseException('Collection not found');
} }
if ( if (
@ -116,7 +116,7 @@ class DatabaseV1 extends Worker
onDelete: $options['onDelete'], onDelete: $options['onDelete'],
) )
) { ) {
throw new Exception('Failed to create Attribute'); throw new DatabaseException('Failed to create Attribute');
} }
if ($options['twoWay']) { if ($options['twoWay']) {
@ -131,42 +131,27 @@ class DatabaseV1 extends Worker
} }
$dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available')); $dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available'));
} catch (DatabaseException $e) {
Console::error($e->getMessage());
$dbForProject->updateDocument(
'attributes',
$attribute->getId(),
$attribute
->setAttribute('status', 'failed')
->setAttribute('error', $e->getMessage())
);
if ($type === Database::VAR_RELATIONSHIP && $options['twoWay']) {
$relatedAttribute = $dbForProject->getDocument('attributes', $database->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $options['twoWayKey']);
$dbForProject->updateDocument(
'attributes',
$relatedAttribute->getId(),
$relatedAttribute
->setAttribute('status', 'failed')
->setAttribute('error', $e->getMessage());
}
} catch (Exception $e) { } catch (Exception $e) {
Console::error($e->getMessage());
if ($e instanceof DatabaseException) {
$attribute->setAttribute('error', $e->getMessage());
if (isset($relatedAttribute)) {
$relatedAttribute->setAttribute('error', $e->getMessage());
}
}
$dbForProject->updateDocument( $dbForProject->updateDocument(
'attributes', 'attributes',
$attribute->getId(), $attribute->getId(),
$attribute $attribute->setAttribute('status', 'failed')
->setAttribute('status', 'failed')
->setAttribute('error', '')
); );
if ($type === Database::VAR_RELATIONSHIP && $options['twoWay']) { if (isset($relatedAttribute)) {
$relatedAttribute = $dbForProject->getDocument('attributes', $database->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $options['twoWayKey']);
$dbForProject->updateDocument( $dbForProject->updateDocument(
'attributes', 'attributes',
$relatedAttribute->getId(), $relatedAttribute->getId(),
$relatedAttribute $relatedAttribute->setAttribute('status', 'failed')
->setAttribute('status', 'failed')
->setAttribute('error', '')
); );
} }
} finally { } finally {
@ -236,7 +221,7 @@ class DatabaseV1 extends Worker
if ($options['twoWay']) { if ($options['twoWay']) {
$relatedCollection = $dbForProject->getDocument('database_' . $database->getInternalId(), $options['relatedCollection']); $relatedCollection = $dbForProject->getDocument('database_' . $database->getInternalId(), $options['relatedCollection']);
if ($relatedCollection->isEmpty()) { if ($relatedCollection->isEmpty()) {
throw new Exception(Exception::COLLECTION_NOT_FOUND); throw new DatabaseException('Collection not found');
} }
$relatedAttribute = $dbForProject->getDocument('attributes', $database->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $options['twoWayKey']); $relatedAttribute = $dbForProject->getDocument('attributes', $database->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $options['twoWayKey']);
} }
@ -246,7 +231,7 @@ class DatabaseV1 extends Worker
throw new DatabaseException('Failed to delete Relationship'); throw new DatabaseException('Failed to delete Relationship');
} }
} elseif (!$dbForProject->deleteAttribute('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $key)) { } elseif (!$dbForProject->deleteAttribute('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $key)) {
throw new Exception('Failed to delete Attribute'); throw new DatabaseException('Failed to delete Attribute');
} }
} }
@ -255,37 +240,27 @@ class DatabaseV1 extends Worker
if (!$relatedAttribute->isEmpty()) { if (!$relatedAttribute->isEmpty()) {
$dbForProject->deleteDocument('attributes', $relatedAttribute->getId()); $dbForProject->deleteDocument('attributes', $relatedAttribute->getId());
} }
} catch (DatabaseException $e) {
Console::error($e->getMessage());
$dbForProject->updateDocument(
'attributes',
$attribute->getId(),
$attribute
->setAttribute('status', 'stuck')
->setAttribute('error', $e->getMessage())
);
$dbForProject->updateDocument(
'attributes',
$relatedAttribute->getId(),
$relatedAttribute
->setAttribute('status', 'stuck')
->setAttribute('error', $e->getMessage())
);
} catch (Exception $e) { } catch (Exception $e) {
Console::error($e->getMessage());
if ($e instanceof DatabaseException) {
$attribute->setAttribute('error', $e->getMessage());
if (!$relatedAttribute->isEmpty()) {
$relatedAttribute->setAttribute('error', $e->getMessage());
}
}
$dbForProject->updateDocument( $dbForProject->updateDocument(
'attributes', 'attributes',
$attribute->getId(), $attribute->getId(),
$attribute $attribute->setAttribute('status', 'stuck')
->setAttribute('status', 'stuck')
->setAttribute('error', '')
);
$dbForProject->updateDocument(
'attributes',
$relatedAttribute->getId(),
$relatedAttribute
->setAttribute('status', 'stuck')
->setAttribute('error', '')
); );
if (!$relatedAttribute->isEmpty()) {
$dbForProject->updateDocument(
'attributes',
$relatedAttribute->getId(),
$relatedAttribute->setAttribute('status', 'stuck')
);
}
} finally { } finally {
$target = Realtime::fromPayload( $target = Realtime::fromPayload(
// Pass first, most verbose event pattern // Pass first, most verbose event pattern
@ -398,22 +373,16 @@ class DatabaseV1 extends Worker
throw new DatabaseException('Failed to create Index'); throw new DatabaseException('Failed to create Index');
} }
$dbForProject->updateDocument('indexes', $index->getId(), $index->setAttribute('status', 'available')); $dbForProject->updateDocument('indexes', $index->getId(), $index->setAttribute('status', 'available'));
} catch (DatabaseException $e) {
Console::error($e->getMessage());
$dbForProject->updateDocument(
'indexes',
$index->getId(),
$index
->setAttribute('status', 'failed')
->setAttribute('error', $e->getMessage())
);
} catch (Exception $e) { } catch (Exception $e) {
Console::error($e->getMessage());
if ($e instanceof DatabaseException) {
$index->setAttribute('error', $e->getMessage());
}
$dbForProject->updateDocument( $dbForProject->updateDocument(
'indexes', 'indexes',
$index->getId(), $index->getId(),
$index $index->setAttribute('status', 'failed')
->setAttribute('status', 'failed')
->setAttribute('error', '')
); );
} finally { } finally {
$target = Realtime::fromPayload( $target = Realtime::fromPayload(
@ -445,6 +414,7 @@ class DatabaseV1 extends Worker
* @param Document $collection * @param Document $collection
* @param Document $index * @param Document $index
* @param string $projectId * @param string $projectId
* @throws Exception
*/ */
protected function deleteIndex(Document $database, Document $collection, Document $index, string $projectId): void protected function deleteIndex(Document $database, Document $collection, Document $index, string $projectId): void
{ {
@ -465,22 +435,16 @@ class DatabaseV1 extends Worker
throw new DatabaseException('Failed to delete index'); throw new DatabaseException('Failed to delete index');
} }
$dbForProject->deleteDocument('indexes', $index->getId()); $dbForProject->deleteDocument('indexes', $index->getId());
} catch (DatabaseException $e) {
Console::error($e->getMessage());
$dbForProject->updateDocument(
'indexes',
$index->getId(),
$index
->setAttribute('status', 'stuck')
->setAttribute('error', $e->getMessage())
);
} catch (Exception $e) { } catch (Exception $e) {
Console::error($e->getMessage());
if ($e instanceof DatabaseException) {
$index->setAttribute('error', $e->getMessage());
}
$dbForProject->updateDocument( $dbForProject->updateDocument(
'indexes', 'indexes',
$index->getId(), $index->getId(),
$index $index->setAttribute('status', 'stuck')
->setAttribute('status', 'stuck')
->setAttribute('error', '')
); );
} finally { } finally {
$target = Realtime::fromPayload( $target = Realtime::fromPayload(