From f0eaee68e4ac99fb8cbf61efaf7e30e851cfdadf Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 22 Sep 2016 09:55:18 -0700 Subject: [PATCH] bug: Fix traceback in get_missed_message_token_from_address(). If you supplied an unrecognizable address to our email system, or you had EMAIL_GATEWAY_PATTERN configured wrong, the get_missed_message_token_from_address() used to crash hard and cryptically with a traceback saying that you can't call startswith() on a None object. Now we throw a ZulipEmailForwardError exception. This will still lead to a traceback, but it should be easier to diagnose the problem. --- zerver/lib/email_mirror.py | 3 +++ zerver/tests/test_email_mirror.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/zerver/lib/email_mirror.py b/zerver/lib/email_mirror.py index 8ccd36d6b7..98908c45ba 100644 --- a/zerver/lib/email_mirror.py +++ b/zerver/lib/email_mirror.py @@ -90,6 +90,9 @@ def get_missed_message_token_from_address(address): # type: (text_type) -> text_type msg_string = get_email_gateway_message_string_from_address(address) + if msg_string is None: + raise ZulipEmailForwardError('Address not recognized by gateway.') + if not is_mm_32_format(msg_string): raise ZulipEmailForwardError('Could not parse missed message address') diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.py index b91413fb74..b6f28358c5 100644 --- a/zerver/tests/test_email_mirror.py +++ b/zerver/tests/test_email_mirror.py @@ -65,6 +65,14 @@ class TestEmailMirrorLibrary(ZulipTestCase): with self.assertRaises(ZulipEmailForwardError): get_token(address) + # Now test the case where we our address does not match the + # EMAIL_GATEWAY_PATTERN. + # This used to crash in an ugly way; we want to throw a proper + # exception. + address = 'alice@not-the-domain-we-were-expecting.com' + with self.assertRaises(ZulipEmailForwardError): + get_token(address) + class TestStreamEmailMessagesSuccess(ZulipTestCase): def test_receive_stream_email_messages_success(self):