- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2018
03:31 AM
07-26-2018
03:31 AM
in my controller, I have this code (I'm getting last order through a form):
...
foreach($array as $item){
try {
$product = $this->_productRepository->getById($item['product_id']);
$product_id = $product->getId();
} catch (NoSuchEntityException $e) {
$this->messageManager->addErrorMessage('Un ou plusieurs produits n\'existe(nt) plus');
continue;
}
if(isset($product)){
$param = array(
'product' => $item['product_id'],
'qty' => $item['qty']
);
}
}
I want to display a simple message on my view. The exception is way to big, I want only to show 'product doesn't exist'. How can I do that ? I saw that I can achieve this with "message manager", but how can I get the exception from the controller to my view ?
On the front, I try
$e->getMessage()
but $e is not define
Solved! Go to Solution.
Labels:
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2018
03:33 AM
07-26-2018
03:33 AM
In fact, this is working. I didn't need to show $e, it's automatically ^^,
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2018
03:33 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2018
03:34 AM
07-26-2018
03:34 AM
Re: Exception from controller to view
You can use throw for send message to front,
foreach($array as $item){ try { $product = $this->_productRepository->getById($item['product_id']); $product_id = $product->getId(); } catch (NoSuchEntityException $e) { throw new \Magento\Framework\Exception\LocalizedException(__('Un ou plusieurs produits n\'existe(nt) plus')); continue; } if(isset($product)){ $param = array( 'product' => $item['product_id'], 'qty' => $item['qty'] ); } }
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial
Magento 2 Blogs/Tutorial