From 785af114f009cf4b7c9e4b86db15026fb6b4c17a Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sun, 30 Jan 2022 02:47:35 +0400 Subject: [PATCH] feat: load error codes in exception --- app/init.php | 1 + src/Appwrite/Extend/Exception.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/app/init.php b/app/init.php index d591d1ddb..cd0ffc8a1 100644 --- a/app/init.php +++ b/app/init.php @@ -127,6 +127,7 @@ App::setMode(App::getEnv('_APP_ENV', App::MODE_TYPE_PRODUCTION)); */ Config::load('events', __DIR__.'/config/events.php'); Config::load('auth', __DIR__.'/config/auth.php'); +Config::load('errorCodes', __DIR__.'/config/errorCodes.php'); Config::load('providers', __DIR__.'/config/providers.php'); Config::load('platforms', __DIR__.'/config/platforms.php'); Config::load('collections', __DIR__.'/config/collections.php'); diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index e68e997da..26a4d4179 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -2,6 +2,8 @@ namespace Appwrite\Extend\Exception; +use Utopia\Config\Config; + class Exception extends \Exception { /** @@ -49,10 +51,20 @@ class Exception extends \Exception private $errorCode = ''; + static $codes = Config::getParam('errorCodes', []); + public function __construct(string $message, int $code = 0, string $errorCode = Exception::TYPE_NONE, \Throwable $previous = null) { + if (!isset(self::$codes)) { + throw new \Exception('Error codes not found', 500); + } + $this->errorCode = $errorCode; + if (isset(self::$codes[$errorCode])) { + $this->$message = self::$codes[$errorCode]['statusCode']; + } + parent::__construct($message, $code, $previous); }