mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
refactor: standardize exception handling with appropriate error codes across migration sources and destinations
This commit is contained in:
1 parent
93aaa9f188
commit
9e70312e3d
10 files changed
+191
-154
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
));
|
||||
}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
)
|
||||
);
|
||||
|
||||
@@ -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
|
||||
));
|
||||
}
|
||||
|
||||
@@ -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
|
||||
)
|
||||
);
|
||||
|
||||
+112
-19
@@ -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<int, mixed>|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
|
||||
)
|
||||
);
|
||||
|
||||
@@ -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<int, mixed>|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
|
||||
));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user