Repository: alephdata/followthemoney

Issue:
Better error handling regarding invalid domains in e-mail cleanup
I believe https://github.com/alephdata/ingest-file/issues/253 should be fixed here in `followthemoney`, because https://github.com/alephdata/followthemoney/blob/4b102568a3b3d13a8c4b34de4b7dc0f856a08b9c/followthemoney/types/email.py#L71 might [raise a UnicodeError](https://docs.python.org/3/library/stdtypes.html?highlight=encode#str.encode).

The suggested change: return `None` if a `UnicodeError` is raised.

Discussion:

Patch:
diff --git a/followthemoney/types/email.py b/followthemoney/types/email.py
index 51052f28..275980a1 100644
--- a/followthemoney/types/email.py
+++ b/followthemoney/types/email.py
@@ -68,7 +68,10 @@ class EmailType(PropertyType):
         domain = domain.lower()
         domain = domain.rstrip(".")
         # handle unicode
-        domain = domain.encode("idna").decode("ascii")
+        try:
+            domain = domain.encode("idna").decode("ascii")
+        except UnicodeError:
+            return None
         if domain is not None and mailbox is not None:
             return "@".join((mailbox, domain))
         return None


Test patch:
diff --git a/tests/types/test_emails.py b/tests/types/test_emails.py
index 0c233e81..57d798b0 100644
--- a/tests/types/test_emails.py
+++ b/tests/types/test_emails.py
@@ -16,6 +16,18 @@ class EmailsTest(unittest.TestCase):
         self.assertEqual(emails.clean(5), None)
         self.assertEqual(emails.clean("foo@PUDO.org"), "foo@pudo.org")
         self.assertEqual(emails.clean("FOO@PUDO.org"), "FOO@pudo.org")
+        self.assertEqual(
+            emails.clean(
+                "foo@0123456789012345678901234567890123456789012345678901234567890.example.com"
+            ),
+            "foo@0123456789012345678901234567890123456789012345678901234567890.example.com",
+        )
+        self.assertEqual(
+            emails.clean(
+                "foo@0123456789012345678901234567890123456789012345678901234567890123.example.com"
+            ),
+            None,
+        )
 
     def test_domain_validity(self):
         self.assertTrue(emails.validate("foo@pudo.org"))