How to do it...

To display an error message to the user when an error condition is encountered, you need to take the following steps:

  1. Add the following import at the beginning of the Python file:
from odoo.exceptions import UserError
from odoo.tools.translate import _
  1. Modify the method to catch the exception raised and raise a UserError exception:
    @api.multi 
    def save(self, filename): 
        if '/' in filename or '\\' in filename: 
            raise UserError('Illegal filename %s' % filename) 
        path = os.path.join('/opt/exports', filename) 
        try: 
            with open(path, 'w') as fobj: 
                for record in self: 
                    fobj.write(record.data) 
                    fobj.write('\n') 
        except (IOError, OSError) as exc: 
            message = _('Unable to save file: %s') % exc 
            raise UserError(message)