1
0
Fork 0
mirror of synced 2024-06-26 10:10:57 +12:00

feat: update error codes in the storage API

This commit is contained in:
Christy Jacob 2022-02-06 18:44:48 +04:00
parent 4905792bb5
commit 9ad151006b
3 changed files with 27 additions and 15 deletions

View file

@ -214,6 +214,16 @@ return [
'description' => 'The requested file could not be found.', 'description' => 'The requested file could not be found.',
'statusCode' => 404, '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 => [ Exception::STORAGE_FILE_NOT_READABLE => [
'name' => Exception::STORAGE_FILE_NOT_READABLE, 'name' => Exception::STORAGE_FILE_NOT_READABLE,
'description' => 'There was an error reading the file from disk.', 'description' => 'There was an error reading the file from disk.',

View file

@ -213,7 +213,7 @@ App::get('/v1/storage/files')
$cursorFile = $dbForProject->getDocument('files', $cursor); $cursorFile = $dbForProject->getDocument('files', $cursor);
if ($cursorFile->isEmpty()) { 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); $file = $dbForProject->getDocument('files', $fileId);
if (empty($file->getId())) { if (empty($file->getId())) {
throw new Exception('File not found', 404); throw new Exception('File not found', 404, Exception::STORAGE_FILE_NOT_FOUND);
} }
$usage $usage
->setParam('storage.files.read', 1) ->setParam('storage.files.read', 1)
@ -304,11 +304,11 @@ App::get('/v1/storage/files/:fileId/preview')
$storage = 'files'; $storage = 'files';
if (!\extension_loaded('imagick')) { 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)) { 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 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); $file = $dbForProject->getDocument('files', $fileId);
if (empty($file->getId())) { 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'); $path = $file->getAttribute('path');
@ -347,7 +347,7 @@ App::get('/v1/storage/files/:fileId/preview')
$device = Storage::getDevice('files'); $device = Storage::getDevice('files');
if (!\file_exists($path)) { 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 $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); $file = $dbForProject->getDocument('files', $fileId);
if (empty($file->getId())) { 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', ''); $path = $file->getAttribute('path', '');
if (!\file_exists($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(); $compressor = new GZIP();
@ -516,13 +516,13 @@ App::get('/v1/storage/files/:fileId/view')
$mimes = Config::getParam('storage-mimes'); $mimes = Config::getParam('storage-mimes');
if (empty($file->getId())) { 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', ''); $path = $file->getAttribute('path', '');
if (!\file_exists($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(); $compressor = new GZIP();
@ -602,12 +602,12 @@ App::put('/v1/storage/files/:fileId')
if (!Auth::isAppUser($roles) && !Auth::isPrivilegedUser($roles)) { if (!Auth::isAppUser($roles) && !Auth::isPrivilegedUser($roles)) {
foreach ($read as $role) { foreach ($read as $role) {
if (!Authorization::isRole($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) { foreach ($write as $role) {
if (!Authorization::isRole($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); $file = $dbForProject->getDocument('files', $fileId);
if (empty($file->getId())) { 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(), [ $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); $file = $dbForProject->getDocument('files', $fileId);
if (empty($file->getId())) { 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'); $device = Storage::getDevice('files');
if ($device->delete($file->getAttribute('path', ''))) { if ($device->delete($file->getAttribute('path', ''))) {
if (!$dbForProject->deleteDocument('files', $fileId)) { 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);
} }
} }

View file

@ -62,6 +62,8 @@ class Exception extends \Exception
/** Storage */ /** Storage */
const STORAGE_FILE_NOT_FOUND = 'storage_file_not_found'; 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_FILE_NOT_READABLE = 'storage_file_not_readable';
const STORAGE_INVALID_READ_PERMISSIONS = 'storage_invalid_read_permissions'; const STORAGE_INVALID_READ_PERMISSIONS = 'storage_invalid_read_permissions';
const STORAGE_INVALID_WRITE_PERMISSIONS = 'storage_invalid_write_permissions'; const STORAGE_INVALID_WRITE_PERMISSIONS = 'storage_invalid_write_permissions';