Handle imap end and error