From 4c676b41e917039a50cc18330e349842710777ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 20 Aug 2019 12:14:56 +0200 Subject: [PATCH] Custom exceptions, fix #91 --- src/DatabaseManager.php | 5 +++-- .../DatabaseManagerNotRegisteredException.php | 11 +++++++++++ .../TenantCouldNotBeIdentifiedException.php | 11 +++++++++++ src/StorageDrivers/DatabaseStorageDriver.php | 3 ++- src/StorageDrivers/RedisStorageDriver.php | 3 ++- src/TenantManager.php | 5 +++-- 6 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 src/Exceptions/DatabaseManagerNotRegisteredException.php create mode 100644 src/Exceptions/TenantCouldNotBeIdentifiedException.php diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index 81864b46..50deb25b 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -5,6 +5,7 @@ namespace Stancl\Tenancy; use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator; use Stancl\Tenancy\Jobs\QueuedTenantDatabaseDeleter; use Illuminate\Database\DatabaseManager as BaseDatabaseManager; +use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException; final class DatabaseManager { @@ -53,7 +54,7 @@ final class DatabaseManager $databaseManagers = config('tenancy.database_managers'); if (! \array_key_exists($driver, $databaseManagers)) { - throw new \Exception("Database could not be created: no database manager for driver $driver is registered."); + throw new DatabaseManagerNotRegisteredException("Database could not be created", $driver); } if (config('tenancy.queue_database_creation', false)) { @@ -79,7 +80,7 @@ final class DatabaseManager $databaseManagers = config('tenancy.database_managers'); if (! \array_key_exists($driver, $databaseManagers)) { - throw new \Exception("Database could not be deleted: no database manager for driver $driver is registered."); + throw new DatabaseManagerNotRegisteredException("Database could not be deleted", $driver); } if (config('tenancy.queue_database_deletion', false)) { diff --git a/src/Exceptions/DatabaseManagerNotRegisteredException.php b/src/Exceptions/DatabaseManagerNotRegisteredException.php new file mode 100644 index 00000000..f836c943 --- /dev/null +++ b/src/Exceptions/DatabaseManagerNotRegisteredException.php @@ -0,0 +1,11 @@ +message = "$error: no database manager for driver $driver is registered."; + } +} diff --git a/src/Exceptions/TenantCouldNotBeIdentifiedException.php b/src/Exceptions/TenantCouldNotBeIdentifiedException.php new file mode 100644 index 00000000..15670aae --- /dev/null +++ b/src/Exceptions/TenantCouldNotBeIdentifiedException.php @@ -0,0 +1,11 @@ +message = "Tenant could not be identified on domain $domain"; + } +} \ No newline at end of file diff --git a/src/StorageDrivers/DatabaseStorageDriver.php b/src/StorageDrivers/DatabaseStorageDriver.php index fec1197a..4d6a8ff1 100644 --- a/src/StorageDrivers/DatabaseStorageDriver.php +++ b/src/StorageDrivers/DatabaseStorageDriver.php @@ -2,6 +2,7 @@ namespace Stancl\Tenancy\StorageDrivers; +use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Tenant; use Stancl\Tenancy\Interfaces\StorageDriver; @@ -16,7 +17,7 @@ class DatabaseStorageDriver implements StorageDriver { $id = $this->getTenantIdByDomain($domain); if (! $id) { - throw new \Exception("Tenant could not be identified on domain {$domain}"); + throw new TenantCouldNotBeIdentifiedException($domain); } return $this->getTenantById($id); diff --git a/src/StorageDrivers/RedisStorageDriver.php b/src/StorageDrivers/RedisStorageDriver.php index b2e4ab44..08372b65 100644 --- a/src/StorageDrivers/RedisStorageDriver.php +++ b/src/StorageDrivers/RedisStorageDriver.php @@ -3,6 +3,7 @@ namespace Stancl\Tenancy\StorageDrivers; use Illuminate\Support\Facades\Redis; +use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Interfaces\StorageDriver; class RedisStorageDriver implements StorageDriver @@ -18,7 +19,7 @@ class RedisStorageDriver implements StorageDriver { $id = $this->getTenantIdByDomain($domain); if (! $id) { - throw new \Exception("Tenant could not be identified on domain {$domain}"); + throw new TenantCouldNotBeIdentifiedException($domain); } return $this->getTenantById($id); diff --git a/src/TenantManager.php b/src/TenantManager.php index de466648..10c7877a 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -6,6 +6,7 @@ use Stancl\Tenancy\Interfaces\StorageDriver; use Stancl\Tenancy\Traits\BootstrapsTenancy; use Illuminate\Contracts\Foundation\Application; use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException; +use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; final class TenantManager { @@ -65,7 +66,7 @@ final class TenantManager $tenant = $this->storage->identifyTenant($domain); if (! $tenant || ! \array_key_exists('uuid', $tenant) || ! $tenant['uuid']) { - throw new \Exception("Tenant could not be identified on domain {$domain}."); + throw new TenantCouldNotBeIdentifiedException($domain); } return $tenant; @@ -176,7 +177,7 @@ final class TenantManager $uuid = $this->getIdByDomain($domain); if (\is_null($uuid)) { - throw new \Exception("Tenant with domain $domain could not be identified."); + throw new TenantCouldNotBeIdentifiedException($domain); } return $this->find($uuid, $fields);