Skip to content

Commit 39864de

Browse files
committed
FileSystem: better error messages
1 parent f0d8f23 commit 39864de

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/Utils/FileSystem.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class FileSystem
2626
public static function createDir(string $dir, int $mode = 0777): void
2727
{
2828
if (!is_dir($dir) && !@mkdir($dir, $mode, true) && !is_dir($dir)) { // @ - dir may already exist
29-
throw new Nette\IOException("Unable to create directory '$dir'. " . error_get_last()['message']);
29+
throw new Nette\IOException("Unable to create directory '$dir'. " . self::getLastError());
3030
}
3131
}
3232

@@ -59,7 +59,7 @@ public static function copy(string $source, string $dest, bool $overwrite = true
5959
} else {
6060
static::createDir(dirname($dest));
6161
if (@stream_copy_to_stream(fopen($source, 'r'), fopen($dest, 'w')) === false) { // @ is escalated to exception
62-
throw new Nette\IOException("Unable to copy file '$source' to '$dest'.");
62+
throw new Nette\IOException("Unable to copy file '$source' to '$dest'. " . self::getLastError());
6363
}
6464
}
6565
}
@@ -74,15 +74,15 @@ public static function delete(string $path): void
7474
if (is_file($path) || is_link($path)) {
7575
$func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
7676
if (!@$func($path)) { // @ is escalated to exception
77-
throw new Nette\IOException("Unable to delete '$path'.");
77+
throw new Nette\IOException("Unable to delete '$path'. " . self::getLastError());
7878
}
7979

8080
} elseif (is_dir($path)) {
8181
foreach (new \FilesystemIterator($path) as $item) {
8282
static::delete($item->getPathname());
8383
}
8484
if (!@rmdir($path)) { // @ is escalated to exception
85-
throw new Nette\IOException("Unable to delete directory '$path'.");
85+
throw new Nette\IOException("Unable to delete directory '$path'. " . self::getLastError());
8686
}
8787
}
8888
}
@@ -107,7 +107,7 @@ public static function rename(string $name, string $newName, bool $overwrite = t
107107
static::delete($newName);
108108
}
109109
if (!@rename($name, $newName)) { // @ is escalated to exception
110-
throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'.");
110+
throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'. " . self::getLastError());
111111
}
112112
}
113113
}
@@ -121,7 +121,7 @@ public static function read(string $file): string
121121
{
122122
$content = @file_get_contents($file); // @ is escalated to exception
123123
if ($content === false) {
124-
throw new Nette\IOException("Unable to read file '$file'.");
124+
throw new Nette\IOException("Unable to read file '$file'. " . self::getLastError());
125125
}
126126
return $content;
127127
}
@@ -135,10 +135,10 @@ public static function write(string $file, string $content, ?int $mode = 0666):
135135
{
136136
static::createDir(dirname($file));
137137
if (@file_put_contents($file, $content) === false) { // @ is escalated to exception
138-
throw new Nette\IOException("Unable to write file '$file'.");
138+
throw new Nette\IOException("Unable to write file '$file'. " . self::getLastError());
139139
}
140140
if ($mode !== null && !@chmod($file, $mode)) { // @ is escalated to exception
141-
throw new Nette\IOException("Unable to chmod file '$file'.");
141+
throw new Nette\IOException("Unable to chmod file '$file'. " . self::getLastError());
142142
}
143143
}
144144

@@ -150,4 +150,10 @@ public static function isAbsolute(string $path): bool
150150
{
151151
return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path);
152152
}
153+
154+
155+
private static function getLastError(): string
156+
{
157+
return preg_replace('#^\w+\(.*?\): #', '', error_get_last()['message']);
158+
}
153159
}

tests/Utils/FileSystem.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Assert::exception(function () {
5454

5555
Assert::exception(function () {
5656
FileSystem::read('');
57-
}, Nette\IOException::class, "Unable to read file ''.");
57+
}, Nette\IOException::class, "Unable to read file ''. Filename cannot be empty");
5858

5959

6060
test(function () { // copy

0 commit comments

Comments
 (0)