Skip to content

Commit

Permalink
fix: always throw exceptions on error
Browse files Browse the repository at this point in the history
  • Loading branch information
secrethash committed Dec 11, 2024
1 parent 1951dfa commit d0e0174
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 44 deletions.
50 changes: 6 additions & 44 deletions src/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
) {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
}
30 changes: 30 additions & 0 deletions src/Exceptions/BaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Vidwan\TenantBuckets\Exceptions;

use Aws\Exception\AwsException;
use Exception;
use Throwable;

abstract class BaseException extends Exception
{

protected array $data;

protected AwsException $awsException;

public function __construct(string $message = "", int $code = 0, Throwable|null $previous = null)
{
parent::__construct($message, $code, $previous);
}

public function getAwsException(): AwsException
{
return $this->awsException;
}

public function getData(): array
{
return $this->data;
}
}
36 changes: 36 additions & 0 deletions src/Exceptions/CreateBucketException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Vidwan\TenantBuckets\Exceptions;

use Aws\Exception\AwsException;
use Stancl\Tenancy\Contracts\Tenant;

class CreateBucketException extends BaseException
{

public function __construct(
protected Tenant $tenant,
protected string $bucketName,
protected AwsException $awsException
)
{
$message = "[tenant-buckets] Error: (Tenant ID: {$tenant->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;
}

}
36 changes: 36 additions & 0 deletions src/Exceptions/DeleteBucketException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Vidwan\TenantBuckets\Exceptions;

use Aws\Exception\AwsException;
use Stancl\Tenancy\Contracts\Tenant;

class DeleteBucketException extends BaseException
{

public function __construct(
protected Tenant $tenant,
protected string $bucketName,
protected AwsException $awsException
)
{
$message = "[tenant-buckets] Error: (Tenant ID: {$tenant->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;
}

}

0 comments on commit d0e0174

Please sign in to comment.