1
0
Fork 0
mirror of synced 2024-06-26 18:20:43 +12:00

feat: update error codes in the storage API

This commit is contained in:
Christy Jacob 2022-02-06 18:32:28 +04:00
parent 42fcb291d4
commit 2c14abb143
4 changed files with 32 additions and 14 deletions

View file

@ -208,15 +208,30 @@ return [
],
/** Files */
Exception::FILE_NOT_FOUND => [
'name' => Exception::FILE_NOT_FOUND,
/** Storage */
Exception::STORAGE_FILE_NOT_FOUND => [
'name' => Exception::STORAGE_FILE_NOT_FOUND,
'description' => 'The requested file could not be found.',
'statusCode' => 404,
],
Exception::FILE_NOT_READABLE => [
'name' => Exception::FILE_NOT_READABLE,
Exception::STORAGE_FILE_NOT_READABLE => [
'name' => Exception::STORAGE_FILE_NOT_READABLE,
'description' => 'There was an error reading the file from disk.',
'statusCode' => 500,
],
Exception::STORAGE_INVALID_READ_PERMISSIONS => [
'name' => Exception::STORAGE_INVALID_READ_PERMISSIONS,
'description' => 'Invalid format for read permissions. Please check the documentation.',
'statusCode' => 400,
],
Exception::STORAGE_INVALID_WRITE_PERMISSIONS => [
'name' => Exception::STORAGE_INVALID_WRITE_PERMISSIONS,
'description' => 'Invalid format for write permissions. Please check the documentation.',
'statusCode' => 400,
],
Exception::STORAGE_INVALID_FILE_SIZE => [
'name' => Exception::STORAGE_INVALID_FILE_SIZE,
'description' => 'The file size is either not valid or exceeds the maximum allowed size.',
'statusCode' => 400,
],
];

View file

@ -43,7 +43,7 @@ $avatarCallback = function ($type, $code, $width, $height, $quality, $response)
$type = 'png';
if (!\is_readable($path)) {
throw new Exception('File not readable in ' . $path, 500, Exception::FILE_NOT_READABLE);
throw new Exception('File not readable in ' . $path, 500, Exception::STORAGE_FILE_NOT_READABLE);
}
$cache = new Cache(new Filesystem(APP_STORAGE_CACHE . '/app-0')); // Limit file number or size

View file

@ -2,7 +2,7 @@
use Appwrite\Auth\Auth;
use Utopia\App;
use Utopia\Exception;
use Appwrite\Extend\Exception
use Utopia\Validator\ArrayList;
use Utopia\Validator\WhiteList;
use Utopia\Validator\Range;
@ -68,12 +68,12 @@ App::post('/v1/storage/files')
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);
}
}
}
@ -102,13 +102,13 @@ App::post('/v1/storage/files')
//}
if (!$fileSize->isValid($file['size'])) { // Check if file size is exceeding allowed limit
throw new Exception('File size not allowed', 400);
throw new Exception('File size not allowed', 400, Exception::STORAGE_INVALID_FILE_SIZE);
}
$device = Storage::getDevice('files');
if (!$upload->isValid($file['tmp_name'])) {
throw new Exception('Invalid file', 403);
throw new Exception('Invalid file', 403, );
}
// Save to storage

View file

@ -60,9 +60,12 @@ class Exception extends \Exception
const AVATAR_REMOTE_URL_FAILED = 'avatar_remote_url_failed';
const AVATAR_ICON_NOT_FOUND = 'avatar_icon_not_found';
/** Files */
const FILE_NOT_FOUND = 'file_not_found';
const FILE_NOT_READABLE = 'file_not_readable';
/** Storage */
const STORAGE_FILE_NOT_FOUND = 'storage_file_not_found';
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';
const STORAGE_INVALID_FILE_SIZE = 'storage_invalid_file_size';
/** Projects */
const PROJECT_NOT_FOUND = 'project_not_found';