Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function report(array $resources = [], array $resourceIds = []): array

} catch (AppwriteException $e) {
if ($e->getCode() === 403) {
throw new \Exception('Missing scope: ' . $scope, previous: $e);
throw new \Exception('Missing scope: ' . $scope, $e->getCode(), $e);
}
throw $e;
}
Expand Down
1 change: 1 addition & 0 deletions src/Migration/Destinations/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ protected function import(array $resources, callable $callback): void
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: $e->getMessage(),
code: MigrationException::CODE_VALIDATION,
previous: $e,
));
continue;
Expand Down
10 changes: 9 additions & 1 deletion src/Migration/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

class Exception extends \Exception implements \JsonSerializable
{
public const CODE_VALIDATION = 400;
public const CODE_UNAUTHORIZED = 401;
public const CODE_FORBIDDEN = 403;
public const CODE_NOT_FOUND = 404;
public const CODE_CONFLICT = 409;
public const CODE_RATE_LIMITED = 429;
public const CODE_INTERNAL = 500;

public string $resourceName;

public string $resourceGroup;
Expand All @@ -26,7 +34,7 @@ public function __construct(
if (\is_numeric($code)) {
$code = (int) $code;
} else {
$code = 500; // PDOException
$code = self::CODE_INTERNAL; // PDOException
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ public function report(array $resources = [], array $resourceIds = []): array
)['version'];
} catch (\Throwable $e) {
if ($e->getCode() === 403) {
throw new \Exception("Missing required scopes.");
throw new \Exception('Missing required scopes.', $e->getCode(), $e);
} else {
throw new \Exception($e->getMessage(), previous: $e);
throw new \Exception($e->getMessage(), $e->getCode(), $e);
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/Migration/Sources/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private function exportRows(int $batchSize): void

while (($row = \fgetcsv($stream, 0, $delimiter, '"', '"')) !== false) {
if (\count($row) !== \count($headers)) {
throw new \Exception('CSV row does not match the number of header columns.');
throw new \Exception('CSV row does not match the number of header columns.', Exception::CODE_VALIDATION);
}

$data = \array_combine($headers, $row);
Expand Down Expand Up @@ -257,7 +257,7 @@ private function exportRows(int $batchSize): void
}

if (!\is_array($arrayValues)) {
throw new \Exception("Invalid array format for column '$key': $parsedValue");
throw new \Exception("Invalid array format for column '$key': $parsedValue", Exception::CODE_VALIDATION);
}

$parsedData[$key] = array_map(function ($item) use ($type) {
Expand Down Expand Up @@ -436,7 +436,7 @@ private function validateCSVHeaders(array $headers, array $columnTypes, array $r
$messages[] = "$label: '" . \implode("', '", $missingRequired) . "'";
}
if (!empty($missingRequired)) {
throw new \Exception('CSV header validation failed: ' . \implode('. ', $messages));
throw new \Exception('CSV header validation failed: ' . \implode('. ', $messages), Exception::CODE_VALIDATION);
}

// If there are unknown columns but no missing required columns, just log a warning
Expand Down Expand Up @@ -479,7 +479,11 @@ private function downloadToLocal(
}

if (!$success) {
throw new \Exception('Failed to transfer CSV file from device to local storage.', previous: $e ?? null);
throw new \Exception(
'Failed to transfer CSV file from device to local storage.',
Exception::CODE_INTERNAL,
$e ?? null
);
}

$this->downloaded = true;
Expand Down
12 changes: 8 additions & 4 deletions src/Migration/Sources/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function exportRows(int $batchSize): void

foreach ($items as $index => $item) {
if (!\is_array($item)) {
throw new \Exception("JSON item at index $index is not an object.");
throw new \Exception("JSON item at index $index is not an object.", Exception::CODE_VALIDATION);
}

$rowId = $item['$id'] ?? 'unique()';
Expand Down Expand Up @@ -257,7 +257,11 @@ private function downloadToLocal(
}

if (!$success) {
throw new \Exception('Failed to transfer JSON file from device to local storage.', previous: $e ?? null);
throw new \Exception(
'Failed to transfer JSON file from device to local storage.',
Exception::CODE_INTERNAL,
$e ?? null
);
}

$this->downloaded = true;
Expand All @@ -270,12 +274,12 @@ private function downloadToLocal(
private function validatePermissions(mixed $permissions): array
{
if (!\is_array($permissions)) {
throw new \Exception('Invalid permissions format; expected an array of strings.');
throw new \Exception('Invalid permissions format; expected an array of strings.', Exception::CODE_VALIDATION);
}

foreach ($permissions as $value) {
if (!\is_string($value)) {
throw new \Exception('Invalid permission value; expected string.');
throw new \Exception('Invalid permission value; expected string.', Exception::CODE_VALIDATION);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Migration/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ protected function call(
}

if (\curl_errno($ch)) {
throw new \Exception(\curl_error($ch));
throw new \Exception(\curl_error($ch), Exception::CODE_INTERNAL);
}

\curl_close($ch);

if ($responseStatus >= 400) {
if (\is_array($responseBody)) {
throw new \Exception(\json_encode($responseBody));
throw new \Exception(\json_encode($responseBody), $responseStatus);
} else {
throw new \Exception($responseStatus.': '.$responseBody);
throw new \Exception($responseStatus.': '.$responseBody, $responseStatus);
}
}

Expand Down