Skip to content

Commit d0e0174

Browse files
committed
fix: always throw exceptions on error
1 parent 1951dfa commit d0e0174

File tree

4 files changed

+108
-44
lines changed

4 files changed

+108
-44
lines changed

src/Bucket.php

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use Aws\Credentials\Credentials;
66
use Aws\Exception\AwsException;
77
use Aws\S3\S3Client;
8-
use Illuminate\Support\Facades\Log;
98
use Stancl\Tenancy\Contracts\Tenant;
109
use Vidwan\TenantBuckets\Events\CreatedBucket;
1110
use Vidwan\TenantBuckets\Events\CreatingBucket;
1211
use Vidwan\TenantBuckets\Events\DeletedBucket;
1312
use Vidwan\TenantBuckets\Events\DeletingBucket;
13+
use Vidwan\TenantBuckets\Exceptions\CreateBucketException;
14+
use Vidwan\TenantBuckets\Exceptions\DeleteBucketException;
1415

1516
class Bucket
1617
{
@@ -41,11 +42,6 @@ class Bucket
4142
*/
4243
protected string|null $bucketName;
4344

44-
/**
45-
* @var \Aws\Exception\AwsException|null Exception Error Bag
46-
*/
47-
protected AwsException|null $e = null;
48-
4945
public function __construct(
5046
protected Tenant $tenant
5147
) {
@@ -118,9 +114,8 @@ public function createBucket(string $name, Credentials $credentials): self
118114
$this->tenant->tenant_bucket = $this->bucketName;
119115
$this->tenant->save();
120116
} catch (AwsException $e) {
121-
$this->e = $e;
122-
throw_if(config('app.debug', false), $e);
123-
Log::critical($this->getErrorMessage());
117+
// We catch to set the error bag
118+
throw new CreateBucketException($this->tenant, $this->bucketName, $e);
124119
}
125120

126121
event(new CreatedBucket($this->tenant));
@@ -154,9 +149,8 @@ public function deleteBucket(string $name, Credentials $credentials): self
154149
'Bucket' => $name,
155150
]);
156151
} catch (AwsException $e) {
157-
$this->e = $e;
158-
throw_if(config('app.debug', false), $e);
159-
Log::critical($this->getErrorMessage());
152+
// We catch to set the error bag
153+
throw new DeleteBucketException($this->tenant, $name, $e);
160154
}
161155

162156
event(new DeletedBucket($this->tenant));
@@ -184,36 +178,4 @@ protected function formatBucketName(string $name): string
184178
return str(preg_replace('/[^a-zA-Z0-9]/', '', $name))->lower()->toString();
185179
}
186180

187-
/**
188-
* Get Error Message
189-
*
190-
* @return string|null
191-
*/
192-
public function getErrorMessage(): string|null
193-
{
194-
if ($this->e) {
195-
$data = [
196-
'tenant' => $this->tenant->id,
197-
'bucket' => $this->bucketName,
198-
'error_code' => $this->e?->getAwsErrorCode(),
199-
'error_type' => $this->e?->getAwsErrorType(),
200-
'error_message' => $this->e?->getAwsErrorMessage(),
201-
'response' => $this->e?->getResponse(),
202-
];
203-
204-
return "[tenant-buckets] Error: (Tenant ID: {$this->tenant->id}) {$this->e->getAwsErrorMessage()} ". json_encode($data);
205-
}
206-
207-
return null;
208-
}
209-
210-
/**
211-
* Get Error Bag
212-
*
213-
* @return AwsException|null
214-
*/
215-
public function getErrorBag(): AwsException|null
216-
{
217-
return $this->e ?: null;
218-
}
219181
}

src/Exceptions/BaseException.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Vidwan\TenantBuckets\Exceptions;
4+
5+
use Aws\Exception\AwsException;
6+
use Exception;
7+
use Throwable;
8+
9+
abstract class BaseException extends Exception
10+
{
11+
12+
protected array $data;
13+
14+
protected AwsException $awsException;
15+
16+
public function __construct(string $message = "", int $code = 0, Throwable|null $previous = null)
17+
{
18+
parent::__construct($message, $code, $previous);
19+
}
20+
21+
public function getAwsException(): AwsException
22+
{
23+
return $this->awsException;
24+
}
25+
26+
public function getData(): array
27+
{
28+
return $this->data;
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Vidwan\TenantBuckets\Exceptions;
4+
5+
use Aws\Exception\AwsException;
6+
use Stancl\Tenancy\Contracts\Tenant;
7+
8+
class CreateBucketException extends BaseException
9+
{
10+
11+
public function __construct(
12+
protected Tenant $tenant,
13+
protected string $bucketName,
14+
protected AwsException $awsException
15+
)
16+
{
17+
$message = "[tenant-buckets] Error: (Tenant ID: {$tenant->id}) {$awsException->getAwsErrorMessage()}";
18+
parent::__construct($message,$awsException->getCode(), $awsException);
19+
20+
$this->setData();
21+
}
22+
23+
private function setData(): static
24+
{
25+
$this->data = [
26+
'tenant' => $this->tenant->id,
27+
'bucket' => $this->bucketName,
28+
'error_code' => $this->awsException->getAwsErrorCode(),
29+
'error_type' => $this->awsException->getAwsErrorType(),
30+
'error_message' => $this->awsException->getAwsErrorMessage(),
31+
'response' => $this->awsException->getResponse(),
32+
];
33+
return $this;
34+
}
35+
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Vidwan\TenantBuckets\Exceptions;
4+
5+
use Aws\Exception\AwsException;
6+
use Stancl\Tenancy\Contracts\Tenant;
7+
8+
class DeleteBucketException extends BaseException
9+
{
10+
11+
public function __construct(
12+
protected Tenant $tenant,
13+
protected string $bucketName,
14+
protected AwsException $awsException
15+
)
16+
{
17+
$message = "[tenant-buckets] Error: (Tenant ID: {$tenant->id}) {$awsException->getAwsErrorMessage()}";
18+
parent::__construct($message,$awsException->getCode(), $awsException);
19+
20+
$this->setData();
21+
}
22+
23+
private function setData(): static
24+
{
25+
$this->data = [
26+
'tenant' => $this->tenant->id,
27+
'bucket' => $this->bucketName,
28+
'error_code' => $this->awsException->getAwsErrorCode(),
29+
'error_type' => $this->awsException->getAwsErrorType(),
30+
'error_message' => $this->awsException->getAwsErrorMessage(),
31+
'response' => $this->awsException->getResponse(),
32+
];
33+
return $this;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)