show error message through plugin Magento2

5.8k views Asked by At

Hi i have use a plugin feature in my module to check the zipcode is available in the module or not .Which works fine but on validating i have set the error message like

$result = $connection->fetchAll("SELECT shipregioncodes FROM shipregioncodes_shipregioncodes WHERE shipregioncodes=$postcode");
$count = count($result);
if($count==0){
        $this->_messageManager->addError("hiiiiiiii");
        throw new NoSuchEntityException(
            __('Shipping is not available on provided zipcode %1', $postcode)
        );

}

it only shows the message in the console not on the site.Please suggest me how can i fix this. thanks

1

There are 1 answers

0
Rohit Goel On

Hi i got this done by using the below code using StateException

 if($count==0){
        throw new StateException(__('Shipping is not available on the provided zipcode.'));
 }

don't forget to include

use Magento\Framework\Exception\StateException;

:)