From 93aaa9f188182525716c365d33226098d462b5c8 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 10 Mar 2026 11:31:59 +0530 Subject: [PATCH] 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 )); }