Use dedicated exceptions if unlinking failed for reasons other than the
file not existing
This commit is contained in:
3 files changed
+45
-1
No files matched your search
@@ -0,0 +1,18 @@
|
||||
<?PHP
|
||||
declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* Reports a failure to create a new directory.
|
||||
*/
|
||||
final class MDFailedToDeleteFile extends Exception {
|
||||
/**
|
||||
* Error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function errorMessage() {
|
||||
//error message
|
||||
return 'Failed to delete file';
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?PHP
|
||||
declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* Reports a failure due to insufficient permissions in the file system.
|
||||
*/
|
||||
final class MDFilePermissionsException extends Exception {
|
||||
/**
|
||||
* Error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function errorMessage() {
|
||||
//error message
|
||||
return 'Insufficient file system permissions';
|
||||
|
||||
}
|
||||
}
|
||||
+9
-1
@@ -110,7 +110,15 @@ public static function mkdir(string $pathname, int $mode = 0775, bool $recursive
|
||||
public static function unlink(string $filename):void {
|
||||
|
||||
if (\unlink($filename) === false) {
|
||||
throw new MDFileDoesNotExist("Failed to delete: $filename");
|
||||
if (!\is_file($filename)) {
|
||||
throw new MDFileDoesNotExist("Failed to delete $filename, it did not exist");
|
||||
}
|
||||
else if (!\is_writable(dirname($filename))) {
|
||||
throw new MDFilePermissionsException("Failed to delete $filename");
|
||||
}
|
||||
else {
|
||||
throw new MDFailedToDeleteFile("Failed to delete $filename");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user