Add and improve custom exception types for all cases

This commit is contained in:
Dale Davies
2022-07-26 14:35:10 +01:00
parent 5b9fe67021
commit d3dc11fdf3
8 changed files with 80 additions and 21 deletions

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -0,0 +1,16 @@
<?php
/**
* ██ ██ ██ ███ ███ ██████
* ██ ██ ██ ████ ████ ██ ██
* ██ ██ ██ ██ ████ ██ ██████
* ██ ██ ██ ██ ██ ██ ██ ██
* █████ ██████ ██ ██ ██
*
* @author Dale Davies <dale@daledavies.co.uk>
* @copyright Copyright (c) 2022, Dale Davies
* @license MIT
*/
namespace Jump\Exceptions;
class APIException extends \Exception {}

View File

@@ -0,0 +1,16 @@
<?php
/**
* ██ ██ ██ ███ ███ ██████
* ██ ██ ██ ████ ████ ██ ██
* ██ ██ ██ ██ ████ ██ ██████
* ██ ██ ██ ██ ██ ██ ██ ██
* █████ ██████ ██ ██ ██
*
* @author Dale Davies <dale@daledavies.co.uk>
* @copyright Copyright (c) 2022, Dale Davies
* @license MIT
*/
namespace Jump\Exceptions;
class ConfigException extends \Exception {}

View File

@@ -0,0 +1,20 @@
<?php
/**
* ██ ██ ██ ███ ███ ██████
* ██ ██ ██ ████ ████ ██ ██
* ██ ██ ██ ██ ████ ██ ██████
* ██ ██ ██ ██ ██ ██ ██ ██
* █████ ██████ ██ ██ ██
*
* @author Dale Davies <dale@daledavies.co.uk>
* @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 . ')');
}
}

View File

@@ -13,4 +13,8 @@
namespace Jump\Exceptions;
class TagNotFoundException extends \Exception {}
class TagNotFoundException extends \Exception {
public function __construct(string $tagname) {
parent::__construct('No sites have been tagged with "' . $tagname . '"');
}
}

View File

@@ -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;

View File

@@ -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) {