From d3dc11fdf358dd7c66d1e0b414663053c0386915 Mon Sep 17 00:00:00 2001 From: Dale Davies Date: Tue, 26 Jul 2022 14:35:10 +0100 Subject: [PATCH] Add and improve custom exception types for all cases --- jumpapp/classes/API/Icon.php | 4 +++- jumpapp/classes/Config.php | 6 +++--- jumpapp/classes/Exceptions/APIException.php | 16 ++++++++++++++ .../classes/Exceptions/ConfigException.php | 16 ++++++++++++++ .../Exceptions/SiteNotFoundException.php | 20 ++++++++++++++++++ .../Exceptions/TagNotFoundException.php | 6 +++++- jumpapp/classes/SearchEngines.php | 12 +++++------ jumpapp/classes/Sites.php | 21 ++++++++++--------- 8 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 jumpapp/classes/Exceptions/APIException.php create mode 100644 jumpapp/classes/Exceptions/ConfigException.php create mode 100644 jumpapp/classes/Exceptions/SiteNotFoundException.php diff --git a/jumpapp/classes/API/Icon.php b/jumpapp/classes/API/Icon.php index ad94ff3..722e3a7 100644 --- a/jumpapp/classes/API/Icon.php +++ b/jumpapp/classes/API/Icon.php @@ -13,11 +13,13 @@ namespace Jump\API; +use \Jump\Exceptions\APIException; + class Icon extends AbstractAPI { public function get_output(): string { if (!isset($this->routeparams['siteurl']) || empty($this->routeparams['siteurl'])) { - throw new \Exception('The siteurl query parameter is not provided or empty'); + throw new APIException('The siteurl query parameter is not provided or empty'); } $sites = new \Jump\Sites($this->config, $this->cache); diff --git a/jumpapp/classes/Config.php b/jumpapp/classes/Config.php index 9233bb6..e7797ac 100644 --- a/jumpapp/classes/Config.php +++ b/jumpapp/classes/Config.php @@ -13,7 +13,7 @@ namespace Jump; -use Exception; +use Jump\Exceptions\ConfigException; /** * Load, parse and enumerate all configuration paramaters requires throughout @@ -67,7 +67,7 @@ class Config { $this->add_wwwroot_to_base_paths(); $this->add_session_config(); if ($this->config_params_missing()) { - throw new Exception('Config.php must always contain... '.implode(', ', self::CONFIG_PARAMS)); + throw new ConfigException('Config.php must always contain... '.implode(', ', self::CONFIG_PARAMS)); } } @@ -115,7 +115,7 @@ class Config { */ public function get(string $key, $strict = true): mixed { if (!$this->config->has($key) && $strict === true) { - throw new Exception('Config key does not exist... ('.$key.')'); + throw new ConfigException('Config key does not exist... ('.$key.')'); } return $this->config->get($key); } diff --git a/jumpapp/classes/Exceptions/APIException.php b/jumpapp/classes/Exceptions/APIException.php new file mode 100644 index 0000000..ec09a2d --- /dev/null +++ b/jumpapp/classes/Exceptions/APIException.php @@ -0,0 +1,16 @@ + + * @copyright Copyright (c) 2022, Dale Davies + * @license MIT + */ + +namespace Jump\Exceptions; + +class APIException extends \Exception {} diff --git a/jumpapp/classes/Exceptions/ConfigException.php b/jumpapp/classes/Exceptions/ConfigException.php new file mode 100644 index 0000000..66ea794 --- /dev/null +++ b/jumpapp/classes/Exceptions/ConfigException.php @@ -0,0 +1,16 @@ + + * @copyright Copyright (c) 2022, Dale Davies + * @license MIT + */ + +namespace Jump\Exceptions; + +class ConfigException extends \Exception {} diff --git a/jumpapp/classes/Exceptions/SiteNotFoundException.php b/jumpapp/classes/Exceptions/SiteNotFoundException.php new file mode 100644 index 0000000..8962e9c --- /dev/null +++ b/jumpapp/classes/Exceptions/SiteNotFoundException.php @@ -0,0 +1,20 @@ + + * @copyright Copyright (c) 2022, Dale Davies + * @license MIT + */ + +namespace Jump\Exceptions; + +class SiteNotFoundException extends \Exception { + public function __construct(string $ref) { + parent::__construct('The site could not be found (' . $ref . ')'); + } +} diff --git a/jumpapp/classes/Exceptions/TagNotFoundException.php b/jumpapp/classes/Exceptions/TagNotFoundException.php index 0a844ee..e86a90f 100644 --- a/jumpapp/classes/Exceptions/TagNotFoundException.php +++ b/jumpapp/classes/Exceptions/TagNotFoundException.php @@ -13,4 +13,8 @@ namespace Jump\Exceptions; -class TagNotFoundException extends \Exception {} \ No newline at end of file +class TagNotFoundException extends \Exception { + public function __construct(string $tagname) { + parent::__construct('No sites have been tagged with "' . $tagname . '"'); + } +} diff --git a/jumpapp/classes/SearchEngines.php b/jumpapp/classes/SearchEngines.php index 6bf05b1..57ac85b 100644 --- a/jumpapp/classes/SearchEngines.php +++ b/jumpapp/classes/SearchEngines.php @@ -13,7 +13,7 @@ namespace Jump; -use \Exception; +use Jump\Exceptions\ConfigException; /** * Loads and validates the search engines defined in searchengines.json. @@ -46,29 +46,29 @@ class SearchEngines { * be decoded to an array. * * @return array AArray of parsed/validated search engine information from searchengines.json - * @throws Exception If searchengines.json cannot be found. + * @throws ConfigException If searchengines.json cannot be found. */ private function load_search_engines_from_json(): array { $searchengines = []; $rawjson = file_get_contents($this->searchfilelocation); if ($rawjson === false) { - throw new Exception('There was a problem loading the searchengines.json file'); + throw new ConfigException('There was a problem loading the searchengines.json file'); } if ($rawjson === '') { - throw new Exception('The searchengines.json file is empty'); + throw new ConfigException('The searchengines.json file is empty'); } // Do some checks to see if the JSON decodes into something // like what we expect to see... $decodedjson = json_decode($rawjson); if (!is_array($decodedjson)) { - throw new Exception('The searchengines.json file is invalid'); + throw new ConfigException('The searchengines.json file is invalid'); } // Build a new array using the values we need... foreach ($decodedjson as $item) { if (!isset($item->name, $item->url)) { - throw new Exception('The searchengines.json does not contain the "name" or "url" properties'); + throw new ConfigException('The searchengines.json does not contain the "name" or "url" properties'); } $searchengine = new \stdClass(); $searchengine->name = $item->name; diff --git a/jumpapp/classes/Sites.php b/jumpapp/classes/Sites.php index 2f3a546..6c0b27f 100644 --- a/jumpapp/classes/Sites.php +++ b/jumpapp/classes/Sites.php @@ -13,7 +13,8 @@ namespace Jump; -use \Exception; +use \Jump\Exceptions\ConfigException; +use \Jump\Exceptions\SiteNotFoundException; use \Jump\Exceptions\TagNotFoundException; /** @@ -67,16 +68,16 @@ class Sites { * be decoded to an array, * * @return array Array of Site objects sites loaded from sites.json - * @throws Exception If sites.json cannot be found. + * @throws ConfigException If sites.json cannot be found. */ private function load_sites_from_json(): array { $allsites = []; $rawjson = file_get_contents($this->sitesfilelocation); if ($rawjson === false) { - throw new Exception('There was a problem loading the sites.json file'); + throw new ConfigException('There was a problem loading the sites.json file'); } if ($rawjson === '') { - throw new Exception('The sites.json file is empty'); + throw new ConfigException('The sites.json file is empty'); } // Do some checks to see if the JSON decodes into something // like what we expect to see... @@ -132,12 +133,12 @@ class Sites { * * @param string $url The URL to search for. * @return Site A matching Site object if found. - * @throws Exception If a site with given URL does not exist. + * @throws SiteNotFoundException If a site with given URL does not exist. */ public function get_site_by_url(string $url): Site { $found = array_search($url, array_column($this->get_sites(), 'url')); if ($found === false) { - throw new Exception('The site could not be found ('.$url.')'); + throw new SiteNotFoundException($url); } return $this->loadedsites[$found]; } @@ -147,12 +148,12 @@ class Sites { * * @param string $id The Site ID to search for. * @return Site A matching Site object if found. - * @throws Exception If a site with given Site ID does not exist. + * @throws SiteNotFoundException If a site with given Site ID does not exist. */ public function get_site_by_id(string $id): Site { $found = array_search($id, array_column($this->get_sites(), 'id')); if ($found === false) { - throw new Exception('The site could not be found ('.$id.')'); + throw new SiteNotFoundException($id); } return $this->loadedsites[$found]; } @@ -162,11 +163,11 @@ class Sites { * * @param string $tagname The tag to look look up sites. * @return array Array of Site objects with the given tag. - * @throws Exception If there are no sites tagged with $tagname. + * @throws TagNotFoundException If there are no sites tagged with $tagname. */ public function get_sites_by_tag(string $tagname): array { if (!in_array($tagname, $this->tags)) { - throw new TagNotFoundException('No sites have been tagged with "'.$tagname.'"'); + throw new TagNotFoundException($tagname); } $found = []; foreach ($this->get_sites() as $site) {