Skip to content

Commit 635bea2

Browse files
committed
improved error handling
1 parent eeb445c commit 635bea2

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

src/Exceptions/UnableToDeleteFileException.php renamed to src/Exceptions/DeleteDirectoryException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
use Exception;
77

8-
class UnableToDeleteFileException extends Exception
8+
class DeleteDirectoryException extends Exception
99
{
1010
}

src/Exceptions/UnableToDeleteDirectoryException.php renamed to src/Exceptions/DeleteFileException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
use Exception;
77

8-
class UnableToDeleteDirectoryException extends Exception
8+
class DeleteFileException extends Exception
99
{
1010
}

src/Exceptions/UploadException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Imahmood\FileStorage\Exceptions;
5+
6+
use Exception;
7+
8+
class UploadException extends Exception
9+
{
10+
}

src/FileStorage.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
use Imahmood\FileStorage\Contracts\MediaAwareInterface;
1212
use Imahmood\FileStorage\Contracts\MediaTypeInterface;
1313
use Imahmood\FileStorage\Exceptions\PersistenceFailedException;
14-
use Imahmood\FileStorage\Exceptions\UnableToDeleteDirectoryException;
15-
use Imahmood\FileStorage\Exceptions\UnableToDeleteFileException;
14+
use Imahmood\FileStorage\Exceptions\DeleteDirectoryException;
15+
use Imahmood\FileStorage\Exceptions\DeleteFileException;
16+
use Imahmood\FileStorage\Exceptions\UploadException;
1617
use Imahmood\FileStorage\Jobs\GeneratePreview;
1718
use Imahmood\FileStorage\Jobs\OptimizeImage;
1819
use Imahmood\FileStorage\Models\Media;
@@ -43,7 +44,7 @@ public function create(
4344

4445
/**
4546
* @throws \Imahmood\FileStorage\Exceptions\PersistenceFailedException
46-
* @throws \Imahmood\FileStorage\Exceptions\UnableToDeleteFileException
47+
* @throws \Imahmood\FileStorage\Exceptions\DeleteFileException
4748
*/
4849
public function update(
4950
MediaTypeInterface $type,
@@ -76,7 +77,7 @@ public function update(
7677
/**
7778
* @throws \InvalidArgumentException
7879
* @throws \Imahmood\FileStorage\Exceptions\PersistenceFailedException
79-
* @throws \Imahmood\FileStorage\Exceptions\UnableToDeleteFileException
80+
* @throws \Imahmood\FileStorage\Exceptions\DeleteFileException
8081
*/
8182
public function updateOrCreate(
8283
MediaTypeInterface $type,
@@ -126,7 +127,7 @@ protected function persistMedia(Media $media, ?UploadedFile $uploadedFile): Medi
126127
]);
127128

128129
if (! $isUploaded) {
129-
throw new PersistenceFailedException();
130+
throw new UploadException();
130131
}
131132

132133
if ($media->is_image) {
@@ -144,7 +145,7 @@ protected function persistMedia(Media $media, ?UploadedFile $uploadedFile): Medi
144145
}
145146

146147
/**
147-
* @throws \Imahmood\FileStorage\Exceptions\UnableToDeleteDirectoryException
148+
* @throws \Imahmood\FileStorage\Exceptions\DeleteDirectoryException
148149
*/
149150
public function delete(Media $media): bool
150151
{
@@ -160,13 +161,13 @@ public function delete(Media $media): bool
160161
}
161162

162163
/**
163-
* @throws \Imahmood\FileStorage\Exceptions\UnableToDeleteDirectoryException
164+
* @throws \Imahmood\FileStorage\Exceptions\DeleteDirectoryException
164165
*/
165166
protected function deleteDirectory(string $dir): void
166167
{
167168
$isDeleted = Storage::disk($this->config->diskName)->deleteDirectory($dir);
168169
if (! $isDeleted) {
169-
throw new UnableToDeleteDirectoryException(sprintf(
170+
throw new DeleteDirectoryException(sprintf(
170171
'[FileStorage] Disk: %s, Directory: %s',
171172
$this->config->diskName,
172173
$dir
@@ -175,15 +176,15 @@ protected function deleteDirectory(string $dir): void
175176
}
176177

177178
/**
178-
* @throws \Imahmood\FileStorage\Exceptions\UnableToDeleteFileException
179+
* @throws \Imahmood\FileStorage\Exceptions\DeleteFileException
179180
*/
180181
protected function deleteFile(array|string $paths): void
181182
{
182183
$isDeleted = Storage::disk($this->config->diskName)->delete($paths);
183184
if (! $isDeleted) {
184185
$paths = is_array($paths) ? implode(', ', $paths) : $paths;
185186

186-
throw new UnableToDeleteFileException(sprintf(
187+
throw new DeleteFileException(sprintf(
187188
'[FileStorage] Disk: %s, Paths: %s',
188189
$this->config->diskName,
189190
$paths

0 commit comments

Comments
 (0)