From cd91e119ce17b022b3b3716081dc4a86d9c2e488 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Mon, 9 Mar 2026 15:40:45 +0530 Subject: [PATCH 1/4] feat: enhance exception handling with standardized error codes Added new constants for common HTTP error codes in the Exception class. Updated various parts of the codebase to utilize these constants for improved error handling, including specific validation and internal error messages in the Appwrite, JSON, and CSV source and destination classes. --- src/Migration/Destinations/Appwrite.php | 2 +- src/Migration/Destinations/JSON.php | 1 + src/Migration/Exception.php | 10 +++++++++- src/Migration/Sources/Appwrite.php | 2 +- src/Migration/Sources/CSV.php | 12 ++++++++---- src/Migration/Sources/JSON.php | 12 ++++++++---- src/Migration/Target.php | 6 +++--- 7 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 98a4e94fb6..82025f5e44 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -224,7 +224,7 @@ class Appwrite extends Destination } 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; } diff --git a/src/Migration/Destinations/JSON.php b/src/Migration/Destinations/JSON.php index 91660474ff..91016ef589 100644 --- a/src/Migration/Destinations/JSON.php +++ b/src/Migration/Destinations/JSON.php @@ -139,6 +139,7 @@ class JSON extends Destination resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: $e->getMessage(), + code: MigrationException::CODE_VALIDATION, previous: $e, )); continue; diff --git a/src/Migration/Exception.php b/src/Migration/Exception.php index 86eb2e22ec..ff599a09b1 100644 --- a/src/Migration/Exception.php +++ b/src/Migration/Exception.php @@ -4,6 +4,14 @@ namespace Utopia\Migration; 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; @@ -26,7 +34,7 @@ class Exception extends \Exception implements \JsonSerializable if (\is_numeric($code)) { $code = (int) $code; } else { - $code = 500; // PDOException + $code = self::CODE_INTERNAL; // PDOException } } diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index 2844bcf105..70ef219eac 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -209,7 +209,7 @@ class Appwrite extends Source if ($e->getCode() === 403) { throw new \Exception("Missing required scopes."); } else { - throw new \Exception($e->getMessage(), previous: $e); + throw new \Exception($e->getMessage(), $e->getCode(), $e); } } diff --git a/src/Migration/Sources/CSV.php b/src/Migration/Sources/CSV.php index 3cd227c5ff..821db4bfd7 100644 --- a/src/Migration/Sources/CSV.php +++ b/src/Migration/Sources/CSV.php @@ -208,7 +208,7 @@ class CSV extends Source 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); @@ -257,7 +257,7 @@ class CSV extends Source } 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) { @@ -436,7 +436,7 @@ class CSV extends Source $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 @@ -479,7 +479,11 @@ class CSV extends Source } 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; diff --git a/src/Migration/Sources/JSON.php b/src/Migration/Sources/JSON.php index 8abf63d77b..4fd0c8eb32 100644 --- a/src/Migration/Sources/JSON.php +++ b/src/Migration/Sources/JSON.php @@ -129,7 +129,7 @@ class JSON extends Source 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()'; @@ -257,7 +257,11 @@ class JSON extends Source } 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; @@ -270,12 +274,12 @@ class JSON extends Source 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); } } diff --git a/src/Migration/Target.php b/src/Migration/Target.php index 8fab14c518..8dbc5a87f2 100644 --- a/src/Migration/Target.php +++ b/src/Migration/Target.php @@ -147,16 +147,16 @@ abstract class Target } 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); } } From 864a351b20ef2a5711ca5aa18a6dcacacd160f06 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Mon, 9 Mar 2026 15:53:48 +0530 Subject: [PATCH 2/4] added error code --- src/Migration/Sources/Appwrite.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index 70ef219eac..56bb07d53f 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -207,7 +207,7 @@ class Appwrite extends Source )['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(), $e->getCode(), $e); } From 93aaa9f188182525716c365d33226098d462b5c8 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 10 Mar 2026 11:31:59 +0530 Subject: [PATCH 3/4] updated supabase error handlers --- src/Migration/Sources/Supabase.php | 118 +++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 13 deletions(-) diff --git a/src/Migration/Sources/Supabase.php b/src/Migration/Sources/Supabase.php index 05006e19b7..1d7f1aa50b 100644 --- a/src/Migration/Sources/Supabase.php +++ b/src/Migration/Sources/Supabase.php @@ -209,6 +209,74 @@ class Supabase extends NHost protected string $host; + private static function statusCodeFromSqlState(?string $sqlState): int + { + if ($sqlState === null || $sqlState === '') { + return Exception::CODE_INTERNAL; + } + + // Common Postgres SQLSTATE mappings → HTTP-ish status codes + // Ref: https://www.postgresql.org/docs/current/errcodes-appendix.html + return match ($sqlState) { + // AuthN/AuthZ + '28P01', // invalid_password + '28000', // invalid_authorization_specification + => Exception::CODE_UNAUTHORIZED, + + '42501', // insufficient_privilege + => Exception::CODE_FORBIDDEN, + + // Not found + '3D000', // invalid_catalog_name (db does not exist) + '42P01', // undefined_table + => Exception::CODE_NOT_FOUND, + + // Validation-ish + '42703', // undefined_column + '42601', // syntax_error + => Exception::CODE_VALIDATION, + + // Conflict + '23505', // unique_violation + => Exception::CODE_CONFLICT, + + // Resource / service constraints + '53300', // too_many_connections + => Exception::CODE_RATE_LIMITED, + + // Connection exceptions (class 08) are typically user-side config/network issues + // (wrong host/port, blocked egress, DNS issues), so treat as validation/user actionable. + default => \str_starts_with($sqlState, '08') + ? Exception::CODE_VALIDATION + : Exception::CODE_INTERNAL, + }; + } + + private static function statusCodeFromThrowable(\Throwable $e): int + { + $code = $e->getCode(); + + // PDO / Postgres typically exposes SQLSTATE as the exception code (string) + if (\is_string($code)) { + return self::statusCodeFromSqlState($code); + } + + if (\is_int($code) && $code > 0) { + return $code; + } + + return Exception::CODE_INTERNAL; + } + + /** + * @param array|null $errorInfo PDOStatement::errorInfo() + */ + private static function statusCodeFromErrorInfo(?array $errorInfo): int + { + $sqlState = $errorInfo[0] ?? null; + return self::statusCodeFromSqlState(\is_string($sqlState) ? $sqlState : null); + } + public function __construct(string $endpoint, string $key, string $host, string $databaseName, string $username, string $password, string $port = '5432') { $this->endpoint = $endpoint; @@ -225,7 +293,7 @@ class Supabase extends NHost try { $this->pdo = new \PDO('pgsql:host='.$this->host.';port='.$this->port.';dbname='.$this->databaseName, $this->username, $this->password); } catch (\PDOException $e) { - throw new \Exception('Failed to connect to database: '.$e->getMessage()); + throw new \Exception('Failed to connect to database: '.$e->getMessage(), self::statusCodeFromThrowable($e), $e); } } @@ -240,11 +308,14 @@ class Supabase extends NHost try { $this->pdo = new \PDO('pgsql:host='.$this->host.';port='.$this->port.';dbname='.$this->databaseName, $this->username, $this->password); } catch (\PDOException $e) { - throw new \Exception('Failed to connect to database. PDO Code: '.$e->getCode().' Error: '.$e->getMessage()); + throw new \Exception('Failed to connect to database. PDO Code: '.$e->getCode().' Error: '.$e->getMessage(), self::statusCodeFromThrowable($e), $e); } if (! empty($this->pdo->errorCode())) { - throw new \Exception('Failed to connect to database. PDO Code: '.$this->pdo->errorCode().(empty($this->pdo->errorInfo()[2]) ? '' : ' Error: '.$this->pdo->errorInfo()[2])); + throw new \Exception( + 'Failed to connect to database. PDO Code: '.$this->pdo->errorCode().(empty($this->pdo->errorInfo()[2]) ? '' : ' Error: '.$this->pdo->errorInfo()[2]), + self::statusCodeFromSqlState($this->pdo->errorCode()) + ); } // Auth @@ -253,7 +324,10 @@ class Supabase extends NHost $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access users table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access users table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_USER] = $statement->fetchColumn(); @@ -269,7 +343,10 @@ class Supabase extends NHost $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access tables table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access tables table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_TABLE] = $statement->fetchColumn(); @@ -280,7 +357,10 @@ class Supabase extends NHost $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access columns table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access columns table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_COLUMN] = $statement->fetchColumn(); @@ -291,7 +371,10 @@ class Supabase extends NHost $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access indexes table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access indexes table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_INDEX] = $statement->fetchColumn(); @@ -302,7 +385,10 @@ class Supabase extends NHost $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access tables table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access tables table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_ROW] = $statement->fetchColumn(); @@ -314,7 +400,10 @@ class Supabase extends NHost $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access buckets table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access buckets table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_BUCKET] = $statement->fetchColumn(); @@ -325,7 +414,10 @@ class Supabase extends NHost $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access files table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access files table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_FILE] = $statement->fetchColumn(); @@ -357,7 +449,7 @@ class Supabase extends NHost Resource::TYPE_BUCKET, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e )); } @@ -431,7 +523,7 @@ class Supabase extends NHost Resource::TYPE_BUCKET, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e )); } @@ -445,7 +537,7 @@ class Supabase extends NHost Resource::TYPE_BUCKET, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e )); } From 9e70312e3d49d083a3ff4c3f3bc69b8bbc8a9803 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 10 Mar 2026 11:44:54 +0530 Subject: [PATCH 4/4] refactor: standardize exception handling with appropriate error codes across migration sources and destinations --- src/Migration/Destinations/Appwrite.php | 26 ++-- src/Migration/Destinations/CSV.php | 16 ++- src/Migration/Destinations/Local.php | 8 +- src/Migration/Sources/Appwrite.php | 34 ++--- .../Sources/Appwrite/Reader/Database.php | 10 +- src/Migration/Sources/CSV.php | 2 +- src/Migration/Sources/Firebase.php | 22 +-- src/Migration/Sources/JSON.php | 2 +- src/Migration/Sources/NHost.php | 131 +++++++++++++++--- src/Migration/Sources/Supabase.php | 94 ++----------- 10 files changed, 191 insertions(+), 154 deletions(-) diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 82025f5e44..c52786cf55 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -224,7 +224,11 @@ class Appwrite extends Destination } catch (AppwriteException $e) { if ($e->getCode() === 403) { - throw new \Exception('Missing scope: ' . $scope, $e->getCode(), $e); + throw new \Exception( + 'Missing scope: ' . $scope, + (int) $e->getCode() ?: Exception::CODE_FORBIDDEN, + $e + ); } throw $e; } @@ -260,7 +264,7 @@ class Appwrite extends Destination Transfer::GROUP_AUTH => $this->importAuthResource($resource), Transfer::GROUP_FUNCTIONS => $this->importFunctionResource($resource), Transfer::GROUP_SITES => $this->importSiteResource($resource), - default => throw new \Exception('Invalid resource group'), + default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION), }; } catch (\Throwable $e) { $resource->setStatus(Resource::STATUS_ERROR, $e->getMessage()); @@ -474,7 +478,7 @@ class Appwrite extends Destination Column::TYPE_VARCHAR => UtopiaDatabase::VAR_VARCHAR, Column::TYPE_MEDIUMTEXT => UtopiaDatabase::VAR_MEDIUMTEXT, Column::TYPE_LONGTEXT => UtopiaDatabase::VAR_LONGTEXT, - default => throw new \Exception('Invalid resource type '.$resource->getType()), + default => throw new \Exception('Invalid resource type '.$resource->getType(), Exception::CODE_VALIDATION), }; $database = $this->database->getDocument( @@ -694,7 +698,7 @@ class Appwrite extends Destination $resource->getFormatOptions(), $resource->getFilters(), )) { - throw new \Exception('Failed to create Column'); + throw new \Exception('Failed to create Column', Exception::CODE_INTERNAL); } } } catch (\Throwable) { @@ -1116,7 +1120,7 @@ class Appwrite extends Destination 'none' => Compression::NONE(), 'gzip' => Compression::GZIP(), 'zstd' => Compression::ZSTD(), - default => throw new \Exception('Invalid Compression: ' . $resource->getCompression()), + default => throw new \Exception('Invalid Compression: ' . $resource->getCompression(), Exception::CODE_VALIDATION), }; $response = $this->storage->createBucket( @@ -1290,7 +1294,7 @@ class Appwrite extends Destination $result = null; if (!$hash) { - throw new \Exception('Password hash is missing'); + throw new \Exception('Password hash is missing', Exception::CODE_VALIDATION); } switch ($hash->getAlgorithm()) { @@ -1434,7 +1438,7 @@ class Appwrite extends Destination 'bun-1.0' => Runtime::BUN10(), 'bun-1.1' => Runtime::BUN11(), 'go-1.23' => Runtime::GO123(), - default => throw new \Exception('Invalid Runtime: ' . $resource->getRuntime()), + default => throw new \Exception('Invalid Runtime: ' . $resource->getRuntime(), Exception::CODE_VALIDATION), }; $this->functions->create( @@ -1524,7 +1528,7 @@ class Appwrite extends Destination ); if (!\is_array($response) || !isset($response['$id'])) { - throw new \Exception('Deployment creation failed'); + throw new \Exception('Deployment creation failed', Exception::CODE_INTERNAL); } if ($deployment->getStart() === 0) { @@ -1617,7 +1621,7 @@ class Appwrite extends Destination 'flutter-3.29' => BuildRuntime::FLUTTER329(), 'flutter-3.32' => BuildRuntime::FLUTTER332(), 'flutter-3.35' => BuildRuntime::FLUTTER335(), - default => throw new \Exception('Invalid Build Runtime: ' . $resource->getBuildRuntime()), + default => throw new \Exception('Invalid Build Runtime: ' . $resource->getBuildRuntime(), Exception::CODE_VALIDATION), }; $framework = match ($resource->getFramework()) { @@ -1711,7 +1715,7 @@ class Appwrite extends Destination ); if (!\is_array($response) || !isset($response['$id'])) { - throw new \Exception('Site deployment creation failed'); + throw new \Exception('Site deployment creation failed', Exception::CODE_INTERNAL); } $deployment->setStatus(Resource::STATUS_SUCCESS); @@ -1735,7 +1739,7 @@ class Appwrite extends Destination ); if (!\is_array($response) || !isset($response['$id'])) { - throw new \Exception('Site deployment creation failed'); + throw new \Exception('Site deployment creation failed', Exception::CODE_INTERNAL); } if ($deployment->getStart() === 0) { diff --git a/src/Migration/Destinations/CSV.php b/src/Migration/Destinations/CSV.php index 8b0acd0747..790e6a0a08 100644 --- a/src/Migration/Destinations/CSV.php +++ b/src/Migration/Destinations/CSV.php @@ -6,6 +6,7 @@ use Utopia\Database\Exception\Authorization; use Utopia\Database\Exception\Conflict; use Utopia\Database\Exception\Structure; use Utopia\Migration\Destination; +use Utopia\Migration\Exception as MigrationException; use Utopia\Migration\Resource as UtopiaResource; use Utopia\Migration\Resources\Database\Row; use Utopia\Migration\Transfer; @@ -90,13 +91,13 @@ class CSV extends Destination if (!isset($handle)) { $handle = \fopen($log, 'a'); if ($handle === false) { - throw new \Exception("Failed to open file for writing: $log"); + throw new \Exception("Failed to open file for writing: $log", MigrationException::CODE_INTERNAL); } } foreach ($buffer['lines'] as $line) { if (!$this->writeCSVLine($handle, $line)) { - throw new \Exception("Failed to write CSV line to file: $log"); + throw new \Exception("Failed to write CSV line to file: $log", MigrationException::CODE_INTERNAL); } } @@ -167,7 +168,7 @@ class CSV extends Destination $destPath = $this->deviceForFiles->getPath($this->directory . '/' . $filename); if (!$this->local->exists($sourcePath)) { - throw new \Exception("No data to export for resource: $this->resourceId"); + throw new \Exception("No data to export for resource: $this->resourceId", MigrationException::CODE_NOT_FOUND); } try { @@ -177,10 +178,13 @@ class CSV extends Destination $this->deviceForFiles ); if ($result === false) { - throw new \Exception('Error transferring to ' . $this->deviceForFiles->getRoot() . '/' . $filename); + throw new \Exception( + 'Error transferring to ' . $this->deviceForFiles->getRoot() . '/' . $filename, + MigrationException::CODE_INTERNAL + ); } if (!$this->deviceForFiles->exists($destPath)) { - throw new \Exception('File not found on destination: ' . $destPath); + throw new \Exception('File not found on destination: ' . $destPath, MigrationException::CODE_NOT_FOUND); } } finally { // Clean up the temporary directory @@ -226,7 +230,7 @@ class CSV extends Destination { if (!\file_exists($path)) { if (!\mkdir($path, 0755, true)) { - throw new \Exception('Error creating directory: ' . $path); + throw new \Exception('Error creating directory: ' . $path, MigrationException::CODE_INTERNAL); } } } diff --git a/src/Migration/Destinations/Local.php b/src/Migration/Destinations/Local.php index 4c17ab2044..747d6d94c9 100644 --- a/src/Migration/Destinations/Local.php +++ b/src/Migration/Destinations/Local.php @@ -3,6 +3,7 @@ namespace Utopia\Migration\Destinations; use Utopia\Migration\Destination; +use Utopia\Migration\Exception as MigrationException; use Utopia\Migration\Resource; use Utopia\Migration\Resources\Functions\Deployment; use Utopia\Migration\Resources\Storage\File; @@ -81,7 +82,7 @@ class Local extends Destination $report = []; if (!\is_writable($this->path . '/backup.json')) { - throw new \Exception('Unable to write to file: ' . $this->path); + throw new \Exception('Unable to write to file: ' . $this->path, MigrationException::CODE_INTERNAL); } return $report; @@ -95,7 +96,10 @@ class Local extends Destination $json = \json_encode($this->data, JSON_PRETTY_PRINT); if ($json === false) { - throw new \Exception('Unable to encode data to JSON, Are you accidentally encoding binary data?'); + throw new \Exception( + 'Unable to encode data to JSON, Are you accidentally encoding binary data?', + MigrationException::CODE_VALIDATION + ); } \file_put_contents($this->path . '/backup.json', $json); diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index 56bb07d53f..c68afc3115 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -107,12 +107,12 @@ class Appwrite extends Source break; case static::SOURCE_DATABASE: if (\is_null($dbForProject)) { - throw new \Exception('Database is required for database source'); + throw new \Exception('Database is required for database source', Exception::CODE_VALIDATION); } $this->database = new DatabaseReader($dbForProject); break; default: - throw new \Exception('Unknown source'); + throw new \Exception('Unknown source', Exception::CODE_VALIDATION); } } @@ -501,7 +501,7 @@ class Appwrite extends Source Resource::TYPE_USER, Transfer::GROUP_AUTH, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e )); } @@ -515,7 +515,7 @@ class Appwrite extends Source Resource::TYPE_TEAM, Transfer::GROUP_AUTH, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e )); } @@ -529,7 +529,7 @@ class Appwrite extends Source Resource::TYPE_MEMBERSHIP, Transfer::GROUP_AUTH, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e )); } @@ -669,7 +669,7 @@ class Appwrite extends Source foreach ($response['memberships'] as $membership) { $user = $cacheUsers[$membership['userId']] ?? null; if ($user === null) { - throw new \Exception('User not found'); + throw new \Exception('User not found', Exception::CODE_NOT_FOUND); } $memberships[] = new Membership( @@ -704,7 +704,7 @@ class Appwrite extends Source Resource::TYPE_DATABASE, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); @@ -722,7 +722,7 @@ class Appwrite extends Source Resource::TYPE_TABLE, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); @@ -740,7 +740,7 @@ class Appwrite extends Source Resource::TYPE_COLUMN, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); @@ -758,7 +758,7 @@ class Appwrite extends Source Resource::TYPE_INDEX, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); @@ -776,7 +776,7 @@ class Appwrite extends Source Resource::TYPE_ROW, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); @@ -1343,7 +1343,7 @@ class Appwrite extends Source Resource::TYPE_BUCKET, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); @@ -1359,7 +1359,7 @@ class Appwrite extends Source Resource::TYPE_FILE, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); @@ -1512,7 +1512,7 @@ class Appwrite extends Source Resource::TYPE_FUNCTION, Transfer::GROUP_FUNCTIONS, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e )); } @@ -1525,7 +1525,7 @@ class Appwrite extends Source Resource::TYPE_DEPLOYMENT, Transfer::GROUP_FUNCTIONS, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e )); } @@ -1542,7 +1542,7 @@ class Appwrite extends Source Resource::TYPE_SITE, Transfer::GROUP_SITES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e )); } @@ -1555,7 +1555,7 @@ class Appwrite extends Source Resource::TYPE_SITE_DEPLOYMENT, Transfer::GROUP_SITES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e )); } diff --git a/src/Migration/Sources/Appwrite/Reader/Database.php b/src/Migration/Sources/Appwrite/Reader/Database.php index bad744ce06..9927610631 100644 --- a/src/Migration/Sources/Appwrite/Reader/Database.php +++ b/src/Migration/Sources/Appwrite/Reader/Database.php @@ -130,7 +130,7 @@ class Database implements Reader resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ); } @@ -155,7 +155,7 @@ class Database implements Reader resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ); } @@ -202,7 +202,7 @@ class Database implements Reader resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ); } @@ -264,7 +264,7 @@ class Database implements Reader resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ); } @@ -310,7 +310,7 @@ class Database implements Reader resourceGroup: $resource->getGroup(), resourceId: $resource->getId(), message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ); } diff --git a/src/Migration/Sources/CSV.php b/src/Migration/Sources/CSV.php index 821db4bfd7..1ef1d2a37c 100644 --- a/src/Migration/Sources/CSV.php +++ b/src/Migration/Sources/CSV.php @@ -113,7 +113,7 @@ class CSV extends Source UtopiaResource::TYPE_ROW, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); diff --git a/src/Migration/Sources/Firebase.php b/src/Migration/Sources/Firebase.php index b1ef5b725c..4bcdc9c137 100644 --- a/src/Migration/Sources/Firebase.php +++ b/src/Migration/Sources/Firebase.php @@ -145,7 +145,7 @@ class Firebase extends Source { // Check our service account is valid if (! isset($this->serviceAccount['project_id'])) { - throw new \Exception('Invalid Firebase Service Account'); + throw new \Exception('Invalid Firebase Service Account', Exception::CODE_UNAUTHORIZED); } $this->authenticate(); @@ -155,15 +155,15 @@ class Firebase extends Source $scopes = explode(' ', $scopes); if (! in_array('https://www.googleapis.com/auth/firebase', $scopes)) { - throw new \Exception('Firebase Scope Missing'); + throw new \Exception('Firebase Scope Missing', Exception::CODE_VALIDATION); } if (! in_array('https://www.googleapis.com/auth/cloud-platform', $scopes)) { - throw new \Exception('Cloud Platform Scope Missing'); + throw new \Exception('Cloud Platform Scope Missing', Exception::CODE_VALIDATION); } if (! in_array('https://www.googleapis.com/auth/datastore', $scopes)) { - throw new \Exception('Datastore Scope Missing'); + throw new \Exception('Datastore Scope Missing', Exception::CODE_VALIDATION); } return []; @@ -195,7 +195,7 @@ class Firebase extends Source Resource::TYPE_USER, Transfer::GROUP_AUTH, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); @@ -293,7 +293,7 @@ class Firebase extends Source Resource::TYPE_DATABASE, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); @@ -310,7 +310,7 @@ class Firebase extends Source Resource::TYPE_TABLE, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); @@ -469,7 +469,7 @@ class Firebase extends Source } elseif (array_key_exists('arrayValue', $field)) { return $this->calculateArrayType($column, $key, $field['arrayValue']); } else { - throw new \Exception('Unknown field type'); + throw new \Exception('Unknown field type', Exception::CODE_VALIDATION); } } @@ -592,7 +592,7 @@ class Firebase extends Source } elseif (array_key_exists('referenceValue', $field)) { //TODO: } else { - throw new \Exception('Unknown field type'); + throw new \Exception('Unknown field type', Exception::CODE_VALIDATION); } } @@ -649,7 +649,7 @@ class Firebase extends Source Resource::TYPE_BUCKET, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e )); } @@ -663,7 +663,7 @@ class Firebase extends Source Resource::TYPE_FILE, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e )); } diff --git a/src/Migration/Sources/JSON.php b/src/Migration/Sources/JSON.php index 4fd0c8eb32..52bbb139d8 100644 --- a/src/Migration/Sources/JSON.php +++ b/src/Migration/Sources/JSON.php @@ -105,7 +105,7 @@ class JSON extends Source UtopiaResource::TYPE_ROW, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: (int) $e->getCode() ?: Exception::CODE_INTERNAL, previous: $e ) ); diff --git a/src/Migration/Sources/NHost.php b/src/Migration/Sources/NHost.php index 5c563248a9..2e288a93d7 100644 --- a/src/Migration/Sources/NHost.php +++ b/src/Migration/Sources/NHost.php @@ -25,6 +25,72 @@ use Utopia\Migration\Warning; class NHost extends Source { + protected static function statusCodeFromSqlState(?string $sqlState): int + { + if ($sqlState === null || $sqlState === '') { + return Exception::CODE_INTERNAL; + } + + // Common Postgres SQLSTATE mappings → HTTP-ish status codes + // Ref: https://www.postgresql.org/docs/current/errcodes-appendix.html + return match ($sqlState) { + // AuthN/AuthZ + '28P01', // invalid_password + '28000', // invalid_authorization_specification + => Exception::CODE_UNAUTHORIZED, + + '42501', // insufficient_privilege + => Exception::CODE_FORBIDDEN, + + // Not found + '3D000', // invalid_catalog_name (db does not exist) + '42P01', // undefined_table + => Exception::CODE_NOT_FOUND, + + // Validation-ish + '42703', // undefined_column + '42601', // syntax_error + => Exception::CODE_VALIDATION, + + // Conflict + '23505', // unique_violation + => Exception::CODE_CONFLICT, + + // Resource / service constraints + '53300', // too_many_connections + => Exception::CODE_RATE_LIMITED, + + // Connection exceptions (class 08) are typically user-side config/network issues + default => \str_starts_with($sqlState, '08') + ? Exception::CODE_VALIDATION + : Exception::CODE_INTERNAL, + }; + } + + protected static function statusCodeFromThrowable(\Throwable $e): int + { + $code = $e->getCode(); + + // PDO / Postgres typically exposes SQLSTATE as the exception code (string) + if (\is_string($code)) { + return self::statusCodeFromSqlState($code); + } + + if (\is_int($code) && $code > 0) { + return $code; + } + + return Exception::CODE_INTERNAL; + } + + /** + * @param array|null $errorInfo PDOStatement::errorInfo() + */ + protected static function statusCodeFromErrorInfo(?array $errorInfo): int + { + $sqlState = $errorInfo[0] ?? null; + return self::statusCodeFromSqlState(\is_string($sqlState) ? $sqlState : null); + } /** * @var \PDO */ @@ -64,7 +130,7 @@ class NHost extends Source try { $this->pdo = new \PDO('pgsql:host='.$this->subdomain.'.db.'.$this->region.'.nhost.run'.';port='.$this->port.';dbname='.$this->databaseName, $this->username, $this->password); } catch (\PDOException $e) { - throw new \Exception('Failed to connect to database: '.$e->getMessage()); + throw new \Exception('Failed to connect to database: '.$e->getMessage(), self::statusCodeFromThrowable($e), $e); } } @@ -111,11 +177,14 @@ class NHost extends Source try { $db = $this->getDatabase(); } catch (\PDOException $e) { - throw new \Exception('Failed to connect to database. PDO Code: '.$e->getCode().' Error: '.$e->getMessage()); + throw new \Exception('Failed to connect to database. PDO Code: '.$e->getCode().' Error: '.$e->getMessage(), self::statusCodeFromThrowable($e), $e); } if (! empty($db->errorCode())) { - throw new \Exception('Failed to connect to database. PDO Code: '.$db->errorCode().(empty($db->errorInfo()[2]) ? '' : ' Error: '.$db->errorInfo()[2])); + throw new \Exception( + 'Failed to connect to database. PDO Code: '.$db->errorCode().(empty($db->errorInfo()[2]) ? '' : ' Error: '.$db->errorInfo()[2]), + self::statusCodeFromSqlState($db->errorCode()) + ); } // Auth @@ -124,7 +193,10 @@ class NHost extends Source $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access users table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access users table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_USER] = $statement->fetchColumn(); @@ -140,7 +212,10 @@ class NHost extends Source $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access tables table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access tables table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_TABLE] = $statement->fetchColumn(); @@ -151,7 +226,10 @@ class NHost extends Source $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access columns table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access columns table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_COLUMN] = $statement->fetchColumn(); @@ -162,7 +240,10 @@ class NHost extends Source $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access indexes table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access indexes table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_INDEX] = $statement->fetchColumn(); @@ -173,7 +254,10 @@ class NHost extends Source $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access tables table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access tables table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_ROW] = $statement->fetchColumn(); @@ -185,7 +269,10 @@ class NHost extends Source $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access buckets table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access buckets table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_BUCKET] = $statement->fetchColumn(); @@ -196,7 +283,10 @@ class NHost extends Source $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access files table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access files table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report[Resource::TYPE_FILE] = $statement->fetchColumn(); @@ -205,7 +295,10 @@ class NHost extends Source $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access files table. Error: '.$statement->errorInfo()[2]); + throw new \Exception( + 'Failed to access files table. Error: '.$statement->errorInfo()[2], + self::statusCodeFromErrorInfo($statement->errorInfo()) + ); } $report['size'] = ($statement->fetchColumn()) / 1024 / 1024; // MB; @@ -227,7 +320,7 @@ class NHost extends Source Resource::TYPE_USER, Transfer::GROUP_AUTH, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e )); } @@ -293,7 +386,7 @@ class NHost extends Source Resource::TYPE_DATABASE, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e ) ); @@ -309,7 +402,7 @@ class NHost extends Source Resource::TYPE_TABLE, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e ) ); @@ -325,7 +418,7 @@ class NHost extends Source Resource::TYPE_COLUMN, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e ) ); @@ -341,7 +434,7 @@ class NHost extends Source Resource::TYPE_ROW, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e ) ); @@ -357,7 +450,7 @@ class NHost extends Source Resource::TYPE_INDEX, Transfer::GROUP_DATABASES, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e ) ); @@ -705,7 +798,7 @@ class NHost extends Source Resource::TYPE_BUCKET, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e ) ); @@ -721,7 +814,7 @@ class NHost extends Source Resource::TYPE_FILE, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: $e->getCode(), + code: self::statusCodeFromThrowable($e), previous: $e ) ); diff --git a/src/Migration/Sources/Supabase.php b/src/Migration/Sources/Supabase.php index 1d7f1aa50b..8b512ab618 100644 --- a/src/Migration/Sources/Supabase.php +++ b/src/Migration/Sources/Supabase.php @@ -209,74 +209,6 @@ class Supabase extends NHost protected string $host; - private static function statusCodeFromSqlState(?string $sqlState): int - { - if ($sqlState === null || $sqlState === '') { - return Exception::CODE_INTERNAL; - } - - // Common Postgres SQLSTATE mappings → HTTP-ish status codes - // Ref: https://www.postgresql.org/docs/current/errcodes-appendix.html - return match ($sqlState) { - // AuthN/AuthZ - '28P01', // invalid_password - '28000', // invalid_authorization_specification - => Exception::CODE_UNAUTHORIZED, - - '42501', // insufficient_privilege - => Exception::CODE_FORBIDDEN, - - // Not found - '3D000', // invalid_catalog_name (db does not exist) - '42P01', // undefined_table - => Exception::CODE_NOT_FOUND, - - // Validation-ish - '42703', // undefined_column - '42601', // syntax_error - => Exception::CODE_VALIDATION, - - // Conflict - '23505', // unique_violation - => Exception::CODE_CONFLICT, - - // Resource / service constraints - '53300', // too_many_connections - => Exception::CODE_RATE_LIMITED, - - // Connection exceptions (class 08) are typically user-side config/network issues - // (wrong host/port, blocked egress, DNS issues), so treat as validation/user actionable. - default => \str_starts_with($sqlState, '08') - ? Exception::CODE_VALIDATION - : Exception::CODE_INTERNAL, - }; - } - - private static function statusCodeFromThrowable(\Throwable $e): int - { - $code = $e->getCode(); - - // PDO / Postgres typically exposes SQLSTATE as the exception code (string) - if (\is_string($code)) { - return self::statusCodeFromSqlState($code); - } - - if (\is_int($code) && $code > 0) { - return $code; - } - - return Exception::CODE_INTERNAL; - } - - /** - * @param array|null $errorInfo PDOStatement::errorInfo() - */ - private static function statusCodeFromErrorInfo(?array $errorInfo): int - { - $sqlState = $errorInfo[0] ?? null; - return self::statusCodeFromSqlState(\is_string($sqlState) ? $sqlState : null); - } - public function __construct(string $endpoint, string $key, string $host, string $databaseName, string $username, string $password, string $port = '5432') { $this->endpoint = $endpoint; @@ -293,7 +225,7 @@ class Supabase extends NHost try { $this->pdo = new \PDO('pgsql:host='.$this->host.';port='.$this->port.';dbname='.$this->databaseName, $this->username, $this->password); } catch (\PDOException $e) { - throw new \Exception('Failed to connect to database: '.$e->getMessage(), self::statusCodeFromThrowable($e), $e); + throw new \Exception('Failed to connect to database: '.$e->getMessage(), parent::statusCodeFromThrowable($e), $e); } } @@ -308,13 +240,13 @@ class Supabase extends NHost try { $this->pdo = new \PDO('pgsql:host='.$this->host.';port='.$this->port.';dbname='.$this->databaseName, $this->username, $this->password); } catch (\PDOException $e) { - throw new \Exception('Failed to connect to database. PDO Code: '.$e->getCode().' Error: '.$e->getMessage(), self::statusCodeFromThrowable($e), $e); + throw new \Exception('Failed to connect to database. PDO Code: '.$e->getCode().' Error: '.$e->getMessage(), parent::statusCodeFromThrowable($e), $e); } if (! empty($this->pdo->errorCode())) { throw new \Exception( 'Failed to connect to database. PDO Code: '.$this->pdo->errorCode().(empty($this->pdo->errorInfo()[2]) ? '' : ' Error: '.$this->pdo->errorInfo()[2]), - self::statusCodeFromSqlState($this->pdo->errorCode()) + parent::statusCodeFromSqlState($this->pdo->errorCode()) ); } @@ -326,7 +258,7 @@ class Supabase extends NHost if ($statement->errorCode() !== '00000') { throw new \Exception( 'Failed to access users table. Error: '.$statement->errorInfo()[2], - self::statusCodeFromErrorInfo($statement->errorInfo()) + parent::statusCodeFromErrorInfo($statement->errorInfo()) ); } @@ -345,7 +277,7 @@ class Supabase extends NHost if ($statement->errorCode() !== '00000') { throw new \Exception( 'Failed to access tables table. Error: '.$statement->errorInfo()[2], - self::statusCodeFromErrorInfo($statement->errorInfo()) + parent::statusCodeFromErrorInfo($statement->errorInfo()) ); } @@ -359,7 +291,7 @@ class Supabase extends NHost if ($statement->errorCode() !== '00000') { throw new \Exception( 'Failed to access columns table. Error: '.$statement->errorInfo()[2], - self::statusCodeFromErrorInfo($statement->errorInfo()) + parent::statusCodeFromErrorInfo($statement->errorInfo()) ); } @@ -373,7 +305,7 @@ class Supabase extends NHost if ($statement->errorCode() !== '00000') { throw new \Exception( 'Failed to access indexes table. Error: '.$statement->errorInfo()[2], - self::statusCodeFromErrorInfo($statement->errorInfo()) + parent::statusCodeFromErrorInfo($statement->errorInfo()) ); } @@ -387,7 +319,7 @@ class Supabase extends NHost if ($statement->errorCode() !== '00000') { throw new \Exception( 'Failed to access tables table. Error: '.$statement->errorInfo()[2], - self::statusCodeFromErrorInfo($statement->errorInfo()) + parent::statusCodeFromErrorInfo($statement->errorInfo()) ); } @@ -402,7 +334,7 @@ class Supabase extends NHost if ($statement->errorCode() !== '00000') { throw new \Exception( 'Failed to access buckets table. Error: '.$statement->errorInfo()[2], - self::statusCodeFromErrorInfo($statement->errorInfo()) + parent::statusCodeFromErrorInfo($statement->errorInfo()) ); } @@ -416,7 +348,7 @@ class Supabase extends NHost if ($statement->errorCode() !== '00000') { throw new \Exception( 'Failed to access files table. Error: '.$statement->errorInfo()[2], - self::statusCodeFromErrorInfo($statement->errorInfo()) + parent::statusCodeFromErrorInfo($statement->errorInfo()) ); } @@ -449,7 +381,7 @@ class Supabase extends NHost Resource::TYPE_BUCKET, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: self::statusCodeFromThrowable($e), + code: parent::statusCodeFromThrowable($e), previous: $e )); } @@ -523,7 +455,7 @@ class Supabase extends NHost Resource::TYPE_BUCKET, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: self::statusCodeFromThrowable($e), + code: parent::statusCodeFromThrowable($e), previous: $e )); } @@ -537,7 +469,7 @@ class Supabase extends NHost Resource::TYPE_BUCKET, Transfer::GROUP_STORAGE, message: $e->getMessage(), - code: self::statusCodeFromThrowable($e), + code: parent::statusCodeFromThrowable($e), previous: $e )); }