Skip to content

Commit

Permalink
Refactor Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PineappleIOnic committed Feb 19, 2024
1 parent 7c00d95 commit 3e13d04
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 18 deletions.
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\Error $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\Error $error */
Console::error('[Destination] ['.$error->getResourceType().'] '.$error->getMessage());
}
}
7 changes: 7 additions & 0 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\Error;
use Utopia\Migration\Resource;
use Utopia\Migration\Resources\Auth\Hash;
use Utopia\Migration\Resources\Auth\Membership;
Expand Down Expand Up @@ -243,6 +244,12 @@ protected function import(array $resources, callable $callback): void
$resource->setStatus(Resource::STATUS_SKIPPED, $e->getMessage());
} else {
$resource->setStatus(Resource::STATUS_ERROR, $e->getMessage());
$this->pushError(new Error(
resourceType: $resource->getGroup(),
resourceId: $resource->getId(),
message: $e->getMessage(),
code: $e->getCode()
));
}

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

namespace Utopia\Migration;

class Error 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;
}
}
2 changes: 1 addition & 1 deletion src/Migration/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class Resource

public const TYPE_HASH = 'hash';

public const TYPE_ENVVAR = 'envvar';
public const TYPE_ENVVAR = 'environment variable';

public const ALL_RESOURCES = [
self::TYPE_ATTRIBUTE,
Expand Down
28 changes: 19 additions & 9 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Appwrite\Services\Storage;
use Appwrite\Services\Teams;
use Appwrite\Services\Users;
use Utopia\Migration\Error;
use Utopia\Migration\Resource;
use Utopia\Migration\Resources\Auth\Hash;
use Utopia\Migration\Resources\Auth\Membership;
Expand Down Expand Up @@ -977,15 +978,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 (\Exception $e) {
$this->pushError(new Error(
resourceType: Resource::TYPE_FILE,
message: $e->getMessage(),
code: $e->getCode(),
resourceId: $file['$id']
));
}

$lastDocument = $file['$id'];
}
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 pushError(Error $error): void
{
$this->errors[] = $error;
}
}
14 changes: 14 additions & 0 deletions src/Migration/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ public function getStatusCounters()
}
}

// 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

0 comments on commit 3e13d04

Please sign in to comment.