Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Error Handling #24

Merged
merged 6 commits into from
Feb 23, 2024
Merged
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 phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
<directory>./tests/Migration/E2E</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
18 changes: 10 additions & 8 deletions playground.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Utopia\CLI\Console;
use Utopia\Migration\Destinations\Appwrite as AppwriteDestination;
use Utopia\Migration\Destinations\Local;
use Utopia\Migration\Resource;
use Utopia\Migration\Sources\Appwrite;
use Utopia\Migration\Sources\Firebase;
use Utopia\Migration\Sources\NHost;
Expand Down Expand Up @@ -88,13 +87,16 @@ function (array $resources) use ($transfer) {

$cache = $transfer->getCache()->getAll();

foreach ($cache as $type => $resources) {
foreach ($resources as $resource) {
/** @var resource $resource */
if ($resource->getStatus() !== Resource::STATUS_ERROR) {
continue;
}
if (count($sourceAppwrite->getErrors()) > 0) {
foreach ($sourceAppwrite->getErrors() as $error) {
/* @var \Utopia\Migration\Exception $error */
Console::error('[Source] ['.$error->getResourceType().'] '.$error->getMessage());
}
}

Console::error($resource->getName().' '.$resource->getInternalId().' '.$resource->getMessage());
if (count($destinationAppwrite->getErrors()) > 0) {
foreach ($destinationAppwrite->getErrors() as $error) {
/* @var \Utopia\Migration\Exception $error */
Console::error('[Destination] ['.$error->getResourceType().'] '.$error->getMessage());
}
}
11 changes: 9 additions & 2 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Appwrite\Services\Teams;
use Appwrite\Services\Users;
use Utopia\Migration\Destination;
use Utopia\Migration\Exception;
use Utopia\Migration\Resource;
use Utopia\Migration\Resources\Auth\Hash;
use Utopia\Migration\Resources\Auth\Membership;
Expand Down Expand Up @@ -204,7 +205,7 @@ public function report(array $resources = []): array
}

return [];
} catch (\Exception $exception) {
} catch (\Throwable $exception) {
if ($exception->getCode() === 403) {
throw new \Exception('Missing permission: '.$currentPermission);
} else {
Expand Down Expand Up @@ -238,11 +239,17 @@ protected function import(array $resources, callable $callback): void
$responseResource = $this->importFunctionResource($resource);
break;
}
} catch (\Exception $e) {
} catch (\Throwable $e) {
if ($e->getCode() === 409) {
$resource->setStatus(Resource::STATUS_SKIPPED, $e->getMessage());
} else {
$resource->setStatus(Resource::STATUS_ERROR, $e->getMessage());
$this->addError(new Exception(
resourceType: $resource->getGroup(),
resourceId: $resource->getId(),
message: $e->getMessage(),
code: $e->getCode()
));
}

$responseResource = $resource;
Expand Down
28 changes: 28 additions & 0 deletions src/Migration/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Utopia\Migration;

class Exception extends \Exception
{
public string $resourceType;

public string $resourceId;

public function __construct(string $resourceType, string $message, int $code = 0, ?\Throwable $previous = null, string $resourceId = '')
{
$this->resourceId = $resourceId;
$this->resourceType = $resourceType;

parent::__construct($message, $code, $previous);
}

public function getResourceType(): string
{
return $this->resourceType;
}

public function getResourceId(): string
{
return $this->resourceId;
}
}
35 changes: 23 additions & 12 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Appwrite\Services\Storage;
use Appwrite\Services\Teams;
use Appwrite\Services\Users;
use Utopia\Migration\Exception;
use Utopia\Migration\Exception;
use Utopia\Migration\Resource;
use Utopia\Migration\Resources\Auth\Hash;
use Utopia\Migration\Resources\Auth\Membership;
Expand Down Expand Up @@ -265,7 +267,7 @@ public function report(array $resources = []): array
$this->previousReport = $report;

return $report;
} catch (\Exception $e) {
} catch (\Throwable $e) {
if ($e->getCode() === 403) {
throw new \Exception("Missing Permission: {$currentPermission}.");
} else {
Expand Down Expand Up @@ -977,15 +979,24 @@ private function exportFiles(int $batchSize)
);

foreach ($response['files'] as $file) {
$this->exportFileData(new File(
$file['$id'],
$bucket,
$file['name'],
$file['signature'],
$file['mimeType'],
$file['$permissions'],
$file['sizeOriginal'],
));
try {
$this->exportFileData(new File(
$file['$id'],
$bucket,
$file['name'],
$file['signature'],
$file['mimeType'],
$file['$permissions'],
$file['sizeOriginal'],
));
} catch (\Throwable $e) {
$this->addError(new Exception(
resourceType: Resource::TYPE_FILE,
message: $e->getMessage(),
code: $e->getCode(),
resourceId: $file['$id']
));
}

$lastDocument = $file['$id'];
}
Expand Down Expand Up @@ -1106,7 +1117,7 @@ private function exportDeployments(int $batchSize, bool $exportOnlyActive = fals

try {
$this->exportDeploymentData($func, $deployment);
} catch (\Exception $e) {
} catch (\Throwable $e) {
$func->setStatus(Resource::STATUS_ERROR, $e->getMessage());
}

Expand All @@ -1128,7 +1139,7 @@ private function exportDeployments(int $batchSize, bool $exportOnlyActive = fals
foreach ($response['deployments'] as $deployment) {
try {
$this->exportDeploymentData($func, $deployment);
} catch (\Exception $e) {
} catch (\Throwable $e) {
$func->setStatus(Resource::STATUS_ERROR, $e->getMessage());
}

Expand Down
4 changes: 2 additions & 2 deletions src/Migration/Sources/Firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private function authenticate()
$this->currentToken = $response['access_token'];
$this->tokenExpires = time() + $response['expires_in'];
$this->headers['Authorization'] = 'Bearer '.$this->currentToken;
} catch (\Exception $e) {
} catch (\Throwable $e) {
throw new \Exception('Failed to authenticate with Firebase: '.$e->getMessage());
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ protected function exportGroupAuth(int $batchSize, array $resources)
// Check if Auth is enabled
try {
$this->call('GET', 'https://identitytoolkit.googleapis.com/v1/projects');
} catch (\Exception $e) {
} catch (\Throwable $e) {
$message = json_decode($e->getMessage(), true);

if (isset($message['error']['details']) && $message['error']['details'][1]['reason'] == 'SERVICE_DISABLED') {
Expand Down
35 changes: 35 additions & 0 deletions src/Migration/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ abstract class Target
*/
public $cache;

/**
* Errors
*
* @var array
*/
public $errors = [];

/**
* Endpoint
*
Expand Down Expand Up @@ -168,4 +175,32 @@ protected function flatten(array $data, string $prefix = ''): array

return $output;
}

/**
* Get Errors
*
* @returns Error[]
*/
public function getErrors(): array
{
return $this->errors;
}

/**
* Set Errors
*
* @param Error[] $errors
*/
public function setErrors(array $errors): void
{
$this->errors = $errors;
}

/**
* Push Error
*/
public function addError(Exception $error): void
{
$this->errors[] = $error;
}
}
22 changes: 19 additions & 3 deletions src/Migration/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,29 @@ public function getStatusCounters()
foreach ($this->cache->getAll() as $resources) {
foreach ($resources as $resource) {
/** @var resource $resource */
$status[$resource->getName()][$resource->getStatus()]++;
if ($status[$resource->getName()]['pending'] > 0) {
$status[$resource->getName()]['pending']--;
if (isset($status[$resource->getName()])) {
$status[$resource->getName()][$resource->getStatus()]++;
if ($status[$resource->getName()]['pending'] > 0) {
$status[$resource->getName()]['pending']--;
}
}
}
}

// Process Destination Errors
foreach ($this->destination->getErrors() as $error) {
if (isset($status[$error->getResourceType()])) {
$status[$error->getResourceType()][Resource::STATUS_ERROR]++;
}
}

// Process Source Errprs
foreach ($this->source->getErrors() as $error) {
if (isset($status[$error->getResourceType()])) {
$status[$error->getResourceType()][Resource::STATUS_ERROR]++;
}
}

// Remove all empty resources
foreach ($status as $resource => $data) {
$allEmpty = true;
Expand Down
2 changes: 1 addition & 1 deletion tests/Migration/resources/supabase/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1336,4 +1336,4 @@
}
],
"data": []
}
}
Loading