Philipp Hörist pushed to branch master at gajim / python-nbxmpp


Commits:
79a39033 by Philipp Hörist at 2024-06-04T22:08:21+02:00
imprv: Protocol: Improve error message when parsing JIDs

- - - - -
7343544f by Philipp Hörist at 2024-06-04T22:08:21+02:00
chore: Update .gitignore

- - - - -


3 changed files:

- .gitignore
- nbxmpp/protocol.py
- test/unit/test_jid_parsing.py


Changes:

=====================================
.gitignore
=====================================
@@ -1,6 +1,7 @@
 build/
 __pycache__/
 .mypy_cache/
+.venv/
 nbxmpp.egg-info
 debian_build/
 dist


=====================================
nbxmpp/protocol.py
=====================================
@@ -648,9 +648,12 @@ class JID:
         if force_bare:
             resourcepart = None
 
-        return cls(localpart=localpart,
-                   domain=domainpart,
-                   resource=resourcepart)
+        try:
+            return cls(localpart=localpart,
+                       domain=domainpart,
+                       resource=resourcepart)
+        except Exception as error:
+            raise InvalidJid('Unable to parse "%s"' % jid_string) from error
 
     @classmethod
     @functools.cache
@@ -681,9 +684,12 @@ class JID:
             localpart = None
             domainpart = user_input
 
-        return cls(localpart=localpart,
-                   domain=domainpart,
-                   resource=None)
+        try:
+            return cls(localpart=localpart,
+                       domain=domainpart,
+                       resource=None)
+        except Exception as error:
+            raise InvalidJid('Unable to parse "%s"' % user_input) from error
 
     @classmethod
     @functools.cache
@@ -700,9 +706,12 @@ class JID:
         if resourcepart is not None:
             resourcepart = GLib.Uri.unescape_string(resourcepart)
 
-        return cls(localpart=localpart,
-                   domain=domainpart,
-                   resource=resourcepart)
+        try:
+            return cls(localpart=localpart,
+                       domain=domainpart,
+                       resource=resourcepart)
+        except Exception as error:
+            raise InvalidJid('Unable to parse "%s"' % iri_str) from error
 
     def __str__(self) -> str:
         if self.localpart:


=====================================
test/unit/test_jid_parsing.py
=====================================
@@ -3,6 +3,7 @@ import unittest
 
 from nbxmpp.protocol import DomainpartByteLimit
 from nbxmpp.protocol import DomainpartNotAllowedChar
+from nbxmpp.protocol import InvalidJid
 from nbxmpp.protocol import JID
 from nbxmpp.protocol import LocalpartByteLimit
 from nbxmpp.protocol import LocalpartNotAllowedChar
@@ -47,9 +48,11 @@ class JIDParsing(unittest.TestCase):
         ]
 
         for jid, exception in tests:
-            with self.assertRaises(exception):
+            with self.assertRaises(InvalidJid) as cm:
                 JID.from_string(jid)
 
+            self.assertIsInstance(cm.exception.__cause__, exception)
+
     def test_invalid_precis_jids(self):
         os.environ['NBXMPP_ENFORCE_PRECIS'] = 'true'
         tests = [
@@ -58,9 +61,11 @@ class JIDParsing(unittest.TestCase):
         ]
 
         for jid, exception in tests:
-            with self.assertRaises(exception):
+            with self.assertRaises(InvalidJid) as cm:
                 JID.from_string(jid)
 
+            self.assertIsInstance(cm.exception.__cause__, exception)
+
         del os.environ['NBXMPP_ENFORCE_PRECIS']
 
     def test_ip_literals(self):



View it on GitLab: 
https://dev.gajim.org/gajim/python-nbxmpp/-/compare/01364cf38a3d57ceacef6937a7300ce5e0c67939...7343544fe8e5e57d4fdbd7646ae31b261c065dee

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/python-nbxmpp/-/compare/01364cf38a3d57ceacef6937a7300ce5e0c67939...7343544fe8e5e57d4fdbd7646ae31b261c065dee
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to