Errors and Translations
Every failure of the file system is a Derafu\File\Exception\FileException, which extends TranslatableRuntimeException of derafu/translation. Catching FileException, or PHP’s RuntimeException, catches all of them.
| Exception | When |
|---|---|
FileException |
A creation, a reading, a writing, a move, a deletion or an archive fails. |
FileNotFoundException |
A file, a directory or a path that must exist does not. |
FileAccessException |
It exists, but it is not of the expected kind (a directory instead of a file) or it can not be read or written. |
PathException |
A path has a null character, or it leaves the directory that it must stay in. |
The last three extend FileException. The messages name the file, the directory or the path:
try {
File::read('/var/app/missing.txt');
} catch (FileNotFoundException $e) {
$e->getMessage(); // The file does not exist: /var/app/missing.txt
}
A wrong argument is not a file error. TempFile::with() and Zip::fromContents() throw a TranslatableInvalidArgumentException (an invalid extension, prefix or entry name, or a content that is not a string).
Translations
The messages are translatable and the Spanish translations ship with the package. To have them in your translator, import its services (or add FileTranslationResourceProvider to the resource providers of the translator):
imports:
- { resource: '../vendor/derafu/file/resources/config/file-services.yaml' }
With the translator, the message of the example above is El archivo no existe: /var/app/missing.txt.