How to add an error notice conditionally

How to add an error notice conditionally