imap: improve error handling