Skip to content

Commit 97583ae

Browse files
authored
Merge pull request #155 from utopia-php/migration-error-codes
2 parents e9aef98 + 9e70312 commit 97583ae

File tree

13 files changed

+246
-100
lines changed

13 files changed

+246
-100
lines changed

src/Migration/Destinations/Appwrite.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ public function report(array $resources = [], array $resourceIds = []): array
272272

273273
} catch (AppwriteException $e) {
274274
if ($e->getCode() === 403) {
275-
throw new \Exception('Missing scope: ' . $scope, previous: $e);
275+
throw new \Exception(
276+
'Missing scope: ' . $scope,
277+
(int) $e->getCode() ?: Exception::CODE_FORBIDDEN,
278+
$e
279+
);
276280
}
277281
throw $e;
278282
}
@@ -309,7 +313,7 @@ protected function import(array $resources, callable $callback): void
309313
Transfer::GROUP_FUNCTIONS => $this->importFunctionResource($resource),
310314
Transfer::GROUP_MESSAGING => $this->importMessagingResource($resource),
311315
Transfer::GROUP_SITES => $this->importSiteResource($resource),
312-
default => throw new \Exception('Invalid resource group'),
316+
default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION),
313317
};
314318
} catch (\Throwable $e) {
315319
$resource->setStatus(Resource::STATUS_ERROR, $e->getMessage());
@@ -523,7 +527,7 @@ protected function createColumn(Column $resource): bool
523527
Column::TYPE_VARCHAR => UtopiaDatabase::VAR_VARCHAR,
524528
Column::TYPE_MEDIUMTEXT => UtopiaDatabase::VAR_MEDIUMTEXT,
525529
Column::TYPE_LONGTEXT => UtopiaDatabase::VAR_LONGTEXT,
526-
default => throw new \Exception('Invalid resource type '.$resource->getType()),
530+
default => throw new \Exception('Invalid resource type '.$resource->getType(), Exception::CODE_VALIDATION),
527531
};
528532

529533
$database = $this->database->getDocument(
@@ -743,7 +747,7 @@ protected function createColumn(Column $resource): bool
743747
$resource->getFormatOptions(),
744748
$resource->getFilters(),
745749
)) {
746-
throw new \Exception('Failed to create Column');
750+
throw new \Exception('Failed to create Column', Exception::CODE_INTERNAL);
747751
}
748752
}
749753
} catch (\Throwable) {
@@ -1165,7 +1169,7 @@ public function importFileResource(Resource $resource): Resource
11651169
'none' => Compression::NONE(),
11661170
'gzip' => Compression::GZIP(),
11671171
'zstd' => Compression::ZSTD(),
1168-
default => throw new \Exception('Invalid Compression: ' . $resource->getCompression()),
1172+
default => throw new \Exception('Invalid Compression: ' . $resource->getCompression(), Exception::CODE_VALIDATION),
11691173
};
11701174

11711175
$response = $this->storage->createBucket(
@@ -1339,7 +1343,7 @@ public function importPasswordUser(User $user): ?array
13391343
$result = null;
13401344

13411345
if (!$hash) {
1342-
throw new \Exception('Password hash is missing');
1346+
throw new \Exception('Password hash is missing', Exception::CODE_VALIDATION);
13431347
}
13441348

13451349
switch ($hash->getAlgorithm()) {
@@ -1483,7 +1487,7 @@ public function importFunctionResource(Resource $resource): Resource
14831487
'bun-1.0' => Runtime::BUN10(),
14841488
'bun-1.1' => Runtime::BUN11(),
14851489
'go-1.23' => Runtime::GO123(),
1486-
default => throw new \Exception('Invalid Runtime: ' . $resource->getRuntime()),
1490+
default => throw new \Exception('Invalid Runtime: ' . $resource->getRuntime(), Exception::CODE_VALIDATION),
14871491
};
14881492

14891493
$this->functions->create(
@@ -1573,7 +1577,7 @@ private function importDeployment(Deployment $deployment): Resource
15731577
);
15741578

15751579
if (!\is_array($response) || !isset($response['$id'])) {
1576-
throw new \Exception('Deployment creation failed');
1580+
throw new \Exception('Deployment creation failed', Exception::CODE_INTERNAL);
15771581
}
15781582

15791583
if ($deployment->getStart() === 0) {
@@ -1698,7 +1702,7 @@ public function importSiteResource(Resource $resource): Resource
16981702
'flutter-3.29' => BuildRuntime::FLUTTER329(),
16991703
'flutter-3.32' => BuildRuntime::FLUTTER332(),
17001704
'flutter-3.35' => BuildRuntime::FLUTTER335(),
1701-
default => throw new \Exception('Invalid Build Runtime: ' . $resource->getBuildRuntime()),
1705+
default => throw new \Exception('Invalid Build Runtime: ' . $resource->getBuildRuntime(), Exception::CODE_VALIDATION),
17021706
};
17031707

17041708
$framework = match ($resource->getFramework()) {
@@ -2114,7 +2118,7 @@ private function importSiteDeployment(SiteDeployment $deployment): Resource
21142118
);
21152119

21162120
if (!\is_array($response) || !isset($response['$id'])) {
2117-
throw new \Exception('Site deployment creation failed');
2121+
throw new \Exception('Site deployment creation failed', Exception::CODE_INTERNAL);
21182122
}
21192123

21202124
$deployment->setStatus(Resource::STATUS_SUCCESS);
@@ -2138,7 +2142,7 @@ private function importSiteDeployment(SiteDeployment $deployment): Resource
21382142
);
21392143

21402144
if (!\is_array($response) || !isset($response['$id'])) {
2141-
throw new \Exception('Site deployment creation failed');
2145+
throw new \Exception('Site deployment creation failed', Exception::CODE_INTERNAL);
21422146
}
21432147

21442148
if ($deployment->getStart() === 0) {

src/Migration/Destinations/CSV.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Utopia\Database\Exception\Conflict;
77
use Utopia\Database\Exception\Structure;
88
use Utopia\Migration\Destination;
9+
use Utopia\Migration\Exception as MigrationException;
910
use Utopia\Migration\Resource as UtopiaResource;
1011
use Utopia\Migration\Resources\Database\Row;
1112
use Utopia\Migration\Transfer;
@@ -90,13 +91,13 @@ protected function import(array $resources, callable $callback): void
9091
if (!isset($handle)) {
9192
$handle = \fopen($log, 'a');
9293
if ($handle === false) {
93-
throw new \Exception("Failed to open file for writing: $log");
94+
throw new \Exception("Failed to open file for writing: $log", MigrationException::CODE_INTERNAL);
9495
}
9596
}
9697

9798
foreach ($buffer['lines'] as $line) {
9899
if (!$this->writeCSVLine($handle, $line)) {
99-
throw new \Exception("Failed to write CSV line to file: $log");
100+
throw new \Exception("Failed to write CSV line to file: $log", MigrationException::CODE_INTERNAL);
100101
}
101102
}
102103

@@ -167,7 +168,7 @@ public function shutdown(): void
167168
$destPath = $this->deviceForFiles->getPath($this->directory . '/' . $filename);
168169

169170
if (!$this->local->exists($sourcePath)) {
170-
throw new \Exception("No data to export for resource: $this->resourceId");
171+
throw new \Exception("No data to export for resource: $this->resourceId", MigrationException::CODE_NOT_FOUND);
171172
}
172173

173174
try {
@@ -177,10 +178,13 @@ public function shutdown(): void
177178
$this->deviceForFiles
178179
);
179180
if ($result === false) {
180-
throw new \Exception('Error transferring to ' . $this->deviceForFiles->getRoot() . '/' . $filename);
181+
throw new \Exception(
182+
'Error transferring to ' . $this->deviceForFiles->getRoot() . '/' . $filename,
183+
MigrationException::CODE_INTERNAL
184+
);
181185
}
182186
if (!$this->deviceForFiles->exists($destPath)) {
183-
throw new \Exception('File not found on destination: ' . $destPath);
187+
throw new \Exception('File not found on destination: ' . $destPath, MigrationException::CODE_NOT_FOUND);
184188
}
185189
} finally {
186190
// Clean up the temporary directory
@@ -226,7 +230,7 @@ protected function createDirectory(string $path): void
226230
{
227231
if (!\file_exists($path)) {
228232
if (!\mkdir($path, 0755, true)) {
229-
throw new \Exception('Error creating directory: ' . $path);
233+
throw new \Exception('Error creating directory: ' . $path, MigrationException::CODE_INTERNAL);
230234
}
231235
}
232236
}

src/Migration/Destinations/JSON.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ protected function import(array $resources, callable $callback): void
139139
resourceGroup: $resource->getGroup(),
140140
resourceId: $resource->getId(),
141141
message: $e->getMessage(),
142+
code: MigrationException::CODE_VALIDATION,
142143
previous: $e,
143144
));
144145
continue;

src/Migration/Destinations/Local.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Utopia\Migration\Destinations;
44

55
use Utopia\Migration\Destination;
6+
use Utopia\Migration\Exception as MigrationException;
67
use Utopia\Migration\Resource;
78
use Utopia\Migration\Resources\Functions\Deployment;
89
use Utopia\Migration\Resources\Storage\File;
@@ -87,7 +88,7 @@ public function report(array $resources = [], array $resourceIds = []): array
8788
$report = [];
8889

8990
if (!\is_writable($this->path . '/backup.json')) {
90-
throw new \Exception('Unable to write to file: ' . $this->path);
91+
throw new \Exception('Unable to write to file: ' . $this->path, MigrationException::CODE_INTERNAL);
9192
}
9293

9394
return $report;
@@ -101,7 +102,10 @@ private function sync(): void
101102
$json = \json_encode($this->data, JSON_PRETTY_PRINT);
102103

103104
if ($json === false) {
104-
throw new \Exception('Unable to encode data to JSON, Are you accidentally encoding binary data?');
105+
throw new \Exception(
106+
'Unable to encode data to JSON, Are you accidentally encoding binary data?',
107+
MigrationException::CODE_VALIDATION
108+
);
105109
}
106110

107111
\file_put_contents($this->path . '/backup.json', $json);

src/Migration/Exception.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
class Exception extends \Exception implements \JsonSerializable
66
{
7+
public const CODE_VALIDATION = 400;
8+
public const CODE_UNAUTHORIZED = 401;
9+
public const CODE_FORBIDDEN = 403;
10+
public const CODE_NOT_FOUND = 404;
11+
public const CODE_CONFLICT = 409;
12+
public const CODE_RATE_LIMITED = 429;
13+
public const CODE_INTERNAL = 500;
14+
715
public string $resourceName;
816

917
public string $resourceGroup;
@@ -26,7 +34,7 @@ public function __construct(
2634
if (\is_numeric($code)) {
2735
$code = (int) $code;
2836
} else {
29-
$code = 500; // PDOException
37+
$code = self::CODE_INTERNAL; // PDOException
3038
}
3139
}
3240

src/Migration/Sources/Appwrite.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ public function __construct(
115115
break;
116116
case static::SOURCE_DATABASE:
117117
if (\is_null($dbForProject)) {
118-
throw new \Exception('Database is required for database source');
118+
throw new \Exception('Database is required for database source', Exception::CODE_VALIDATION);
119119
}
120120
$this->database = new DatabaseReader($dbForProject);
121121
break;
122122
default:
123-
throw new \Exception('Unknown source');
123+
throw new \Exception('Unknown source', Exception::CODE_VALIDATION);
124124
}
125125
}
126126

@@ -222,9 +222,9 @@ public function report(array $resources = [], array $resourceIds = []): array
222222
)['version'];
223223
} catch (\Throwable $e) {
224224
if ($e->getCode() === 403) {
225-
throw new \Exception("Missing required scopes.");
225+
throw new \Exception('Missing required scopes.', $e->getCode(), $e);
226226
} else {
227-
throw new \Exception($e->getMessage(), previous: $e);
227+
throw new \Exception($e->getMessage(), $e->getCode(), $e);
228228
}
229229
}
230230

@@ -516,7 +516,7 @@ protected function exportGroupAuth(int $batchSize, array $resources): void
516516
Resource::TYPE_USER,
517517
Transfer::GROUP_AUTH,
518518
message: $e->getMessage(),
519-
code: $e->getCode(),
519+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
520520
previous: $e
521521
));
522522
}
@@ -530,7 +530,7 @@ protected function exportGroupAuth(int $batchSize, array $resources): void
530530
Resource::TYPE_TEAM,
531531
Transfer::GROUP_AUTH,
532532
message: $e->getMessage(),
533-
code: $e->getCode(),
533+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
534534
previous: $e
535535
));
536536
}
@@ -544,7 +544,7 @@ protected function exportGroupAuth(int $batchSize, array $resources): void
544544
Resource::TYPE_MEMBERSHIP,
545545
Transfer::GROUP_AUTH,
546546
message: $e->getMessage(),
547-
code: $e->getCode(),
547+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
548548
previous: $e
549549
));
550550
}
@@ -684,7 +684,7 @@ private function exportMemberships(int $batchSize): void
684684
foreach ($response['memberships'] as $membership) {
685685
$user = $cacheUsers[$membership['userId']] ?? null;
686686
if ($user === null) {
687-
throw new \Exception('User not found');
687+
throw new \Exception('User not found', Exception::CODE_NOT_FOUND);
688688
}
689689

690690
$memberships[] = new Membership(
@@ -719,7 +719,7 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
719719
Resource::TYPE_DATABASE,
720720
Transfer::GROUP_DATABASES,
721721
message: $e->getMessage(),
722-
code: $e->getCode(),
722+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
723723
previous: $e
724724
)
725725
);
@@ -737,7 +737,7 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
737737
Resource::TYPE_TABLE,
738738
Transfer::GROUP_DATABASES,
739739
message: $e->getMessage(),
740-
code: $e->getCode(),
740+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
741741
previous: $e
742742
)
743743
);
@@ -755,7 +755,7 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
755755
Resource::TYPE_COLUMN,
756756
Transfer::GROUP_DATABASES,
757757
message: $e->getMessage(),
758-
code: $e->getCode(),
758+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
759759
previous: $e
760760
)
761761
);
@@ -773,7 +773,7 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
773773
Resource::TYPE_INDEX,
774774
Transfer::GROUP_DATABASES,
775775
message: $e->getMessage(),
776-
code: $e->getCode(),
776+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
777777
previous: $e
778778
)
779779
);
@@ -791,7 +791,7 @@ protected function exportGroupDatabases(int $batchSize, array $resources): void
791791
Resource::TYPE_ROW,
792792
Transfer::GROUP_DATABASES,
793793
message: $e->getMessage(),
794-
code: $e->getCode(),
794+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
795795
previous: $e
796796
)
797797
);
@@ -1358,7 +1358,7 @@ protected function exportGroupStorage(int $batchSize, array $resources): void
13581358
Resource::TYPE_BUCKET,
13591359
Transfer::GROUP_STORAGE,
13601360
message: $e->getMessage(),
1361-
code: $e->getCode(),
1361+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
13621362
previous: $e
13631363
)
13641364
);
@@ -1374,7 +1374,7 @@ protected function exportGroupStorage(int $batchSize, array $resources): void
13741374
Resource::TYPE_FILE,
13751375
Transfer::GROUP_STORAGE,
13761376
message: $e->getMessage(),
1377-
code: $e->getCode(),
1377+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
13781378
previous: $e
13791379
)
13801380
);
@@ -1527,7 +1527,7 @@ protected function exportGroupFunctions(int $batchSize, array $resources): void
15271527
Resource::TYPE_FUNCTION,
15281528
Transfer::GROUP_FUNCTIONS,
15291529
message: $e->getMessage(),
1530-
code: $e->getCode(),
1530+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
15311531
previous: $e
15321532
));
15331533
}
@@ -1540,7 +1540,7 @@ protected function exportGroupFunctions(int $batchSize, array $resources): void
15401540
Resource::TYPE_DEPLOYMENT,
15411541
Transfer::GROUP_FUNCTIONS,
15421542
message: $e->getMessage(),
1543-
code: $e->getCode(),
1543+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
15441544
previous: $e
15451545
));
15461546
}
@@ -1557,7 +1557,7 @@ protected function exportGroupSites(int $batchSize, array $resources): void
15571557
Resource::TYPE_SITE,
15581558
Transfer::GROUP_SITES,
15591559
message: $e->getMessage(),
1560-
code: $e->getCode(),
1560+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
15611561
previous: $e
15621562
));
15631563
}
@@ -1570,7 +1570,7 @@ protected function exportGroupSites(int $batchSize, array $resources): void
15701570
Resource::TYPE_SITE_DEPLOYMENT,
15711571
Transfer::GROUP_SITES,
15721572
message: $e->getMessage(),
1573-
code: $e->getCode(),
1573+
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
15741574
previous: $e
15751575
));
15761576
}

0 commit comments

Comments
 (0)