diff --git a/doc/src/releases.rst b/doc/src/releases.rst
index e2a07dd..95d087c 100644
--- a/doc/src/releases.rst
+++ b/doc/src/releases.rst
@@ -21,6 +21,8 @@ Changed
   are not used anymore.
 - More precise exceptions available in `imapclient.exceptions` are raised when
   an error happens
+- `imapclient.exceptions.ProtocolError` is now raised when the reply from a
+  remote server violates the IMAP protocol.
 - GMail labels are now strings instead of bytes in Python 3.
 
 Fixed
diff --git a/imapclient/exceptions.py b/imapclient/exceptions.py
index 1130da1..26fd51c 100644
--- a/imapclient/exceptions.py
+++ b/imapclient/exceptions.py
@@ -36,3 +36,7 @@ class InvalidCriteriaError(IMAPClientError):
     A command using a search criteria failed, probably due to a syntax 
     error in the criteria string.
     """
+
+
+class ProtocolError(IMAPClientError):
+    """The server replied with a response that violates the IMAP protocol."""
diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py
index 58fae81..fea196b 100644
--- a/imapclient/imapclient.py
+++ b/imapclient/imapclient.py
@@ -24,7 +24,7 @@ from . import tls
 from .datetime_util import datetime_to_INTERNALDATE, format_criteria_date
 from .imap_utf7 import encode as encode_utf7, decode as decode_utf7
 from .response_parser import parse_response, parse_message_list, parse_fetch_response
-from .util import to_bytes, to_unicode
+from .util import to_bytes, to_unicode, assert_imap_protocol
 xrange = moves.xrange
 
 if PY3:
@@ -1526,7 +1526,7 @@ def _maybe_int_to_bytes(val):
 
 
 def _parse_untagged_response(text):
-    assert text.startswith(b'* ')
+    assert_imap_protocol(text.startswith(b'* '))
     text = text[2:]
     if text.startswith((b'OK ', b'NO ')):
         return tuple(text.split(b' ', 1))
diff --git a/imapclient/response_lexer.py b/imapclient/response_lexer.py
index 88e09fb..2504c65 100644
--- a/imapclient/response_lexer.py
+++ b/imapclient/response_lexer.py
@@ -13,6 +13,8 @@ from __future__ import unicode_literals
 
 import six
 
+from .util import assert_imap_protocol
+
 __all__ = ["TokenSource"]
 
 CTRL_CHARS = frozenset(c for c in range(32))
@@ -97,7 +99,7 @@ class Lexer(object):
                     if nextchar in whitespace:
                         yield token
                     elif nextchar == DOUBLE_QUOTE:
-                        assert not token
+                        assert_imap_protocol(not token)
                         token.append(nextchar)
                         token.extend(read_until(stream_i, nextchar))
                         yield token
@@ -138,7 +140,7 @@ class LiteralHandlingIter:
             # A 'record' with a string which includes a literal marker, and
             # the literal itself.
             self.src_text = resp_record[0]
-            assert self.src_text.endswith(b"}"), self.src_text
+            assert_imap_protocol(self.src_text.endswith(b"}"), self.src_text)
             self.literal = resp_record[1]
         else:
             # just a line with no literals.
diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py
index 1b65ad4..4331be6 100644
--- a/imapclient/response_parser.py
+++ b/imapclient/response_parser.py
@@ -22,14 +22,11 @@ import six
 from .datetime_util import parse_to_datetime
 from .response_lexer import TokenSource
 from .response_types import BodyData, Envelope, Address, SearchIds
+from .exceptions import ProtocolError
 
 xrange = six.moves.xrange
 
-__all__ = ['parse_response', 'parse_message_list', 'ParseError']
-
-
-class ParseError(ValueError):
-    pass
+__all__ = ['parse_response', 'parse_message_list']
 
 
 def parse_response(data):
@@ -93,11 +90,11 @@ def gen_parsed_response(text):
     try:
         for token in src:
             yield atom(src, token)
-    except ParseError:
+    except ProtocolError:
         raise
     except ValueError:
         _, err, _ = sys.exc_info()
-        raise ParseError("%s: %s" % (str(err), token))
+        raise ProtocolError("%s: %s" % (str(err), token))
 
 
 def parse_fetch_response(text, normalise_times=True, uid_is_key=True):
@@ -121,12 +118,12 @@ def parse_fetch_response(text, normalise_times=True, uid_is_key=True):
         try:
             msg_response = six.next(response)
         except StopIteration:
-            raise ParseError('unexpected EOF')
+            raise ProtocolError('unexpected EOF')
 
         if not isinstance(msg_response, tuple):
-            raise ParseError('bad response type: %s' % repr(msg_response))
+            raise ProtocolError('bad response type: %s' % repr(msg_response))
         if len(msg_response) % 2:
-            raise ParseError('uneven number of response items: %s' % repr(msg_response))
+            raise ProtocolError('uneven number of response items: %s' % repr(msg_response))
 
         # always return the sequence of the message, so it is available
         # even if we return keyed by UID.
@@ -159,7 +156,7 @@ def _int_or_error(value, error_text):
     try:
         return int(value)
     except (TypeError, ValueError):
-        raise ParseError('%s: %s' % (error_text, repr(value)))
+        raise ProtocolError('%s: %s' % (error_text, repr(value)))
 
 
 def _convert_INTERNALDATE(date_string, normalise_times=True):
@@ -212,9 +209,9 @@ def atom(src, token):
         literal_len = int(token[1:-1])
         literal_text = src.current_literal
         if literal_text is None:
-            raise ParseError('No literal corresponds to %r' % token)
+            raise ProtocolError('No literal corresponds to %r' % token)
         if len(literal_text) != literal_len:
-            raise ParseError('Expecting literal of size %d, got %d' % (
+            raise ProtocolError('Expecting literal of size %d, got %d' % (
                 literal_len, len(literal_text)))
         return literal_text
     elif len(token) >= 2 and (token[:1] == token[-1:] == b'"'):
@@ -232,7 +229,7 @@ def parse_tuple(src):
             return tuple(out)
         out.append(atom(src, token))
     # no terminator
-    raise ParseError('Tuple incomplete before "(%s"' % _fmt_tuple(out))
+    raise ProtocolError('Tuple incomplete before "(%s"' % _fmt_tuple(out))
 
 
 def _fmt_tuple(t):
diff --git a/imapclient/util.py b/imapclient/util.py
index 3314ea5..aa89115 100644
--- a/imapclient/util.py
+++ b/imapclient/util.py
@@ -7,6 +7,8 @@ from __future__ import unicode_literals
 import logging
 from six import binary_type, text_type
 
+from . import exceptions
+
 logger = logging.getLogger(__name__)
 
 
@@ -27,3 +29,11 @@ def to_bytes(s, charset='ascii'):
     if isinstance(s, text_type):
         return s.encode(charset)
     return s
+
+
+def assert_imap_protocol(condition, message=None):
+    if not condition:
+        msg = "Server replied with a response that violates the IMAP protocol"
+        if message:
+            msg += "{}: {}".format(msg, message)
+        raise exceptions.ProtocolError(msg)

diff --git a/tests/test_imapclient.py b/tests/test_imapclient.py
index f8f458f..f56ca5b 100644
--- a/tests/test_imapclient.py
+++ b/tests/test_imapclient.py
@@ -12,7 +12,9 @@ import logging
 
 import six
 
-from imapclient.exceptions import CapabilityError, IMAPClientError
+from imapclient.exceptions import (
+    CapabilityError, IMAPClientError, ProtocolError
+)
 from imapclient.imapclient import IMAPlibLoggerAdapter
 from imapclient.fixed_offset import FixedOffset
 from imapclient.testable_imapclient import TestableIMAPClient as IMAPClient
@@ -651,3 +653,14 @@ class TestContextManager(IMAPClientTest):
         with self.assertRaises(ValueError):
             with self.client as _:
                 raise ValueError("Error raised inside the context manager")
+
+
+class TestProtocolError(IMAPClientTest):
+
+    def test_tagged_response_with_parse_error(self):
+        client = self.client
+        client._imap.tagged_commands = {sentinel.tag: None}
+        client._imap._get_response = lambda: b'NOT-A-STAR 99 EXISTS'
+
+        with self.assertRaises(ProtocolError):
+            client._consume_until_tagged_response(sentinel.tag, b'IDLE')
\ No newline at end of file
diff --git a/tests/test_response_parser.py b/tests/test_response_parser.py
index 946cd16..04694fc 100644
--- a/tests/test_response_parser.py
+++ b/tests/test_response_parser.py
@@ -16,9 +16,9 @@ from imapclient.response_parser import (
     parse_response,
     parse_message_list,
     parse_fetch_response,
-    ParseError,
 )
 from imapclient.response_types import Envelope, Address
+from imapclient.exceptions import ProtocolError
 from tests.util import unittest
 from .util import patch
 
@@ -160,7 +160,7 @@ class TestParseResponse(unittest.TestCase):
     def _test_parse_error(self, to_parse, expected_msg):
         if not isinstance(to_parse, list):
             to_parse = [to_parse]
-        self.assertRaisesRegex(ParseError, expected_msg,
+        self.assertRaisesRegex(ProtocolError, expected_msg,
                                parse_response, to_parse)
 
 
@@ -200,13 +200,13 @@ class TestParseFetchResponse(unittest.TestCase):
         self.assertEqual(parse_fetch_response([None]), {})
 
     def test_bad_msgid(self):
-        self.assertRaises(ParseError, parse_fetch_response, [b'abc ()'])
+        self.assertRaises(ProtocolError, parse_fetch_response, [b'abc ()'])
 
     def test_bad_data(self):
-        self.assertRaises(ParseError, parse_fetch_response, [b'2 WHAT'])
+        self.assertRaises(ProtocolError, parse_fetch_response, [b'2 WHAT'])
 
     def test_missing_data(self):
-        self.assertRaises(ParseError, parse_fetch_response, [b'2'])
+        self.assertRaises(ProtocolError, parse_fetch_response, [b'2'])
 
     def test_simple_pairs(self):
         self.assertEqual(parse_fetch_response([b'23 (ABC 123 StUfF "hello")']),
@@ -215,8 +215,8 @@ class TestParseFetchResponse(unittest.TestCase):
                                b'SEQ': 23}})
 
     def test_odd_pairs(self):
-        self.assertRaises(ParseError, parse_fetch_response, [b'(ONE)'])
-        self.assertRaises(ParseError, parse_fetch_response, [b'(ONE TWO THREE)'])
+        self.assertRaises(ProtocolError, parse_fetch_response, [b'(ONE)'])
+        self.assertRaises(ProtocolError, parse_fetch_response, [b'(ONE TWO THREE)'])
 
     def test_UID(self):
         self.assertEqual(parse_fetch_response([b'23 (UID 76)']),
@@ -230,7 +230,7 @@ class TestParseFetchResponse(unittest.TestCase):
                                b'SEQ': 23}})
 
     def test_bad_UID(self):
-        self.assertRaises(ParseError, parse_fetch_response, [b'(UID X)'])
+        self.assertRaises(ProtocolError, parse_fetch_response, [b'(UID X)'])
 
     def test_FLAGS(self):
         self.assertEqual(parse_fetch_response([b'23 (FLAGS (\Seen Stuff))']),
diff --git a/tests/test_util_functions.py b/tests/test_util_functions.py
index b3becb8..ba3f271 100644
--- a/tests/test_util_functions.py
+++ b/tests/test_util_functions.py
@@ -4,15 +4,17 @@
 
 from __future__ import unicode_literals
 
-from imapclient.exceptions import InvalidCriteriaError
+from imapclient.exceptions import InvalidCriteriaError, ProtocolError
 from imapclient.imapclient import (
     join_message_ids,
     _normalise_search_criteria,
     normalise_text_list,
     seq_to_parenstr,
     seq_to_parenstr_upper,
-    _quoted
+    _quoted,
+    _parse_untagged_response
 )
+from imapclient.util import assert_imap_protocol
 from tests.util import unittest
 
 
@@ -166,3 +168,17 @@ class Test_normalise_search_criteria(unittest.TestCase):
 
     def test_empty(self):
         self.assertRaises(InvalidCriteriaError, _normalise_search_criteria, '', None)
+
+
+class TestAssertIMAPProtocol(unittest.TestCase):
+
+    def test_assert_imap_protocol(self):
+        assert_imap_protocol(True)
+        with self.assertRaises(ProtocolError):
+            assert_imap_protocol(False)
+
+
+    def test_assert_imap_protocol_with_message(self):
+        assert_imap_protocol(True, 'foo')
+        with self.assertRaises(ProtocolError):
+            assert_imap_protocol(False, 'foo')

Avoid using assert when parsing server response
Some servers are not totally compliant with the IMAP protocol or sometimes give buggy output like: `"TEXT" "PLAIN" ("charset " " "utf-8"")`.

I believe IMAPClient should not `assert` when parsing the response from a server. Assert makes it hard to recover from such errors. A custom IMAPClient exception would be more helpful to the user.


tests/test_imapclient.py::TestListFolders::test_blanks tests/test_imapclient.py::TestListFolders::test_empty_response tests/test_imapclient.py::TestListFolders::test_folder_encode_off tests/test_imapclient.py::TestListFolders::test_funky_characters tests/test_imapclient.py::TestListFolders::test_list_folders tests/test_imapclient.py::TestListFolders::test_list_folders_NO tests/test_imapclient.py::TestListFolders::test_list_sub_folders tests/test_imapclient.py::TestListFolders::test_list_sub_folders_NO tests/test_imapclient.py::TestListFolders::test_mixed tests/test_imapclient.py::TestListFolders::test_quoted_specials tests/test_imapclient.py::TestListFolders::test_simple tests/test_imapclient.py::TestListFolders::test_unquoted_numeric_folder_name tests/test_imapclient.py::TestListFolders::test_unquoted_numeric_folder_name_parsed_as_long tests/test_imapclient.py::TestListFolders::test_utf7_decoding tests/test_imapclient.py::TestListFolders::test_without_quotes tests/test_imapclient.py::TestSelectFolder::test_normal tests/test_imapclient.py::TestSelectFolder::test_unselect tests/test_imapclient.py::TestAppend::test_with_msg_time tests/test_imapclient.py::TestAppend::test_without_msg_time tests/test_imapclient.py::TestAclMethods::test_getacl tests/test_imapclient.py::TestAclMethods::test_setacl tests/test_imapclient.py::TestIdleAndNoop::test_consume_until_tagged_response tests/test_imapclient.py::TestIdleAndNoop::test_idle tests/test_imapclient.py::TestIdleAndNoop::test_idle_check_blocking tests/test_imapclient.py::TestIdleAndNoop::test_idle_check_timeout tests/test_imapclient.py::TestIdleAndNoop::test_idle_check_with_data tests/test_imapclient.py::TestIdleAndNoop::test_idle_done tests/test_imapclient.py::TestIdleAndNoop::test_noop tests/test_imapclient.py::TestDebugLogging::test_IMAP_is_patched tests/test_imapclient.py::TestTimeNormalisation::test_default tests/test_imapclient.py::TestTimeNormalisation::test_pass_through tests/test_imapclient.py::TestNamespace::test_complex tests/test_imapclient.py::TestNamespace::test_folder_decoding tests/test_imapclient.py::TestNamespace::test_other_only tests/test_imapclient.py::TestNamespace::test_simple tests/test_imapclient.py::TestNamespace::test_without_folder_decoding tests/test_imapclient.py::TestCapabilities::test_caching tests/test_imapclient.py::TestCapabilities::test_has_capability tests/test_imapclient.py::TestCapabilities::test_post_auth_request tests/test_imapclient.py::TestCapabilities::test_preauth tests/test_imapclient.py::TestCapabilities::test_server_returned_capability_after_auth tests/test_imapclient.py::TestCapabilities::test_with_starttls tests/test_imapclient.py::TestId::test_id tests/test_imapclient.py::TestId::test_invalid_parameters tests/test_imapclient.py::TestId::test_no_support tests/test_imapclient.py::TestRawCommand::test_complex tests/test_imapclient.py::TestRawCommand::test_embedded_literal tests/test_imapclient.py::TestRawCommand::test_failed_continuation_wait tests/test_imapclient.py::TestRawCommand::test_invalid_input_type tests/test_imapclient.py::TestRawCommand::test_literal_at_end tests/test_imapclient.py::TestRawCommand::test_multiple_literals tests/test_imapclient.py::TestRawCommand::test_not_uid tests/test_imapclient.py::TestRawCommand::test_plain tests/test_imapclient.py::TestExpunge::test_expunge tests/test_imapclient.py::TestExpunge::test_id_expunge tests/test_imapclient.py::TestShutdown::test_shutdown tests/test_imapclient.py::TestContextManager::test_context_manager tests/test_imapclient.py::TestContextManager::test_context_manager_fail_closing tests/test_imapclient.py::TestContextManager::test_exception_inside_context_manager tests/test_imapclient.py::TestProtocolError::test_tagged_response_with_parse_error tests/test_response_parser.py::TestParseResponse::test_bad_literal tests/test_response_parser.py::TestParseResponse::test_bad_quoting tests/test_response_parser.py::TestParseResponse::test_complex_mixed tests/test_response_parser.py::TestParseResponse::test_deeper_nest_tuple tests/test_response_parser.py::TestParseResponse::test_empty_tuple tests/test_response_parser.py::TestParseResponse::test_envelopey tests/test_response_parser.py::TestParseResponse::test_envelopey_quoted tests/test_response_parser.py::TestParseResponse::test_incomplete_tuple tests/test_response_parser.py::TestParseResponse::test_int tests/test_response_parser.py::TestParseResponse::test_int_and_tuple tests/test_response_parser.py::TestParseResponse::test_literal tests/test_response_parser.py::TestParseResponse::test_literal_with_more tests/test_response_parser.py::TestParseResponse::test_nested_tuple tests/test_response_parser.py::TestParseResponse::test_nil tests/test_response_parser.py::TestParseResponse::test_quoted_specials tests/test_response_parser.py::TestParseResponse::test_square_brackets tests/test_response_parser.py::TestParseResponse::test_string tests/test_response_parser.py::TestParseResponse::test_tuple tests/test_response_parser.py::TestParseResponse::test_unquoted tests/test_response_parser.py::TestParseMessageList::test_basic tests/test_response_parser.py::TestParseMessageList::test_modseq tests/test_response_parser.py::TestParseMessageList::test_modseq_interleaved tests/test_response_parser.py::TestParseMessageList::test_modseq_no_space tests/test_response_parser.py::TestParseMessageList::test_one_id tests/test_response_parser.py::TestParseFetchResponse::test_Address_str tests/test_response_parser.py::TestParseFetchResponse::test_Address_str_ignores_encoding_error tests/test_response_parser.py::TestParseFetchResponse::test_BODY tests/test_response_parser.py::TestParseFetchResponse::test_BODYSTRUCTURE tests/test_response_parser.py::TestParseFetchResponse::test_BODY_HEADER_FIELDS tests/test_response_parser.py::TestParseFetchResponse::test_ENVELOPE tests/test_response_parser.py::TestParseFetchResponse::test_ENVELOPE_with_empty_addresses tests/test_response_parser.py::TestParseFetchResponse::test_ENVELOPE_with_invalid_date tests/test_response_parser.py::TestParseFetchResponse::test_ENVELOPE_with_no_date tests/test_response_parser.py::TestParseFetchResponse::test_FLAGS tests/test_response_parser.py::TestParseFetchResponse::test_INTERNALDATE tests/test_response_parser.py::TestParseFetchResponse::test_INTERNALDATE_normalised tests/test_response_parser.py::TestParseFetchResponse::test_UID tests/test_response_parser.py::TestParseFetchResponse::test_bad_UID tests/test_response_parser.py::TestParseFetchResponse::test_bad_data tests/test_response_parser.py::TestParseFetchResponse::test_bad_msgid tests/test_response_parser.py::TestParseFetchResponse::test_basic tests/test_response_parser.py::TestParseFetchResponse::test_literals tests/test_response_parser.py::TestParseFetchResponse::test_literals_and_keys_with_square_brackets tests/test_response_parser.py::TestParseFetchResponse::test_missing_data tests/test_response_parser.py::TestParseFetchResponse::test_mixed_types tests/test_response_parser.py::TestParseFetchResponse::test_multiple_messages tests/test_response_parser.py::TestParseFetchResponse::test_none_special_case tests/test_response_parser.py::TestParseFetchResponse::test_not_uid_is_key tests/test_response_parser.py::TestParseFetchResponse::test_odd_pairs tests/test_response_parser.py::TestParseFetchResponse::test_partial_fetch tests/test_response_parser.py::TestParseFetchResponse::test_same_message_appearing_multiple_times tests/test_response_parser.py::TestParseFetchResponse::test_simple_pairs tests/test_util_functions.py::Test_normalise_text_list::test_binary tests/test_util_functions.py::Test_normalise_text_list::test_iter tests/test_util_functions.py::Test_normalise_text_list::test_list tests/test_util_functions.py::Test_normalise_text_list::test_mixed_list tests/test_util_functions.py::Test_normalise_text_list::test_tuple tests/test_util_functions.py::Test_normalise_text_list::test_unicode tests/test_util_functions.py::Test_seq_to_parenstr::test_binary tests/test_util_functions.py::Test_seq_to_parenstr::test_iter tests/test_util_functions.py::Test_seq_to_parenstr::test_list tests/test_util_functions.py::Test_seq_to_parenstr::test_mixed_list tests/test_util_functions.py::Test_seq_to_parenstr::test_tuple tests/test_util_functions.py::Test_seq_to_parenstr::test_unicode tests/test_util_functions.py::Test_seq_to_parenstr_upper::test_binary tests/test_util_functions.py::Test_seq_to_parenstr_upper::test_iter tests/test_util_functions.py::Test_seq_to_parenstr_upper::test_list tests/test_util_functions.py::Test_seq_to_parenstr_upper::test_mixed_list tests/test_util_functions.py::Test_seq_to_parenstr_upper::test_tuple tests/test_util_functions.py::Test_seq_to_parenstr_upper::test_unicode tests/test_util_functions.py::Test_join_message_ids::test_binary tests/test_util_functions.py::Test_join_message_ids::test_binary_non_numeric tests/test_util_functions.py::Test_join_message_ids::test_int tests/test_util_functions.py::Test_join_message_ids::test_iter tests/test_util_functions.py::Test_join_message_ids::test_mixed_list tests/test_util_functions.py::Test_join_message_ids::test_tuple tests/test_util_functions.py::Test_join_message_ids::test_unicode tests/test_util_functions.py::Test_join_message_ids::test_unicode_non_numeric tests/test_util_functions.py::Test_normalise_search_criteria::test_None tests/test_util_functions.py::Test_normalise_search_criteria::test_binary tests/test_util_functions.py::Test_normalise_search_criteria::test_binary_with_charset tests/test_util_functions.py::Test_normalise_search_criteria::test_empty tests/test_util_functions.py::Test_normalise_search_criteria::test_ints tests/test_util_functions.py::Test_normalise_search_criteria::test_list tests/test_util_functions.py::Test_normalise_search_criteria::test_mixed_list tests/test_util_functions.py::Test_normalise_search_criteria::test_no_quoting_when_criteria_given_as_string tests/test_util_functions.py::Test_normalise_search_criteria::test_quoting tests/test_util_functions.py::Test_normalise_search_criteria::test_tuple tests/test_util_functions.py::Test_normalise_search_criteria::test_unicode tests/test_util_functions.py::Test_normalise_search_criteria::test_unicode_with_charset tests/test_util_functions.py::TestAssertIMAPProtocol::test_assert_imap_protocol tests/test_util_functions.py::TestAssertIMAPProtocol::test_assert_imap_protocol_with_message