Skip to content

Commit f0e14b0

Browse files
committed
Use messageManager instead of throwing exceptions
1 parent ba8fae3 commit f0e14b0

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

app/code/Magento/ImportExport/Controller/Adminhtml/Export/File/Delete.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
use Magento\Backend\App\Action;
1111
use Magento\Framework\App\Action\HttpPostActionInterface;
12-
use Magento\Framework\Controller\ResultFactory;
1312
use Magento\Framework\Exception\FileSystemException;
14-
use Magento\Framework\Exception\LocalizedException;
1513
use Magento\Framework\App\Filesystem\DirectoryList;
1614
use Magento\ImportExport\Controller\Adminhtml\Export as ExportController;
1715
use Magento\Framework\Filesystem;
@@ -56,29 +54,33 @@ public function __construct(
5654
/**
5755
* Controller basic method implementation.
5856
*
59-
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
60-
* @throws LocalizedException
57+
* @return \Magento\Framework\Controller\ResultInterface
6158
*/
6259
public function execute()
6360
{
61+
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
62+
$resultRedirect = $this->resultRedirectFactory->create();
63+
$resultRedirect->setPath('adminhtml/export/index');
64+
$fileName = $this->getRequest()->getParam('filename');
65+
if (empty($fileName)) {
66+
$this->messageManager->addErrorMessage(\__('Please provide valid export file name'));
67+
68+
return $resultRedirect;
69+
}
6470
try {
65-
if (empty($fileName = $this->getRequest()->getParam('filename'))) {
66-
throw new LocalizedException(__('Please provide export file name'));
67-
}
6871
$directory = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
6972
$path = $directory->getAbsolutePath() . 'export/' . $fileName;
7073

71-
if (!$directory->isFile($path)) {
72-
throw new LocalizedException(__('Sorry, but the data is invalid or the file is not uploaded.'));
74+
if ($directory->isFile($path)) {
75+
$this->file->deleteFile($path);
76+
$this->messageManager->addSuccessMessage(\__('File %1 deleted', $fileName));
77+
} else {
78+
$this->messageManager->addErrorMessage(\__('%1 is not a valid file', $fileName));
7379
}
74-
75-
$this->file->deleteFile($path);
76-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
77-
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
78-
$resultRedirect->setPath('adminhtml/export/index');
79-
return $resultRedirect;
8080
} catch (FileSystemException $exception) {
81-
throw new LocalizedException(__('There are no export file with such name %1', $fileName));
81+
$this->messageManager->addErrorMessage($exception->getMessage());
8282
}
83+
84+
return $resultRedirect;
8385
}
8486
}

app/code/Magento/ImportExport/Controller/Adminhtml/Export/File/Download.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Backend\App\Action;
1111
use Magento\Framework\App\Action\HttpGetActionInterface;
1212
use Magento\Framework\App\Response\Http\FileFactory;
13-
use Magento\Framework\Exception\LocalizedException;
1413
use Magento\Framework\App\Filesystem\DirectoryList;
1514
use Magento\ImportExport\Controller\Adminhtml\Export as ExportController;
1615
use Magento\Framework\Filesystem;
@@ -55,12 +54,17 @@ public function __construct(
5554
* Controller basic method implementation.
5655
*
5756
* @return \Magento\Framework\App\ResponseInterface
58-
* @throws LocalizedException
5957
*/
6058
public function execute()
6159
{
62-
if (empty($fileName = $this->getRequest()->getParam('filename'))) {
63-
throw new LocalizedException(__('Please provide export file name'));
60+
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
61+
$resultRedirect = $this->resultRedirectFactory->create();
62+
$resultRedirect->setPath('adminhtml/export/index');
63+
$fileName = $this->getRequest()->getParam('filename');
64+
if (empty($fileName) || \preg_match('/\.\.(\\\|\/)/', $fileName) !== 0) {
65+
$this->messageManager->addErrorMessage(\__('Please provide valid export file name'));
66+
67+
return $resultRedirect;
6468
}
6569
try {
6670
$path = 'export/' . $fileName;
@@ -72,8 +76,11 @@ public function execute()
7276
DirectoryList::VAR_DIR
7377
);
7478
}
75-
} catch (LocalizedException | \Exception $exception) {
76-
throw new LocalizedException(__('There are no export file with such name %1', $fileName));
79+
$this->messageManager->addErrorMessage(\__('%1 is not a valid file', $fileName));
80+
} catch (\Exception $exception) {
81+
$this->messageManager->addErrorMessage($exception->getMessage());
7782
}
83+
84+
return $resultRedirect;
7885
}
7986
}

0 commit comments

Comments
 (0)