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.
In fact, this is working. I didn't need to show $e, it's automatically ^^,
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'] ); } }