Skip to content

Commit

Permalink
Improve file system error feedback
Browse files Browse the repository at this point in the history
Provides more action-specific messaging when a file/folder action can not be taken
  • Loading branch information
smg6511 committed Feb 26, 2024
1 parent f197da7 commit b3e7977
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
16 changes: 14 additions & 2 deletions core/lexicon/en/file.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
$_lang['file_edit'] = 'Edit File';
$_lang['file_open'] = 'Open File Url';
$_lang['file_err_ae'] = 'File %s already exists';
$_lang['file_err_create'] = 'An unknown error occurred while trying to create the file.';
$_lang['file_err_create_general_exception'] = 'An unknown error occurred while trying to create the file. Please check the MODX and/or server error logs for more information.';
$_lang['file_err_create_write_exception'] = 'The file could not be created. Please verify you have write permissions for its target directory and try again.';
$_lang['file_err_ext_not_allowed'] = 'File extension `[[+ext]]` is not permitted.';
$_lang['file_err_filter'] = 'No files match the specified filter.';
$_lang['file_err_invalid'] = 'The file is not a regular file and cannot be deleted.';
$_lang['file_err_move_general_exception'] = 'An unknown error occurred while trying to move the file. Please check the MODX and/or server error logs for more information.';
$_lang['file_err_move_write_exception'] = 'The file could not be moved. Please verify you have write permissions for both the file and its target directory and try again.';
$_lang['file_err_nf'] = 'File does not exist!';
$_lang['file_err_ns'] = 'Please specify a valid file.';
$_lang['file_err_open'] = 'Cannot open file: ';
$_lang['file_err_rename'] = 'MODX failed to rename the file. Please make sure your permissions are set correctly.';
$_lang['file_err_remove'] = 'MODX failed to delete the file. Please make sure your permissions are set correctly.';
$_lang['file_err_too_large'] = 'Uploaded file is too large at [[+size]] bytes. Please ensure your files are less than [[+allowed]] bytes.';
$_lang['file_err_unzip'] = 'Unzip Failed!';
$_lang['file_err_update_general_exception'] = 'An unknown system error occurred while trying to update this file. Please check the MODX and/or server error logs for more information.';
$_lang['file_err_update_write_exception'] = 'The file could not be updated. Please verify you have write permissions for it and try again.';
$_lang['file_err_upload'] = 'An error occurred while trying to upload the files.';
$_lang['file_extensions'] = 'File Extensions';
$_lang['file_folder_path'] = 'Path';
Expand All @@ -40,9 +45,12 @@
$_lang['file_folder_err_ae'] = 'A directory already exists with that name in that location.';
$_lang['file_folder_err_create'] = 'An unknown error occurred while trying to create the directory.';
$_lang['file_folder_err_invalid'] = 'The specified directory is not a directory.';
$_lang['file_folder_err_move_general_exception'] = 'An unknown error occurred while trying to move the directory. Please check the MODX and/or server error logs for more information.';
$_lang['file_folder_err_move_write_exception'] = 'The directory could not be moved. Please verify you have write permissions for both this directory and its target directory and try again.';
$_lang['file_folder_err_ns'] = 'Please specify a valid directory.';
$_lang['file_folder_err_ns_name'] = 'Please specify a valid name for the directory.';
$_lang['file_folder_err_rename'] = 'An unknown error occurred while trying to rename the directory.';
$_lang['file_folder_err_rename_general_exception'] = 'An unknown error occurred while trying to rename the directory. Please check the MODX and/or server error logs for more information.';
$_lang['file_folder_err_rename_write_exception'] = 'The directory could not be renamed. Please verify you have write permissions for it and try again.';
$_lang['file_folder_err_rename_protected'] = 'Renaming the protected system directory is not permitted.';
$_lang['file_folder_err_remove'] = 'An error occurred while trying to delete the directory.';
$_lang['file_folder_err_remove_protected'] = 'Deleting the protected system directory is not permitted.';
Expand Down Expand Up @@ -115,3 +123,7 @@
$_lang['upload.clear_list.notpermitted'] = 'Delete not permitted only';
$_lang['upload.msg.title.error'] = 'Error';
$_lang['upload.upload.success'] = 'Upload successful';

/** Deprecated keys */
$_lang['file_err_create'] = $_lang['file_err_create_general_exception'];
$_lang['file_folder_err_rename'] = $_lang['file_folder_err_rename_general_exception'];
26 changes: 22 additions & 4 deletions core/src/Revolution/Sources/modMediaSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,11 @@ public function createObject($path, $name, $content)
try {
$this->filesystem->write($path, $content, $config);
} catch (FilesystemException | UnableToWriteFile $e) {
$this->addError('name', $this->xpdo->lexicon('file_err_create'));
$messageKey = $e instanceof UnableToWriteFile
? 'file_err_create_write_exception'
: 'file_err_create_general_exception'
;
$this->addError('name', $this->xpdo->lexicon($messageKey));
$this->xpdo->log(modX::LOG_LEVEL_ERROR, $e->getMessage());
return false;
}
Expand Down Expand Up @@ -812,7 +816,13 @@ public function moveObject($from, $to, $point = 'append', $to_source = 0)
try {
$this->filesystem->move($path, $newPath);
} catch (FilesystemException | UnableToMoveFile $e) {
$this->addError('from', $this->xpdo->lexicon('file_err_rename'));
// $this->addError('from', $this->xpdo->lexicon('file_err_rename'));
$prefix = $mimeType === 'directory' ? 'file_folder_' : 'file_' ;
$messageKey = $e instanceof UnableToMoveFile
? $prefix . 'err_move_write_exception'
: $prefix . 'err_move_write_general'
;
$this->addError('name', $this->xpdo->lexicon($messageKey));
$this->xpdo->log(modX::LOG_LEVEL_ERROR, $e->getMessage());
return false;
}
Expand Down Expand Up @@ -960,7 +970,11 @@ public function renameContainer($oldPath, $newName)
try {
$this->filesystem->move($oldPath, $newPath);
} catch (FilesystemException | UnableToMoveFile $e) {
$this->addError('name', $this->xpdo->lexicon('file_folder_err_rename'));
$messageKey = $e instanceof UnableToMoveFile
? 'file_folder_err_rename_write_exception'
: 'file_folder_err_rename_general_exception'
;
$this->addError('name', $this->xpdo->lexicon($messageKey));
$this->xpdo->log(modX::LOG_LEVEL_ERROR, $e->getMessage());
return false;
}
Expand Down Expand Up @@ -1071,7 +1085,11 @@ public function updateObject($path, $content)
try {
$this->filesystem->write($path, $content, $config);
} catch (FilesystemException | UnableToWriteFile $e) {
$this->addError('name', $this->xpdo->lexicon('file_folder_err_update'));
$messageKey = $e instanceof UnableToWriteFile
? 'file_err_update_write_exception'
: 'file_err_update_write_general'
;
$this->addError('name', $this->xpdo->lexicon($messageKey));
$this->xpdo->log(modX::LOG_LEVEL_ERROR, $e->getMessage());
return false;
}
Expand Down

0 comments on commit b3e7977

Please sign in to comment.