From 53807ab47c4a6abdedf29b572def9446e3ab4184 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sun, 6 Feb 2022 21:21:47 +0400 Subject: [PATCH] feat: update error codes in the health API --- app/controllers/api/health.php | 14 +++++++------- src/Appwrite/Extend/Exception.php | 9 ++++++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/health.php b/app/controllers/api/health.php index bcfbbd20c..fdc8101f5 100644 --- a/app/controllers/api/health.php +++ b/app/controllers/api/health.php @@ -2,7 +2,7 @@ use Appwrite\Utopia\Response; use Utopia\App; -use Utopia\Exception; +use Appwrite\Extend\Exception; use Utopia\Storage\Device\Local; use Utopia\Storage\Storage; use Appwrite\ClamAV\Network; @@ -75,7 +75,7 @@ App::get('/v1/health/db') $statement->execute(); } catch (Exception $_e) { - throw new Exception('Database is not available', 500); + throw new Exception('Database is not available', 500, Exception::DATABASE_NOT_AVAILABLE); } $output = [ @@ -109,7 +109,7 @@ App::get('/v1/health/cache') $redis = $utopia->getResource('cache'); if (!$redis->ping(true)) { - throw new Exception('Cache is not available', 500); + throw new Exception('Cache is not available', 500, Exception::CACHE_NOT_AVAILABLE); } $output = [ @@ -166,7 +166,7 @@ App::get('/v1/health/time') $diff = ($timestamp - \time()); if ($diff > $gap || $diff < ($gap * -1)) { - throw new Exception('Server time gaps detected'); + throw new Exception('Server time gaps detected', 500, Exception::TIME_GAPS_DETECTED); } $output = [ @@ -294,11 +294,11 @@ App::get('/v1/health/storage/local') $device = new Local($volume); if (!\is_readable($device->getRoot())) { - throw new Exception('Device '.$key.' dir is not readable'); + throw new Exception('Device '.$key.' dir is not readable', 500, Exception::STORAGE_NOT_READABLE); } if (!\is_writable($device->getRoot())) { - throw new Exception('Device '.$key.' dir is not writable'); + throw new Exception('Device '.$key.' dir is not writable', 500, Exception::STORAGE_NOT_WRITABLE); } } @@ -341,7 +341,7 @@ App::get('/v1/health/anti-virus') $output['version'] = @$antivirus->version(); $output['status'] = (@$antivirus->ping()) ? 'pass' : 'fail'; } catch( \Exception $e) { - throw new Exception('Antivirus is not available', 500); + throw new Exception('Antivirus is not available', 500, Exception::ANTIVIRUS_NOT_AVAILABLE); } } diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index ccab54662..5bad4ad3a 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -75,6 +75,7 @@ class Exception extends \Exception const AVATAR_ICON_NOT_FOUND = 'avatar_icon_not_found'; /** Storage */ + const STORAGE_ERROR = 'storage_error'; const STORAGE_FILE_NOT_FOUND = 'storage_file_not_found'; const STORAGE_DEVICE_NOT_FOUND = 'storage_device_not_found'; const STORAGE_FILE_DELETION_FAILED = 'storage_file_deletion_failed'; @@ -155,9 +156,15 @@ class Exception extends \Exception const INVALID_READ_PERMISSIONS = 'invalid_read_permissions'; const INVALID_WRITE_PERMISSIONS = 'invalid_write_permissions'; const UNAUTHORIZED_SCOPE = 'unauthorized_scope'; - const STORAGE_ERROR = 'storage_error'; const RATE_LIMIT_EXCEEDED = 'rate_limit_exceeded'; const SMTP_DISABLED = 'smtp_disabled'; + const DATABASE_NOT_AVAILABLE = 'database_not_available'; + const CACHE_NOT_AVAILABLE = 'cache_not_available'; + const TIME_GAPS_DETECTED = 'time_gaps_detected'; + const STORAGE_NOT_READABLE = 'storage_not_readable'; + const STORAGE_NOT_WRITABLE = 'storage_not_writable'; + const ANTIVIRUS_NOT_AVAILABLE = 'antivirus_not_available'; + private $errorCode = '';