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.
This commit is contained in:
ArnabChatterjee20k committed 2026-03-09 15:40:45 +05:30
1 parent c2d016944c
commit cd91e119ce
7 files changed
+31 -14

No files matched your search

+1 -1
View File
@@ -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;
}
+1
View File
@@ -139,6 +139,7 @@ class JSON extends Destination
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: $e->getMessage(),
code: MigrationException::CODE_VALIDATION,
previous: $e,
));
continue;
+9 -1
View File
@@ -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
}
}
+1 -1
View File
@@ -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);
}
}
+8 -4
View File
@@ -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;
+8 -4
View File
@@ -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);
}
}
+3 -3
View File
@@ -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);
}
}