I'm creating a before plugin for the class \Magento\Customer\Controller\Account\EditPost. In the execute method I want to throw an Exception based on certain condition but the plugin is showing output like this.

enter image description here

I want to print proper exception message in notification area like this.

enter image description here

Here is my Plugin Class

class EditPost {

        public function beforeExecute(\Magento\Customer\Controller\Account\EditPost $subject) {

            throw new \Exception('Exception trigger');
            return $subject;


What I'm doing wrong here.

2 Answers 2


This is my solution using around plugin.

public function aroundExecute(\Magento\Customer\Controller\Account\EditPost $subject, $proceed) {

            if(true) { // for true condition
                $this->messageManager->addErrorMessage('My custome Error');
                /** @var Redirect $resultRedirect */
                $resultRedirect = $this->resultRedirectFactory->create();
                return $resultRedirect;
            return $proceed();

You can still use your before plugin and use \Magento\Framework\Message\ManagerInterface for this.

 * @var \Magento\Framework\Message\ManagerInterface
private $messageManager;

public function __construct(\Magento\Framework\Message\ManagerInterface $messageManager)
    $this->messageManager = $messageManager;

public function beforeExecute(\Magento\Customer\Controller\Account\EditPost $subject) {
    try {
        //your code
        throw new \Exception('Exception trigger');
    } catch (\Exception $e) {

    return null;

The important part is if you decide to throw an exception that you catch it. Then you can use the Message\Manager to display your desired message.

If you return null or as in your answer with the around plugin $proceed() keep in mind that the base method will be executed and it will still save the customer.

  • its not working this way e.g. if you want to restrict 'change_email'. With your solution this is not fulfill my requirement that's why I'm using around plugin Sep 24, 2019 at 6:48
  • 1
    That is true if you want to restrict that. But it answers the question about the message maybe add that you want to restrict certain functionality in your question then a around plugin is the way to go.
    – Daniel
    Sep 24, 2019 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.