Magento 2, You can show Success and Error Message Using Interface Magento\Framework\Message\ManagerInterface.
You can display success message Using addSuccessMessage( ) and display error message using addErrorMessage( ) function.
addError( ), addSuccess( ), addWarning( ) and addNotice( ) methods are deprecated.
You can directly fetch Object of Magento\Framework\Message\ManagerInterface in controller file but for other files like Plugin, Observer, Model, Block or Helper you need to instantiate an object in your __construct( ) method.
public function __construct( \Magento\Framework\Message\ManagerInterface $messageManager ) { $this->messageManager = $messageManager; }
Now You can display messages using the below way,
For Success Message,
$message = __('Success Message'); $this->messageManager->addSuccessMessage($message);
For Error Message,
$message = __('Error Message'); $this->messageManager->addErrorMessage($message);
For Warning Messages,
$message = __('Warning Message'); $this->messageManager->addWarningMessage($message);
For Notice Message,
$message = __('Notice Message'); $this->messageManager->addNoticeMessage($message);