From 9ad151006b64c893e9f8364c793367bbef7bea8e Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sun, 6 Feb 2022 18:44:48 +0400 Subject: [PATCH] feat: update error codes in the storage API --- app/config/errors.php | 10 ++++++++++ app/controllers/api/storage.php | 30 +++++++++++++++--------------- src/Appwrite/Extend/Exception.php | 2 ++ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index fc5d8ab87..22d72f8ac 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -214,6 +214,16 @@ return [ 'description' => 'The requested file could not be found.', 'statusCode' => 404, ], + Exception::STORAGE_DEVICE_NOT_FOUND => [ + 'name' => Exception::STORAGE_DEVICE_NOT_FOUND, + 'description' => 'The requested storage device could not be found.', + 'statusCode' => 400, + ], + Exception::STORAGE_FAILED_TO_DELETE_FILE => [ + 'name' => Exception::STORAGE_FAILED_TO_DELETE_FILE, + 'description' => 'There was an issue deleting the file from the database', + 'statusCode' => 500, + ], Exception::STORAGE_FILE_NOT_READABLE => [ 'name' => Exception::STORAGE_FILE_NOT_READABLE, 'description' => 'There was an error reading the file from disk.', diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index b6e014eb7..a74240e71 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -213,7 +213,7 @@ App::get('/v1/storage/files') $cursorFile = $dbForProject->getDocument('files', $cursor); if ($cursorFile->isEmpty()) { - throw new Exception("File '{$cursor}' for the 'cursor' value not found.", 400); + throw new Exception("File '{$cursor}' for the 'cursor' value not found.", 400, Exception::STORAGE_FILE_NOT_FOUND); } } @@ -257,7 +257,7 @@ App::get('/v1/storage/files/:fileId') $file = $dbForProject->getDocument('files', $fileId); if (empty($file->getId())) { - throw new Exception('File not found', 404); + throw new Exception('File not found', 404, Exception::STORAGE_FILE_NOT_FOUND); } $usage ->setParam('storage.files.read', 1) @@ -304,11 +304,11 @@ App::get('/v1/storage/files/:fileId/preview') $storage = 'files'; if (!\extension_loaded('imagick')) { - throw new Exception('Imagick extension is missing', 500); + throw new Exception('Imagick extension is missing', 500, Exception::IMAGIC_EXTENSION_MISSING); } if (!Storage::exists($storage)) { - throw new Exception('No such storage device', 400); + throw new Exception('No such storage device', 400, Exception::STORAGE_DEVICE_NOT_FOUND); } if ((\strpos($request->getAccept(), 'image/webp') === false) && ('webp' == $output)) { // Fallback webp to jpeg when no browser support @@ -325,7 +325,7 @@ App::get('/v1/storage/files/:fileId/preview') $file = $dbForProject->getDocument('files', $fileId); if (empty($file->getId())) { - throw new Exception('File not found', 404); + throw new Exception('File not found', 404, Exception::STORAGE_FILE_NOT_FOUND); } $path = $file->getAttribute('path'); @@ -347,7 +347,7 @@ App::get('/v1/storage/files/:fileId/preview') $device = Storage::getDevice('files'); if (!\file_exists($path)) { - throw new Exception('File not found', 404); + throw new Exception('File not found', 404, Exception::STORAGE_FILE_NOT_FOUND); } $cache = new Cache(new Filesystem(APP_STORAGE_CACHE.'/app-'.$project->getId())); // Limit file number or size @@ -450,13 +450,13 @@ App::get('/v1/storage/files/:fileId/download') $file = $dbForProject->getDocument('files', $fileId); if (empty($file->getId())) { - throw new Exception('File not found', 404); + throw new Exception('File not found', 404, Exception::STORAGE_FILE_NOT_FOUND); } $path = $file->getAttribute('path', ''); if (!\file_exists($path)) { - throw new Exception('File not found in '.$path, 404); + throw new Exception('File not found in '.$path, 404, Exception::STORAGE_FILE_NOT_FOUND); } $compressor = new GZIP(); @@ -516,13 +516,13 @@ App::get('/v1/storage/files/:fileId/view') $mimes = Config::getParam('storage-mimes'); if (empty($file->getId())) { - throw new Exception('File not found', 404); + throw new Exception('File not found', 404, Exception::STORAGE_FILE_NOT_FOUND); } $path = $file->getAttribute('path', ''); if (!\file_exists($path)) { - throw new Exception('File not found in '.$path, 404); + throw new Exception('File not found in '.$path, 404, Exception::STORAGE_FILE_NOT_FOUND); } $compressor = new GZIP(); @@ -602,12 +602,12 @@ App::put('/v1/storage/files/:fileId') if (!Auth::isAppUser($roles) && !Auth::isPrivilegedUser($roles)) { foreach ($read as $role) { if (!Authorization::isRole($role)) { - throw new Exception('Read permissions must be one of: ('.\implode(', ', $roles).')', 400); + throw new Exception('Read permissions must be one of: ('.\implode(', ', $roles).')', 400, Exception::STORAGE_INVALID_READ_PERMISSIONS); } } foreach ($write as $role) { if (!Authorization::isRole($role)) { - throw new Exception('Write permissions must be one of: ('.\implode(', ', $roles).')', 400); + throw new Exception('Write permissions must be one of: ('.\implode(', ', $roles).')', 400, Exception::STORAGE_INVALID_WRITE_PERMISSIONS); } } } @@ -615,7 +615,7 @@ App::put('/v1/storage/files/:fileId') $file = $dbForProject->getDocument('files', $fileId); if (empty($file->getId())) { - throw new Exception('File not found', 404); + throw new Exception('File not found', 404, Exception::STORAGE_FILE_NOT_FOUND); } $file = $dbForProject->updateDocument('files', $fileId, new Document(\array_merge($file->getArrayCopy(), [ @@ -664,14 +664,14 @@ App::delete('/v1/storage/files/:fileId') $file = $dbForProject->getDocument('files', $fileId); if (empty($file->getId())) { - throw new Exception('File not found', 404); + throw new Exception('File not found', 404, Exception::STORAGE_FILE_NOT_FOUND); } $device = Storage::getDevice('files'); if ($device->delete($file->getAttribute('path', ''))) { if (!$dbForProject->deleteDocument('files', $fileId)) { - throw new Exception('Failed to remove file from DB', 500); + throw new Exception('Failed to remove file from DB', 500, Exception::STORAGE_FAILED_TO_DELETE_FILE); } } diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 2b585d4b5..381fe04c9 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -62,6 +62,8 @@ class Exception extends \Exception /** Storage */ const STORAGE_FILE_NOT_FOUND = 'storage_file_not_found'; + const STORAGE_DEVICE_NOT_FOUND = 'storage_device_not_found'; + const STORAGE_FAILED_TO_DELETE_FILE = 'storage_failed_to_delete_file'; const STORAGE_FILE_NOT_READABLE = 'storage_file_not_readable'; const STORAGE_INVALID_READ_PERMISSIONS = 'storage_invalid_read_permissions'; const STORAGE_INVALID_WRITE_PERMISSIONS = 'storage_invalid_write_permissions';