From d0e017422bdf06c3a85449d8f671356b68fd176b Mon Sep 17 00:00:00 2001 From: Shashwat Mishra <11258035+secrethash@users.noreply.github.com> Date: Thu, 12 Dec 2024 00:54:08 +0530 Subject: [PATCH] fix: always throw exceptions on error --- src/Bucket.php | 50 +++--------------------- src/Exceptions/BaseException.php | 30 ++++++++++++++ src/Exceptions/CreateBucketException.php | 36 +++++++++++++++++ src/Exceptions/DeleteBucketException.php | 36 +++++++++++++++++ 4 files changed, 108 insertions(+), 44 deletions(-) create mode 100644 src/Exceptions/BaseException.php create mode 100644 src/Exceptions/CreateBucketException.php create mode 100644 src/Exceptions/DeleteBucketException.php diff --git a/src/Bucket.php b/src/Bucket.php index 4490929..edf1d3d 100644 --- a/src/Bucket.php +++ b/src/Bucket.php @@ -5,12 +5,13 @@ use Aws\Credentials\Credentials; use Aws\Exception\AwsException; use Aws\S3\S3Client; -use Illuminate\Support\Facades\Log; use Stancl\Tenancy\Contracts\Tenant; use Vidwan\TenantBuckets\Events\CreatedBucket; use Vidwan\TenantBuckets\Events\CreatingBucket; use Vidwan\TenantBuckets\Events\DeletedBucket; use Vidwan\TenantBuckets\Events\DeletingBucket; +use Vidwan\TenantBuckets\Exceptions\CreateBucketException; +use Vidwan\TenantBuckets\Exceptions\DeleteBucketException; class Bucket { @@ -41,11 +42,6 @@ class Bucket */ protected string|null $bucketName; - /** - * @var \Aws\Exception\AwsException|null Exception Error Bag - */ - protected AwsException|null $e = null; - public function __construct( protected Tenant $tenant ) { @@ -118,9 +114,8 @@ public function createBucket(string $name, Credentials $credentials): self $this->tenant->tenant_bucket = $this->bucketName; $this->tenant->save(); } catch (AwsException $e) { - $this->e = $e; - throw_if(config('app.debug', false), $e); - Log::critical($this->getErrorMessage()); + // We catch to set the error bag + throw new CreateBucketException($this->tenant, $this->bucketName, $e); } event(new CreatedBucket($this->tenant)); @@ -154,9 +149,8 @@ public function deleteBucket(string $name, Credentials $credentials): self 'Bucket' => $name, ]); } catch (AwsException $e) { - $this->e = $e; - throw_if(config('app.debug', false), $e); - Log::critical($this->getErrorMessage()); + // We catch to set the error bag + throw new DeleteBucketException($this->tenant, $name, $e); } event(new DeletedBucket($this->tenant)); @@ -184,36 +178,4 @@ protected function formatBucketName(string $name): string return str(preg_replace('/[^a-zA-Z0-9]/', '', $name))->lower()->toString(); } - /** - * Get Error Message - * - * @return string|null - */ - public function getErrorMessage(): string|null - { - if ($this->e) { - $data = [ - 'tenant' => $this->tenant->id, - 'bucket' => $this->bucketName, - 'error_code' => $this->e?->getAwsErrorCode(), - 'error_type' => $this->e?->getAwsErrorType(), - 'error_message' => $this->e?->getAwsErrorMessage(), - 'response' => $this->e?->getResponse(), - ]; - - return "[tenant-buckets] Error: (Tenant ID: {$this->tenant->id}) {$this->e->getAwsErrorMessage()} ". json_encode($data); - } - - return null; - } - - /** - * Get Error Bag - * - * @return AwsException|null - */ - public function getErrorBag(): AwsException|null - { - return $this->e ?: null; - } } diff --git a/src/Exceptions/BaseException.php b/src/Exceptions/BaseException.php new file mode 100644 index 0000000..0b2c2eb --- /dev/null +++ b/src/Exceptions/BaseException.php @@ -0,0 +1,30 @@ +awsException; + } + + public function getData(): array + { + return $this->data; + } +} diff --git a/src/Exceptions/CreateBucketException.php b/src/Exceptions/CreateBucketException.php new file mode 100644 index 0000000..c951d78 --- /dev/null +++ b/src/Exceptions/CreateBucketException.php @@ -0,0 +1,36 @@ +id}) {$awsException->getAwsErrorMessage()}"; + parent::__construct($message,$awsException->getCode(), $awsException); + + $this->setData(); + } + + private function setData(): static + { + $this->data = [ + 'tenant' => $this->tenant->id, + 'bucket' => $this->bucketName, + 'error_code' => $this->awsException->getAwsErrorCode(), + 'error_type' => $this->awsException->getAwsErrorType(), + 'error_message' => $this->awsException->getAwsErrorMessage(), + 'response' => $this->awsException->getResponse(), + ]; + return $this; + } + +} diff --git a/src/Exceptions/DeleteBucketException.php b/src/Exceptions/DeleteBucketException.php new file mode 100644 index 0000000..b64e5ac --- /dev/null +++ b/src/Exceptions/DeleteBucketException.php @@ -0,0 +1,36 @@ +id}) {$awsException->getAwsErrorMessage()}"; + parent::__construct($message,$awsException->getCode(), $awsException); + + $this->setData(); + } + + private function setData(): static + { + $this->data = [ + 'tenant' => $this->tenant->id, + 'bucket' => $this->bucketName, + 'error_code' => $this->awsException->getAwsErrorCode(), + 'error_type' => $this->awsException->getAwsErrorType(), + 'error_message' => $this->awsException->getAwsErrorMessage(), + 'response' => $this->awsException->getResponse(), + ]; + return $this; + } + +}